I'll add this as an answer (was originally meant as a comment). Perhaps it helps someone since there is and has been a lot of confusion about this. It expands the answers above. There is another helpful question with answers.
In short, to print the wkt comment and avoid the warnings with PROJ6 CRS objects, use sp::wkt
instead of sp::proj4string
:
sp::wkt(x)
[1] "GEOGCRS[\"WGS 84 (with axis order normalized for visualization)\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]]]"
To assign a CRS to a SpatialPolygons object, which does not have a CRS, use the sp::proj4string
function even though you are assigning wkt (= well-known text, PROJ6 CRS format). Note how you can now omit the SRS_string
argument within sp::CRS()
.
library(sp)
x <- SpatialPolygons(
list(Polygons(
list(Polygon(cbind(c(1,2,3,4,1), c(3,8,10,12,3)))),
ID = 1))
)
proj4string(x) <- CRS("EPSG:4326")
proj4string(x) # print the PROJ4 string
#> Warning in proj4string(x): CRS object has comment, which is lost in output
#> [1] "+proj=longlat +datum=WGS84 +no_defs"
wkt(x) # print the PROJ6 wkt comment
#> [1] "GEOGCRS[\"WGS 84 (with axis order normalized for visualization)\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]]]"
To borrow the PROJ6 from another SpatialPolygons object, you can either use only proj4string
or use the combination of proj4string
and wkt
:
y <- SpatialPolygons(
list(Polygons(
list(Polygon(cbind(c(1,2,3,4,1), c(3,8,10,12,3)))),
ID = 1))
)
proj4string(y) <- proj4string(x) # proj4string(y) <- wkt(x) would also work
# but wkt(y) <- wkt(x) would not because wkt() has no assingment.
# proj4string(y) <- CRS(wkt(x)) also works as shown by JonasV
proj4string(y)
#> Warning in proj4string(y): CRS object has comment, which is lost in output
#> [1] "+proj=longlat +datum=WGS84 +no_defs"
wkt(y)
#> [1] "GEOGCRS[\"WGS 84 (with axis order normalized for visualization)\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]]]"
The sp::proj4string()
should be understood as a function name. It does not necessarily have to do with PROJ4 any longer if it is used in the assignment. The good news is that you only need to change the "inside" from "+init=epsg:4326" to "EPSG:4326" in the sp::CRS()
function and things should work as previously as long as you do not use the sp::proj4string()
function to retrieve PROJ6 (wkt) information. If you do, you'll get a warning, but the comment is actually not lost anyway in the assignment (as demonstrated by the proj4string(y) <- proj4string(x)
example). It is only lost if you use proj4string()
to print CRS information:
comment(proj4string(x))
#> NULL
EDIT: What if you want both, the string and comment? Use raster::crs()
. This can be useful to keep it PROJ6 through a function/script.
raster::crs(x)
#> CRS arguments: +proj=longlat +datum=WGS84 +no_defs
comment(raster::crs(x))
[1] "GEOGCRS[\"WGS 84 (with axis order normalized for visualization)\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]]]"
Created on 2021-05-27 by the reprex package (v2.0.0)