From 6e13be9191ec202800205819c04b43e5942eb381 Mon Sep 17 00:00:00 2001 From: edzer Date: Wed, 31 Jul 2024 10:49:56 +0200 Subject: [PATCH] closes #2415 --- NAMESPACE | 1 + NEWS.md | 2 ++ R/transform.R | 8 ++++++++ man/st_transform.Rd | 7 +++++++ src/gdal.cpp | 6 ++++++ 5 files changed, 24 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index d0253d02c..be8f53fce 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -314,6 +314,7 @@ S3method(st_snap,sfg) S3method(st_sym_difference,sf) S3method(st_sym_difference,sfc) S3method(st_sym_difference,sfg) +S3method(st_transform,bbox) S3method(st_transform,sf) S3method(st_transform,sfc) S3method(st_transform,sfg) diff --git a/NEWS.md b/NEWS.md index f4089c6af..e4ddcdf7a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # version 1.0-17 +* add `st_transform()` method for `bbox` objects; this uses OGRCoordinateTransformation::TransformBounds(), densifying first and antemeridian proof; #2415 + * `st_filter.sf()` correctly scopes `x` and `y` arguments using !! operator; #2416 * `[.sfc` and `[<-.sfc` use matrix/array type subsetting for `sfc` objects that have a `dim` attribute diff --git a/R/transform.R b/R/transform.R index 2ef56f448..96103d7b8 100644 --- a/R/transform.R +++ b/R/transform.R @@ -165,6 +165,14 @@ st_transform.sfg = function(x, crs = st_crs(x), ...) { structure(st_transform(x, crs, ...)[[1]], crs = crs) } +#' @name st_transform +#' @export +#' @param densify integer, number of points for discretizing lines between bounding box corner points; see Details +#' @details the method for `bbox` objects densifies lines for geographic coordinates along Cartesian lines, not great circle arcs +st_transform.bbox = function(x, crs, ..., densify = 21) { + st_bbox(CPL_transform_bounds(x, st_crs(crs), densify), crs = crs) +} + #' @name st_transform #' @export st_wrap_dateline = function(x, options, quiet) UseMethod("st_wrap_dateline") diff --git a/man/st_transform.Rd b/man/st_transform.Rd index 6e764cd9e..e38fad030 100644 --- a/man/st_transform.Rd +++ b/man/st_transform.Rd @@ -6,6 +6,7 @@ \alias{st_transform.sfc} \alias{st_transform.sf} \alias{st_transform.sfg} +\alias{st_transform.bbox} \alias{st_wrap_dateline} \alias{st_wrap_dateline.sfc} \alias{st_wrap_dateline.sf} @@ -34,6 +35,8 @@ st_transform(x, crs, ...) \method{st_transform}{sfg}(x, crs = st_crs(x), ...) +\method{st_transform}{bbox}(x, crs, ..., densify = 21) + st_wrap_dateline(x, options, quiet) \method{st_wrap_dateline}{sfc}(x, options = "WRAPDATELINE=YES", quiet = TRUE) @@ -74,6 +77,8 @@ at least the one specified will be considered; a negative value disables this fe \item{check}{logical; if \code{TRUE}, perform a sanity check on resulting polygons} +\item{densify}{integer, number of points for discretizing lines between bounding box corner points; see Details} + \item{options}{character; should have "WRAPDATELINE=YES" to function; another parameter that is used is "DATELINEOFFSET=10" (where 10 is the default value)} \item{quiet}{logical; print options after they have been parsed?} @@ -99,6 +104,8 @@ because WKT1 does not store axis order unambiguously. The \code{st_transform} method for \code{sfg} objects assumes that the CRS of the object is available as an attribute of that name. +the method for \code{bbox} objects densifies lines for geographic coordinates along Cartesian lines, not great circle arcs + For a discussion of using \code{options}, see \url{https://github.com/r-spatial/sf/issues/280} and \url{https://github.com/r-spatial/sf/issues/1983} \code{sf_proj_info} lists the available projections, ellipses, datums, units, or data search path of the PROJ library when \code{type} is equal to proj, ellps, datum, units or path; when \code{type} equals \code{have_datum_files} a boolean is returned indicating whether datum files are installed and accessible (checking for \code{conus}). \code{path} returns the \code{PROJ_INFO.searchpath} field directly, as a single string with path separaters (\code{:} or \verb{;}). diff --git a/src/gdal.cpp b/src/gdal.cpp index e7ed751e4..2246c6d2f 100644 --- a/src/gdal.cpp +++ b/src/gdal.cpp @@ -698,6 +698,12 @@ Rcpp::NumericVector CPL_transform_bounds(Rcpp::NumericVector bb, Rcpp::List crs_ ret[1] = ymin; ret[2] = xmax; ret[3] = ymax; + Rcpp::CharacterVector names(4); + names(0) = "xmin"; + names(1) = "ymin"; + names(2) = "xmax"; + names(3) = "ymax"; + ret.attr("names") = names; ct->DestroyCT(ct); dst->Release(); src->Release();