Transform or convert coordinates of simple feature

st_transform(x, crs, ...)

# S3 method for sfc
st_transform(x, crs, ..., partial = TRUE, check = FALSE,
  use_gdal = TRUE)

# S3 method for sf
st_transform(x, crs, ...)

# S3 method for sfg
st_transform(x, crs, ...)

st_proj_info(type = "proj")

st_wrap_dateline(x, options, quiet)

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

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

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

Arguments

x

object of class sf, sfc or sfg

crs

coordinate reference system: integer with the EPSG code, or character with proj4string

...

ignored

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; perform a sanity check on resulting polygons?

use_gdal

logical; this parameter is deprecated. For transformations using PROJ.4 directly rather than indirectly through GDAL, use st_transform_proj of package lwgeom (see Details)

type

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

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?

Details

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

st_transform uses GDAL for coordinate transformations; internally, GDAL converts the proj4string into a well-known-text representation, before passing that on to PROJ.4. In this process, some information can get lost. Adding parameter +wktext to the proj4string definition may resolve this; see https://github.com/edzer/sp/issues/42.

Some PROJ.4 projections are not supported by GDAL, e.g. "+proj=wintri" because it does not have an inverse projection. Projecting to unsupported projections can be done by st_transform_proj, part of package lwgeom. Note that the unsupported proj4string cannot be passed as argument to st_crs, but has to be given as character string.

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

st_proj_info lists the available projections, ellipses, datums or units supported by the Proj.4 library when type is equal to proj, ellps, datum or units; when type equals have_datum_files a boolean is returned indicating whether datum files are installed and accessible (checking for conus).

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

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

Examples

p1 = st_point(c(7,52)) p2 = st_point(c(-30,20)) sfc = st_sfc(p1, p2, crs = 4326) sfc
#> Geometry set for 2 features #> geometry type: POINT #> dimension: XY #> bbox: xmin: -30 ymin: 20 xmax: 7 ymax: 52 #> epsg (SRID): 4326 #> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> POINT (7 52)
#> POINT (-30 20)
st_transform(sfc, 3857)
#> Geometry set for 2 features #> geometry type: POINT #> dimension: XY #> bbox: xmin: -3339585 ymin: 2273031 xmax: 779236.4 ymax: 6800125 #> epsg (SRID): 3857 #> proj4string: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
#> POINT (779236.4 6800125)
#> POINT (-3339585 2273031)
st_transform(st_sf(a=2:1, geom=sfc), "+init=epsg:3857")
#> Simple feature collection with 2 features and 1 field #> geometry type: POINT #> dimension: XY #> bbox: xmin: -3339585 ymin: 2273031 xmax: 779236.4 ymax: 6800125 #> epsg (SRID): 3857 #> proj4string: +proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +nadgrids=@null +units=m +no_defs #> a geom #> 1 2 POINT (779236.4 6800125) #> 2 1 POINT (-3339585 2273031)
nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `/home/travis/build/r-spatial/sf/inst/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 #> epsg (SRID): 4267 #> proj4string: +proj=longlat +datum=NAD27 +no_defs
st_area(nc[1,]) # area from long/lat
#> 1137394863 [m^2]
st_area(st_transform(nc[1,], 32119)) # NC state plane, m
#> 1137598162 [m^2]
st_area(st_transform(nc[1,], 2264)) # NC state plane, US foot
#> 12244955726 [US_survey_foot^2]
library(units)
#> udunits system database from /usr/share/xml/udunits
set_units(st_area(st_transform(nc[1,], 2264)), m^2)
#> 1137598162 [m^2]
st_transform(structure(p1, proj4string = "+init=epsg:4326"), "+init=epsg:3857")
#> POINT (779236.4 6800125)
st_proj_info("datum")
#> name ellipse #> 1 WGS84 WGS84 #> 2 GGRS87 GRS80 #> 3 NAD83 GRS80 #> 4 NAD27 clrk66 #> 5 potsdam bessel #> 6 carthage clark80 #> 7 hermannskogel bessel #> 8 ire65 mod_airy #> 9 nzgd49 intl #> 10 OSGB36 airy #> definition #> 1 towgs84=0,0,0 #> 2 towgs84=-199.87,74.79,246.62 #> 3 towgs84=0,0,0 #> 4 nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat #> 5 towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 #> 6 towgs84=-263.0,6.0,431.0 #> 7 towgs84=653.0,-212.0,449.0 #> 8 towgs84=482.530,-130.596,564.557,-1.042,-0.214,-0.631,8.15 #> 9 towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 #> 10 towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 #> description #> 1 #> 2 Greek_Geodetic_Reference_System_1987 #> 3 North_American_Datum_1983 #> 4 North_American_Datum_1927 #> 5 Potsdam Rauenberg 1950 DHDN #> 6 Carthage 1934 Tunisia #> 7 Hermannskogel #> 8 Ireland 1965 #> 9 New Zealand Geodetic Datum 1949 #> 10 Airy 1830
st_wrap_dateline(st_sfc(st_linestring(rbind(c(-179,0),c(179,0))), crs = 4326))
#> Geometry set for 1 feature #> geometry type: MULTILINESTRING #> dimension: XY #> bbox: xmin: -180 ymin: 0 xmax: 180 ymax: 0 #> epsg (SRID): 4326 #> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> MULTILINESTRING ((-179 0, -180 0), (180 0, 179 0))
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)
#> Warning: GDAL Error 1: IllegalArgumentException: Points of LinearRing do not form a closed linestring
#> Warning: GDAL Error 1: IllegalArgumentException: Points of LinearRing do not form a closed linestring
wrld_moll <- st_transform(wrld_wrap, "+proj=moll") plot(st_geometry(wrld_moll), col = "transparent")