Skip to contents

bbox_transform() is a convenience function to transform the coordinates of a boundary from their current spatial reference system to a new target spatial reference system.

Usage

bbox_transform(bbox, srs_from, srs_to, use_transform_bounds = TRUE)

Arguments

bbox

Numeric vector of length four containing a bounding box (xmin, ymin, xmax, ymax) to transform.

srs_from

Character string specifying the spatial reference system for pts. May be in WKT format or any of the formats supported by srs_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().

use_transform_bounds

Logical value, TRUE to use transform_bounds() (the default, requires GDAL >= 3.4). If FALSE, transformation is done with g_transform().

Value

Numeric vector of length four containing a transformed bounding box (xmin, ymin, xmax, ymax).

Details

With use_transform_bounds = TRUE (the default) this function returns:

# requires GDAL >= 3.4
transform_bounds(bbox, srs_from, srs_to)

With use_transform_bounds = FALSE, this function returns:

bbox_to_wkt(bbox) |>
  g_transform(srs_from, srs_to) |>
  bbox_from_wkt()

Examples

bb <- c(-1405880.72, -1371213.76, 5405880.72, 5371213.76)

# the default assumes GDAL >= 3.4
if (as.integer(gdal_version()[2]) >= 3040000) {
  bb_wgs84 <- bbox_transform(bb, "EPSG:32661", "EPSG:4326")
} else {
  bb_wgs84 <- bbox_transform(bb, "EPSG:32661", "EPSG:4326",
                             use_transform_bounds = FALSE)
}

print(bb_wgs84)
#> [1] -180.00000   48.65641  180.00000   90.00000