g_transform()
will transform the coordinates of a geometry from their
current spatial reference system to a new target spatial reference system.
Normally this means reprojecting the vectors, but it could include datum
shifts, and changes of units.
Usage
g_transform(
geom,
srs_from,
srs_to,
wrap_date_line = FALSE,
date_line_offset = 10L,
as_wkb = TRUE,
as_iso = FALSE,
byte_order = "LSB",
quiet = FALSE
)
Arguments
- geom
Either a raw vector of WKB or list of raw vectors, or a character vector containing one or more WKT strings.
- srs_from
Character string specifying the spatial reference system for
pts
. May be in WKT format or any of the formats supported bysrs_to_wkt()
.- srs_to
Character string specifying the output spatial reference system. May be in WKT format or any of the formats supported by
srs_to_wkt()
.- wrap_date_line
Logical scalar.
TRUE
to correct geometries that incorrectly go from a longitude on a side of the antimeridian to the other side. Defaults toFALSE
.- date_line_offset
Integer scalar. Longitude gap in degree. Defaults to
10
.- as_wkb
Logical,
TRUE
to return the output geometry in WKB format (the default), orFALSE
to return as WKT.- as_iso
Logical,
TRUE
to export as ISO WKB/WKT (ISO 13249 SQL/MM Part 3), orFALSE
(the default) to export as "Extended WKB/WKT".- byte_order
Character string specifying the byte order when output is WKB. One of
"LSB"
(the default) or"MSB"
(uncommon).- quiet
Logical,
TRUE
to suppress warnings. Defaults toFALSE
.
Value
A geometry as WKB raw vector or WKT string, or a list/character vector of
geometries as WKB/WKT with length equal to the number of input geometries.
NA
is returned with a warning if an input geometry cannot be converted
into an OGR geometry object, or if an error occurs in the call to the
underlying OGR API.
Note
This function uses the OGR_GeomTransformer_Create()
and
OGR_GeomTransformer_Transform()
functions in the GDAL API: "This is an
enhanced version of OGR_G_Transform()
. When reprojecting geometries from
a Polar Stereographic projection or a projection naturally crossing the
antimeridian (like UTM Zone 60) to a geographic CRS, it will cut geometries
along the antimeridian. So a LineString
might be returned as a
MultiLineString
."
The wrap_date_line = TRUE
option might be specified for circumstances to
correct geometries that incorrectly go from a longitude on a side of the
antimeridian to the other side, e.g., LINESTRING (-179 0,179 0)
will be
transformed to MULTILINESTRING ((-179 0,-180 0),(180 0,179 0))
. For that
use case, srs_to
might be the same as srs_from
.
Examples
pt <- "POINT (-114.0 47.0)"
g_transform(pt, "WGS84", "EPSG:5070", as_wkb = FALSE)
#> [1] "POINT (-1367125.77866242 2797551.11860518)"
# correct geometries that incorrectly go from a longitude on a side of the
# antimeridian to the other side
geom <- "LINESTRING (-179 0,179 0)"
g_transform(geom, "WGS84", "WGS84", wrap_date_line = TRUE, as_wkb = FALSE)
#> [1] "MULTILINESTRING ((-179 0,-180 0),(180 0,179 0))"