st_transform: Transform or convert coordinates of simple feature

Description Usage Arguments Details See Also Examples

View source: R/transform.R

Description

Transform or convert coordinates of simple feature

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
st_transform(x, crs, ...)

## S3 method for class 'sfc'
st_transform(
  x,
  crs = st_crs(x),
  ...,
  aoi = numeric(0),
  pipeline = character(0),
  reverse = FALSE,
  desired_accuracy = -1,
  allow_ballpark = TRUE,
  partial = TRUE,
  check = FALSE
)

## S3 method for class 'sf'
st_transform(x, crs = st_crs(x), ...)

## S3 method for class 'sfg'
st_transform(x, crs = st_crs(x), ...)

st_wrap_dateline(x, options, quiet)

## S3 method for class 'sfc'
st_wrap_dateline(x, options = "WRAPDATELINE=YES", quiet = TRUE)

## S3 method for class 'sf'
st_wrap_dateline(x, options = "WRAPDATELINE=YES", quiet = TRUE)

## S3 method for class 'sfg'
st_wrap_dateline(x, options = "WRAPDATELINE=YES", quiet = TRUE)

sf_proj_info(type = "proj", path)

Arguments

x

object of class sf, sfc or sfg

crs

target coordinate reference system: object of class 'crs', or input string for st_crs

...

ignored

aoi

area of interest, in degrees: WestLongitude, SouthLatitude, EastLongitude, NorthLatitude

pipeline

character; coordinate operation pipeline, for overriding the default operation

reverse

boolean; has only an effect when pipeline is defined: if TRUE, the inverse operation of the pipeline is applied

desired_accuracy

numeric; Only coordinate operations that offer an accuracy of at least the one specified will be considered; a negative value disables this feature (requires GDAL >= 3.3)

allow_ballpark

logical; are ballpark (low accuracy) transformations allowed? (requires GDAL >= 3.3)

partial

logical; allow for partial projection, if not all points of a geometry can be projected (corresponds to setting environment variable OGR_ENABLE_PARTIAL_REPROJECTION to TRUE)

check

logical; if TRUE, perform a sanity check on resulting polygons

options

character; should have "WRAPDATELINE=YES" to function; another parameter that is used is "DATELINEOFFSET=10" (where 10 is the default value)

quiet

logical; print options after they have been parsed?

type

character; one of have_datum_files, proj, ellps, datum, units or prime_meridians; see Details.

path

character; PROJ search path to be set

Details

Transforms coordinates of object to new projection. Features that cannot be transformed are returned as empty geometries.

The st_transform method for sfg objects assumes that the CRS of the object is available as an attribute of that name.

For a discussion of using options, see https://github.com/r-spatial/sf/issues/280 and https://github.com/r-spatial/sf/issues/541

sf_proj_info lists the available projections, ellipses, datums, units, or data search path of the PROJ library when type is equal to proj, ellps, datum, units or path; when type equals have_datum_files a boolean is returned indicating whether datum files are installed and accessible (checking for conus).

for PROJ >= 6, sf_proj_info does not provide option type = "datums". PROJ < 6 does not provide the option type = "prime_meridians".

for PROJ >= 7.1.0, the "units" query of sf_proj_info returns the to_meter variable as numeric, previous versions return a character vector containing a numeric expression.

See Also

Projecting simple feature geometries to projections not supported by GDAL may be done by st_transform_proj, part of package lwgeom.

sf_project projects a matrix of coordinates, bypassing GDAL altogether

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
p1 = st_point(c(7,52))
p2 = st_point(c(-30,20))
sfc = st_sfc(p1, p2, crs = 4326)
sfc
st_transform(sfc, 3857)
st_transform(st_sf(a=2:1, geom=sfc), "+init=epsg:3857")
try(st_transform(sfc, 3857, aoi = c(-280,-90,180,90)))
if (sf_extSoftVersion()["GDAL"] >= "3.0.0") {
  st_transform(sfc, pipeline =
	  "+proj=pipeline +step +proj=axisswap +order=2,1") # reverse axes
  st_transform(sfc, pipeline =
	  "+proj=pipeline +step +proj=axisswap +order=2,1", reverse = TRUE) # also reverse axes
}
nc = st_read(system.file("shape/nc.shp", package="sf"))
st_area(nc[1,]) # area from long/lat
st_area(st_transform(nc[1,], 32119)) # NC state plane, m
st_area(st_transform(nc[1,], 2264)) # NC state plane, US foot
library(units)
set_units(st_area(st_transform(nc[1,], 2264)), m^2)
st_transform(structure(p1, proj4string = "+init=epsg:4326"), "+init=epsg:3857")
st_wrap_dateline(st_sfc(st_linestring(rbind(c(-179,0),c(179,0))), crs = 4326))
library(maps)
wrld <- st_as_sf(maps::map("world", fill = TRUE, plot = FALSE))
wrld_wrap <- st_wrap_dateline(wrld, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"),
   quiet = TRUE)
wrld_moll <- st_transform(wrld_wrap, "+proj=moll")
plot(st_geometry(wrld_moll), col = "transparent")
sf_proj_info("datum")

Example output

Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
Geometry set for 2 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: -30 ymin: 20 xmax: 7 ymax: 52
geographic CRS: WGS 84
POINT (7 52)
POINT (-30 20)
Geometry set for 2 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: -3339585 ymin: 2273031 xmax: 779236.4 ymax: 6800125
projected CRS:  WGS 84 / Pseudo-Mercator
POINT (779236.4 6800125)
POINT (-3339585 2273031)
Simple feature collection with 2 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: -3339585 ymin: 2273031 xmax: 779236.4 ymax: 6800125
projected CRS:  WGS 84 / Pseudo-Mercator
  a                     geom
1 2 POINT (779236.4 6800125)
2 1 POINT (-3339585 2273031)
Warning message:
In CPL_crs_from_input(x) :
  GDAL Message 1: +init=epsg:XXXX syntax is deprecated. It might return a CRS with a non-EPSG compliant axis order.
Error in CPL_transform(x, crs, aoi, pipeline, reverse) : 
  values for area of interest not accepted
In addition: Warning message:
In CPL_transform(x, crs, aoi, pipeline, reverse) :
  GDAL Error 1: Invalid dfWestLongitudeDeg
Geometry set for 2 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: 20 ymin: -30 xmax: 52 ymax: 7
geographic CRS: WGS 84
POINT (52 7)
POINT (20 -30)
Reading layer `nc' from data source `/usr/lib/R/site-library/sf/shape/nc.shp' using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
1137388604 [m^2]
1137598162 [m^2]
12244955726 [US_survey_foot^2]
udunits system database from /usr/share/xml/udunits
1137598162 [m^2]
POINT (779236.4 6800125)
Geometry set for 1 feature 
geometry type:  MULTILINESTRING
dimension:      XY
bbox:           xmin: -180 ymin: 0 xmax: 180 ymax: 0
geographic CRS: WGS 84
MULTILINESTRING ((-179 0, -180 0), (180 0, 179 0))
Warning messages:
1: In CPL_wrap_dateline(st_geometry(x), options, quiet) :
  GDAL Error 1: IllegalArgumentException: Points of LinearRing do not form a closed linestring
2: In CPL_wrap_dateline(st_geometry(x), options, quiet) :
  GDAL Error 1: IllegalArgumentException: Points of LinearRing do not form a closed linestring
data frame with 0 columns and 0 rows

sf documentation built on March 17, 2021, 5:08 p.m.