Skip to contents

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(
  wkt,
  srs_from,
  srs_to,
  wrap_date_line = FALSE,
  date_line_offset = 10L
)

Arguments

wkt

Character. OGC WKT string for a simple feature geometry.

srs_from

Character string in OGC WKT format specifying the spatial reference system for the geometry given by wkt.

srs_to

Character string in OGC WKT format specifying the target spatial reference system.

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 to FALSE.

date_line_offset

Integer scalar. Longitude gap in degree. Defaults to 10.

Value

Character string for a transformed OGC WKT geometry.

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.

See also

Examples

elev_file <- system.file("extdata/storml_elev.tif", package="gdalraster")
ds <- new(GDALRaster, elev_file)

# the convenience function bbox_transform() does this:
bbox_to_wkt(ds$bbox()) |>
  g_transform(ds$getProjection(), epsg_to_wkt(4326)) |>
  bbox_from_wkt()
#> [1] -113.28289   46.04764 -113.22629   46.07760

ds$close()

# correct geometries that incorrectly go from a longitude on a side of the
# antimeridian to the other side
geom <- "LINESTRING (-179 0,179 0)"
srs <- epsg_to_wkt(4326)
g_transform(geom, srs, srs, wrap_date_line = TRUE)
#> [1] "MULTILINESTRING ((-179 0,-180 0),(180 0,179 0))"