Learn R Programming

tidyterra (version 1.3.0)

pull_crs: Extract CRS in WKT format

Description

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

Value

A WKT representation of the corresponding CRS.

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 a integer (representing an EPSG code).

...

Ignored.

Internals

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

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.

See Also

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

Examples

Run this code

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

cat(fromsf1)

Run the code above in your browser using DataLab