Skip to contents

Extract the WKT version of the CRS associated with a string, number, sf object or Spat* object. Well-known text (WKT) is a character string representation of coordinate reference systems (CRS). It identifies the parameters of each CRS precisely and is the standard used by sf and terra.

Usage

pull_crs(.data, ...)

Arguments

.data

Input potentially including or representing a CRS. It could be an sf or sfc object, a SpatRaster or SpatVector object, a crs object from sf::st_crs(), a character string (for example a PROJ string) or an integer (representing an EPSG code).

...

Ignored.

Value

A character string containing the CRS in WKT format, or NA if no CRS can be determined.

Details

Although the WKT representation is the same, the sf and terra APIs differ slightly. For example, sf can do:

sf::st_transform(x, 25830)

While the terra equivalent is:

terra::project(bb, "epsg:25830")

Knowing the WKT helps smooth workflows when working with different packages and object types.

Internals

A thin wrapper around sf::st_crs() and terra::crs().

See also

terra::crs() and sf::st_crs() to learn how these packages handle CRS definitions.

Examples


# sf objects.

sfobj <- sf::st_as_sfc("MULTIPOINT ((0 0), (1 1))", crs = 4326)

fromsf1 <- pull_crs(sfobj)
fromsf2 <- pull_crs(sf::st_crs(sfobj))

# terra objects.

v <- terra::vect(sfobj)
r <- terra::rast(v)

fromterra1 <- pull_crs(v)
fromterra2 <- pull_crs(r)

# Integers.
fromint <- pull_crs(4326)

# Characters.
fromchar <- pull_crs("epsg:4326")

all(
  fromsf1 == fromsf2,
  fromsf2 == fromterra1,
  fromterra1 == fromterra2,
  fromterra2 == fromint,
  fromint == fromchar
)
#> [1] TRUE

cat(fromsf1)
#> GEOGCRS["WGS 84",
#>     ENSEMBLE["World Geodetic System 1984 ensemble",
#>         MEMBER["World Geodetic System 1984 (Transit)"],
#>         MEMBER["World Geodetic System 1984 (G730)"],
#>         MEMBER["World Geodetic System 1984 (G873)"],
#>         MEMBER["World Geodetic System 1984 (G1150)"],
#>         MEMBER["World Geodetic System 1984 (G1674)"],
#>         MEMBER["World Geodetic System 1984 (G1762)"],
#>         MEMBER["World Geodetic System 1984 (G2139)"],
#>         ELLIPSOID["WGS 84",6378137,298.257223563,
#>             LENGTHUNIT["metre",1]],
#>         ENSEMBLEACCURACY[2.0]],
#>     PRIMEM["Greenwich",0,
#>         ANGLEUNIT["degree",0.0174532925199433]],
#>     CS[ellipsoidal,2],
#>         AXIS["geodetic latitude (Lat)",north,
#>             ORDER[1],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>         AXIS["geodetic longitude (Lon)",east,
#>             ORDER[2],
#>             ANGLEUNIT["degree",0.0174532925199433]],
#>     USAGE[
#>         SCOPE["Horizontal component of 3D system."],
#>         AREA["World."],
#>         BBOX[-90,-180,90,180]],
#>     ID["EPSG",4326]]