Learn R Programming

sf (version 0.4-3)

st_crs: Retrieve coordinate reference system from object

Description

Retrieve coordinate reference system from sf or sfc object

Set or replace retrieve coordinate reference system from object

Usage

st_crs(x, ...)

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

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

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

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

# 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

wkt

character well-known-text representation of the crs

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; codeepsg or proj4string, or one of proj4strings named components without the +; see examples

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).

Format

An object of class crs of length 2.

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

Run this code
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)
sfc = st_sfc(st_point(c(0,0)), st_point(c(1,1)))
st_crs(sfc) = 4326
sfc
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
st_crs("+init=epsg:3857")$epsg 
st_crs("+init=epsg:3857")$proj4string 
st_crs("+init=epsg:3857 +units=km")$b     # numeric
st_crs("+init=epsg:3857 +units=km")$units # character

Run the code above in your browser using DataLab