CRS of Polygon – How to Set Coordinate Reference System (CRS) for a Simple Polygon

coordinate systemrsf

I have a simple polygon:

extent = sf::st_polygon(list(matrix(c(-88.17182, 40.06722, -88.17182, 43.06722, -85.17182, 43.06722, -85.17182, 40.06722, -88.17182, 40.06722), ncol=2, byrow=TRUE))).

How can I set the CRS to EPSG:4326?

I thought certainly sf::st_set_crs(extent, 'EPSG:4326') or sf::st_crs(extent) <- 4326 would work, but both fail with no applicable method for 'st_crs<-' applied to an object of class "c('XY', 'POLYGON', 'sfg')".

Best Answer

Wrap the st_csr call on each side. Although, you are missing a step sf::st_sfc in creating the correct object type.

e <- sf::st_sfc(sf::st_polygon(list(matrix(c(-88.17182, 40.06722, 
                -88.17182, 43.06722, -85.17182, 43.06722, -85.17182, 
                40.06722, -88.17182, 40.06722), ncol=2, byrow=TRUE))))

    sf::st_crs(e) <- sf::st_crs(4326) 

The right side creates the projection object which is then piped into the spatial object. Although, with valid sfc geometry you can now now pass the crs directly.

e <- sf::st_sfc(sf::st_polygon(list(matrix(c(-88.17182, 40.06722, 
                -88.17182, 43.06722, -85.17182, 43.06722, -85.17182, 
                40.06722, -88.17182, 40.06722), ncol=2, byrow=TRUE))), 
                crs=4326)
Related Question