library(sf)
## Linking to GEOS 3.7.0, GDAL 2.3.2, PROJ 5.2.0
library(units)
## udunits system database from /usr/share/xml/udunits
# Lima location
lima = c(-77.040660, -12.047227) %>% st_point %>% st_sfc(crs = 4326)
# Queried coordinates
x = st_point(c(8761774.367, 237636))
# All possible EPSG codes
epsg = rgdal::make_EPSG()
epsg = epsg[!is.na(epsg$code), ]
# Calculate distances from Lima
for(i in 1:nrow(epsg)) {
y = st_sfc(x, crs = epsg$code[i])
y = try(st_transform(y, crs = 4326), silent = TRUE)
if(class(y)[1] != "try-error") {
dist = st_distance(lima, y)
dist = dist %>% set_units("m") %>% as.numeric
epsg$dist[i] = dist
}
}
## Warning in CPL_transform(x, crs$proj4string): GDAL
## Error 6: Failed to initialize PROJ pipeline from `
## +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0
## +geoidgrids=g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx
## +vunits=m +no_defs' to `+proj=longlat +datum=WGS84 +no_defs': failed to
## load datum shift file
## Warning in CPL_transform(x, crs$proj4string): GDAL
## Error 6: Failed to initialize PROJ pipeline from `
## +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0
## +geoidgrids=g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx
## +vunits=m +no_defs' to `+proj=longlat +datum=WGS84 +no_defs': failed to
## load datum shift file
## Warning in CPL_transform(x, crs$proj4string): GDAL
## Error 6: Failed to initialize PROJ pipeline from `
## +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0
## +geoidgrids=g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx
## +vunits=m +no_defs' to `+proj=longlat +datum=WGS84 +no_defs': failed to
## load datum shift file
## Warning in CPL_transform(x, crs$proj4string): GDAL Error 6: Failed
## to initialize PROJ pipeline from `+proj=longlat +ellps=GRS80
## +geoidgrids=g2012a_conus.gtx,g2012a_alaska.gtx,g2012a_guam.gtx,g2012a_hawaii.gtx,g2012a_puertorico.gtx,g2012a_samoa.gtx
## +vunits=m +no_defs' to `+proj=longlat +datum=WGS84 +no_defs': failed to
## load datum shift file
## Warning in CPL_transform(x, crs$proj4string): GDAL Error 6: Failed to
## initialize PROJ pipeline from `+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0
## +datum=WGS84 +units=m +geoidgrids=egm08_25.gtx +vunits=m +no_defs' to `
## +proj=longlat +datum=WGS84 +no_defs': failed to load datum shift file
## Warning in CPL_transform(x, crs$proj4string): GDAL Error 6: Failed to
## initialize PROJ pipeline from `+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0
## +datum=WGS84 +units=m +geoidgrids=egm08_25.gtx +vunits=m +no_defs' to `
## +proj=longlat +datum=WGS84 +no_defs': failed to load datum shift file
# Result
epsg[order(epsg$dist), c("code", "note", "dist")] %>% head
## code note dist
## 1712 3207 # WGS 84 / SCAR IMW SQ01-02 623027.4
## 786 2279 # NAD83 / Texas South (ftUS) 664247.1
## 1426 2920 # NAD83(HARN) / Texas South (ftUS) 664247.1
## 2177 3672 # NAD83(NSRS2007) / Texas South (ftUS) 664247.1
## 3272 6586 # NAD83(2011) / Texas South (ftUS) 664247.1
## 2085 3580 # NAD83 / NWT Lambert 678676.6