R: Get or set a coordinate reference system

crs {terra} | R Documentation

## Get or set a coordinate reference system

### Description

Get or set the coordinate reference system (CRS), also referred to as a "projection", of a SpatRaster or SpatVector object.

Setting a new CRS does not change the data itself, it just changes the label. So you should only set the CRS of a dataset (if it does not come with one) to what it *is*, not to what you would *like it to be*. See `project` to *transform* an object from one CRS to another.

### Usage

    ## S4 method for signature 'SpatRaster'
    crs(x, proj=FALSE, describe=FALSE, parse=FALSE)

    ## S4 method for signature 'SpatVector'
    crs(x, proj=FALSE, describe=FALSE, parse=FALSE)

    ## S4 replacement method for signature 'SpatRaster'
    crs(x)<-value

    ## S4 replacement method for signature 'SpatVector'
    crs(x)<-value

### Arguments

`x` | SpatRaster or SpatVector
`proj` | logical. If `TRUE` the crs is returned in PROJ-string notation
`describe` | logical. If `TRUE` the name, EPSG code, and the name and extent of the area of use are returned if known
`value` | character string describing a coordinate reference system. This can be in a WKT format, as a <authority:number> code such as "EPSG:4326", or a PROJ-string format such as "+proj=utm +zone=12" (see Note)
`parse` | logical. If `TRUE`, wkt parts are parsed into a vector (each line becomes an element)

### Value

character or modified SpatRaster/Vector

### Note

Projections are handled by the PROJ/GDAL libaries. Recent changes in the PROJ library to improve transformations between datums have degraded the library's usability. The PROJ developers suggest to no longer use the proj-string notation to define a CRS, but use the WKT2 or <authority>:<code> notation instead. These alternative systems work for formally described CRSs that are in databases, but they do not cover the infinite number of CRSs that exist. It is not practical to define one's own custom CRS with WKT2. Moreover, unlike the proj-notation, these newer systems are hard to read and that leads to code that cannot be easily understood and, therefore, is more error-prone.

It is still possible to use the PROJ-string notation with one major caveat: the datum should be WGS84 (or the equivalent NAD83) – if you want to transform your data to a coordinate reference system with a different datum. Thus as long as you use WGS84, or an ellipsoid instead of a datum, you can safely use PROJ-strings to represent your CRS; including to define your own custom CRS.

### Examples

    r <- rast()
    crs(r)
    crs(r, describe=TRUE, proj=TRUE)

    crs(r) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
    crs(r)

    # You can also use epsg codes
    crs(r)  <- "epsg:25831"
    crs(r, describe=TRUE)$area

[Package terra version 1.4-22 Index]