Retrieve coordinate reference system from sf or sfc object

Set or replace retrieve coordinate reference system from object

st_crs(x, ...)

# S3 method for sf
st_crs(x, ...)

# S3 method for numeric
st_crs(x, proj4text = "", valid = TRUE, ...)

# S3 method for character
st_crs(x, ..., wkt)

# S3 method for sfc
st_crs(x, ..., parameters = FALSE)

# S3 method for bbox
st_crs(x, ...)

# S3 method for crs
st_crs(x, ...)

st_crs(x) <- value

# S3 method for sf
st_crs(x) <- value

# S3 method for sfc
st_crs(x) <- value

st_set_crs(x, value)

NA_crs_

# S3 method for crs
is.na(x)

# S3 method for crs
$(x, name)

Arguments

x

numeric, character, or object of class sf or sfc

...

ignored

proj4text

character. Must be used in conjunction with valid = FALSE.

valid

default TRUE. This allows to create crs without checking against the local proj4 database. It can be used to synchronize crs with a remote database, but avoid it as much as possible.

wkt

character well-known-text representation of the crs

parameters

logical; FALSE by default; if TRUE return a list of coordinate reference system parameters, with named elements SemiMajor, InvFlattening, units_gdal, IsVertical, WktPretty, and Wkt

value

one of (i) character: a valid proj4string (ii) integer, a valid EPSG value (numeric), or (iii) a list containing named elements proj4string (character) and/or epsg (integer) with (i) and (ii).

name

element name; epsg or proj4string, or one of proj4strings named components without the +; see examples

Format

An object of class crs of length 2.

Value

If x is numeric, return crs object for SRID x; if x is character, return crs object for proj4string x; if wkt is given, return crs object for well-known-text representation wkt; if x is of class sf or sfc, return its crs object.

Object of class crs, which is a list with elements epsg (length-1 integer) and proj4string (length-1 character).

Details

The *crs functions create, get, set or replace the crs attribute of a simple feature geometry list-column. This attribute is of class crs, and is a list consisting of epsg (integer EPSG code) and proj4string (character). Two objects of class crs are semantically identical when: (1) they are completely identical, or (2) they have identical proj4string but one of them has a missing EPSG ID. As a consequence, equivalent but different proj4strings, e.g. "+proj=longlat +datum=WGS84" and "+datum=WGS84 +proj=longlat" , are considered different. The operators == and != are overloaded for crs objects to establish semantical identity.

In case a coordinate reference system is replaced, no transformation takes place and a warning is raised to stress this. EPSG values are either read from proj4strings that contain +init=epsg:... or set to 4326 in case the proj4string contains +proj=longlat and +datum=WGS84, literally.

If both epsg and proj4string are provided, they are assumed to be consistent. In processing them, the EPSG code, if not missing valued, is used and the proj4string is derived from it by a call to GDAL (which in turn will call PROJ.4). Warnings are raised when epsg is not consistent with a proj4string that is already present.

NA_crs_ is the crs object with missing values for epsg and proj4string.

Examples

sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) sf = st_sf(a = 1:2, geom = sfc) st_crs(sf) = 4326 st_geometry(sf)
#> Geometry set for 2 features #> geometry type: POINT #> dimension: XY #> bbox: xmin: 0 ymin: 0 xmax: 1 ymax: 1 #> epsg (SRID): 4326 #> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> POINT (0 0)
#> POINT (1 1)
sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) st_crs(sfc) = 4326 sfc
#> Geometry set for 2 features #> geometry type: POINT #> dimension: XY #> bbox: xmin: 0 ymin: 0 xmax: 1 ymax: 1 #> epsg (SRID): 4326 #> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> POINT (0 0)
#> POINT (1 1)
sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1))) library(dplyr) x = sfc %>% st_set_crs(4326) %>% st_transform(3857) x
#> Geometry set for 2 features #> geometry type: POINT #> dimension: XY #> bbox: xmin: 0 ymin: -7.081155e-10 xmax: 111319.5 ymax: 111325.1 #> epsg (SRID): 3857 #> proj4string: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
#> POINT (0 -7.081155e-10)
#> POINT (111319.5 111325.1)
st_crs("+init=epsg:3857")$epsg
#> [1] 3857
st_crs("+init=epsg:3857")$proj4string
#> [1] "+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +nadgrids=@null +units=m +no_defs"
st_crs("+init=epsg:3857 +units=km")$b # numeric
#> [1] 6378137
st_crs("+init=epsg:3857 +units=km")$units # character
#> [1] "km"