Skip to content

Commit

Permalink
add CPL_transform_bounds()
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed May 6, 2024
1 parent a6e210e commit ccdd071
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ CPL_transform <- function(sfc, crs, AOI, pipeline, reverse = FALSE, desired_accu
.Call(`_sf_CPL_transform`, sfc, crs, AOI, pipeline, reverse, desired_accuracy, allow_ballpark)
}

CPL_transform_bounds <- function(bb, crs_dst, densify_pts = 21L) {
.Call(`_sf_CPL_transform_bounds`, bb, crs_dst, densify_pts)
}

CPL_wrap_dateline <- function(sfc, opt, quiet = TRUE) {
.Call(`_sf_CPL_wrap_dateline`, sfc, opt, quiet)
}
Expand Down
4 changes: 2 additions & 2 deletions inst/docker/gdal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN apt-get upgrade -y

RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update \
&& apt-get install -y \
devscripts \
gdb \
git \
libcairo2-dev \
Expand Down Expand Up @@ -100,7 +101,7 @@ RUN cd proj* \

# GDAL:
ENV GDAL_VERSION 3.9.0
ENV GDAL_VERSION_NAME 3.9.0beta1
ENV GDAL_VERSION_NAME 3.9.0rc1
#https://download.osgeo.org/gdal/3.9.0/gdal-3.9.0beta1.tar.gz

RUN wget -q http://download.osgeo.org/gdal/${GDAL_VERSION}/gdal-${GDAL_VERSION_NAME}.tar.gz \
Expand All @@ -122,7 +123,6 @@ RUN git clone --depth 10 https://github.com/r-spatial/stars.git

RUN R CMD build --no-build-vignettes --no-manual lwgeom
RUN R CMD build --no-build-vignettes --no-manual sf
RUN Rscript -e 'install.packages("viridis")'
RUN R CMD build --no-manual stars
# RUN pkg-config proj --modversion
RUN R CMD INSTALL sf
Expand Down
14 changes: 14 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_transform_bounds
Rcpp::NumericVector CPL_transform_bounds(Rcpp::NumericVector bb, Rcpp::List crs_dst, int densify_pts);
RcppExport SEXP _sf_CPL_transform_bounds(SEXP bbSEXP, SEXP crs_dstSEXP, SEXP densify_ptsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type bb(bbSEXP);
Rcpp::traits::input_parameter< Rcpp::List >::type crs_dst(crs_dstSEXP);
Rcpp::traits::input_parameter< int >::type densify_pts(densify_ptsSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_transform_bounds(bb, crs_dst, densify_pts));
return rcpp_result_gen;
END_RCPP
}
// CPL_wrap_dateline
Rcpp::List CPL_wrap_dateline(Rcpp::List sfc, Rcpp::CharacterVector opt, bool quiet);
RcppExport SEXP _sf_CPL_wrap_dateline(SEXP sfcSEXP, SEXP optSEXP, SEXP quietSEXP) {
Expand Down Expand Up @@ -1471,6 +1484,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_curve_to_linestring", (DL_FUNC) &_sf_CPL_curve_to_linestring, 1},
{"_sf_CPL_can_transform", (DL_FUNC) &_sf_CPL_can_transform, 2},
{"_sf_CPL_transform", (DL_FUNC) &_sf_CPL_transform, 7},
{"_sf_CPL_transform_bounds", (DL_FUNC) &_sf_CPL_transform_bounds, 3},
{"_sf_CPL_wrap_dateline", (DL_FUNC) &_sf_CPL_wrap_dateline, 3},
{"_sf_CPL_get_gdal_drivers", (DL_FUNC) &_sf_CPL_get_gdal_drivers, 1},
{"_sf_CPL_sfc_from_wkt", (DL_FUNC) &_sf_CPL_sfc_from_wkt, 1},
Expand Down
38 changes: 38 additions & 0 deletions src/gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,44 @@ Rcpp::List CPL_transform(Rcpp::List sfc, Rcpp::List crs,
return ret;
}

// [[Rcpp::export]]
Rcpp::NumericVector CPL_transform_bounds(Rcpp::NumericVector bb, Rcpp::List crs_dst,
int densify_pts = 21) {

#if GDAL_VERSION_NUM >= 3040000
if (bb.size() != 4)
Rcpp::stop("bb should have length 4");
Rcpp::List crs_src = bb.attr("crs");
OGRSpatialReference *src = OGRSrs_from_crs(crs_src);
OGRSpatialReference *dst = OGRSrs_from_crs(crs_dst);
if (src == NULL)
Rcpp::stop("crs_src not found: is it missing?"); // #nocov
if (dst == NULL)
Rcpp::stop("crs_dst not found: is it missing?"); // #nocov
OGRCoordinateTransformation *ct = OGRCreateCoordinateTransformation(src, dst);
if (ct == NULL) {
dst->Release(); // #nocov start
src->Release();
Rcpp::stop("transform_bounds(): transformation not available"); // #nocov end
}
double xmin, ymin, xmax, ymax;
int success = ct->TransformBounds(bb[0], bb[1], bb[2], bb[3], &xmin, &ymin, &xmax, &ymax, densify_pts);
if (!success)
Rcpp::stop("transform_bounds(): failures encountered"); // #nocov end
Rcpp::NumericVector ret(4);
ret[0] = xmin;
ret[1] = ymin;
ret[2] = xmax;
ret[3] = ymax;
ct->DestroyCT(ct);
dst->Release();
src->Release();
#else
Rcpp::stop("transform_bounds() requires GDAL >= 3.4");
#endif
return ret;
}

// [[Rcpp::export]]
Rcpp::List CPL_wrap_dateline(Rcpp::List sfc, Rcpp::CharacterVector opt, bool quiet = true) {

Expand Down

0 comments on commit ccdd071

Please sign in to comment.