Motivation
A common source of silent failures in raster reprojection is a CRS
string that PROJ accepts but interprets differently from what was
intended. The classic example is a bare +proj= PROJ string:
PROJ treats it as a coordinate operation (a unit conversion or
a transformation step) rather than as a full CRS definition that
includes a datum.
crs_describe() calls PROJ directly via the C API and
returns every output representation it can produce. This makes it easy
to check whether a string is being read as a CRS or as an operation, and
to convert between formats.
EPSG codes: the safe baseline
d <- crs_describe("EPSG:4326")
cat("id:", d$id, "\n")
#> id: NA
cat("WKT2 (first 120 chars):\n")
#> WKT2 (first 120 chars):
cat(substr(d$wkt2_2019, 1, 120), "...\n")
#> GEOGCRS["WGS 84",
#> ENSEMBLE["World Geodetic System 1984 ensemble",
#> MEMBER["World Geodetic System 1984 (Transi ...
cat("PROJ4:", d$proj4, "\n")
#> PROJ4: +proj=longlat +datum=WGS84 +no_defs +type=crsBare PROJ string: treated as an operation
Without +type=crs, PROJ interprets
+proj=laea as a conversion step, not a CRS. It has no WKT1
form and no datum information.
d_bare <- crs_describe("+proj=laea")
cat("id:", d_bare$id, "\n")
#> id: laea
cat("description:", d_bare$description, "\n")
#> description: PROJ-based coordinate operation
cat("wkt2_2019:", substr(d_bare$wkt2_2019, 1, 60), "...\n")
#> wkt2_2019: CONVERSION["PROJ-based coordinate operation",
#> METHOD["PR ...Adding +type=crs: now it is a CRS
d_typed <- crs_describe("+proj=laea +datum=WGS84 +type=crs")
cat("id:", d_typed$id, "\n")
#> id: NA
cat("description:", d_typed$description, "\n")
#> description: unknown
cat("WKT2 (first 200 chars):\n")
#> WKT2 (first 200 chars):
cat(substr(d_typed$wkt2_2019, 1, 200), "...\n")
#> PROJCRS["unknown",
#> BASEGEOGCRS["unknown",
#> DATUM["World Geodetic System 1984",
#> ELLIPSOID["WGS 84",6378137,298.257223563,
#> LENGTHUNIT["metre",1]],
#> ID[" ...EPSG:3412 (Antarctic Polar Stereographic)
d3412 <- crs_describe("EPSG:3412")
cat("id:", d3412$id, "\n")
#> id: NA
cat("description:", d3412$description, "\n")
#> description: NSIDC Sea Ice Polar Stereographic South
cat("PROJ4:", d3412$proj4, "\n")
#> PROJ4: +proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs +type=crsWKT as input
crs_describe() accepts WKT strings too. You can
round-trip from EPSG to WKT and back:
wkt <- crs_describe("EPSG:32755")$wkt1_gdal
cat("WKT1 (first 100 chars):", substr(wkt, 1, 100), "...\n")
#> WKT1 (first 100 chars): PROJCS["WGS 84 / UTM zone 55S",
#> GEOGCS["WGS 84",
#> DATUM["WGS_1984",
#> SPHEROID[ ...
d_from_wkt <- crs_describe(wkt)
cat("Round-trip id:", d_from_wkt$id, "\n")
#> Round-trip id: NAQuick validity check helper
A simple function that flags whether a string is recognised as a CRS:
is_crs <- function(x) {
d <- crs_describe(x)
is.na(d$error) && !is.na(d$wkt1_gdal)
}
is_crs("EPSG:4326")
#> [1] TRUE
is_crs("+proj=laea") # FALSE - operation, not CRS
#> [1] FALSE
is_crs("+proj=laea +datum=WGS84 +type=crs") # TRUE
#> [1] TRUE