Skip to content

Commit

Permalink
Merge pull request #2305 from goergen95/gdalfootprint
Browse files Browse the repository at this point in the history
Add gdal_footprint support to gdal_utils
  • Loading branch information
edzer authored Jan 2, 2024
2 parents c880270 + 517f8fd commit 26ebfea
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* add `st_line_interpolate()` to obtain a point at a certain distance along a line; #2291

* add support for the GDAL footprint utility (requiring GDAL >=3.8.0) to gdal_utils; #2305

# version 1.0-15

* add `st_perimeter()` to cover both geographic and projected coordinates; #268, #2279, by @JosiahParry
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ CPL_gdaltranslate <- function(src, dst, options, oo, co, quiet = TRUE) {
.Call(`_sf_CPL_gdaltranslate`, src, dst, options, oo, co, quiet)
}

CPL_gdalfootprint <- function(src, dst, options, oo, co, quiet = TRUE) {
.Call(`_sf_CPL_gdalfootprint`, src, dst, options, oo, co, quiet)
}

CPL_gdalvectortranslate <- function(src, dst, options, oo, doo, co, quiet = TRUE) {
.Call(`_sf_CPL_gdalvectortranslate`, src, dst, options, oo, doo, co, quiet)
}
Expand Down
3 changes: 2 additions & 1 deletion R/gdal_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resampling_method = function(option = "near") {

#' Native interface to gdal utils
#' @name gdal_utils
#' @param util character; one of \code{info}, \code{warp}, \code{rasterize}, \code{translate}, \code{vectortranslate} (for ogr2ogr), \code{buildvrt}, \code{demprocessing}, \code{nearblack}, \code{grid}, \code{mdiminfo} and \code{mdimtranslate} (the last two requiring GDAL 3.1), \code{ogrinfo} (requiring GDAL 3.7)
#' @param util character; one of \code{info}, \code{warp}, \code{rasterize}, \code{translate}, \code{vectortranslate} (for ogr2ogr), \code{buildvrt}, \code{demprocessing}, \code{nearblack}, \code{grid}, \code{mdiminfo} and \code{mdimtranslate} (the last two requiring GDAL 3.1), \code{ogrinfo} (requiring GDAL 3.7), \code{footprint} (requiring GDAL 3.8)
#' @param source character; name of input layer(s); for \code{warp}, \code{buidvrt} or \code{mdimtranslate} this can be more than one
#' @param destination character; name of output layer
#' @param options character; options for the utility
Expand Down Expand Up @@ -112,6 +112,7 @@ gdal_utils = function(util = "info", source, destination, options = character(0)
"-te", "-tr", "-tap", "-ts", "-ot")) # https://gdal.org/programs/gdal_rasterize.html
CPL_gdalrasterize(source, destination, options, oo, doo, config_options, overwrite, quiet)
}, # nocov end
footprint = CPL_gdalfootprint(source, destination, options, oo, config_options, quiet),
translate = CPL_gdaltranslate(source, destination, options, oo, config_options, quiet),
vectortranslate = CPL_gdalvectortranslate(source, destination, options, oo, doo, config_options, quiet),
buildvrt = CPL_gdalbuildvrt(if (missing(source)) character(0) else source, destination, options, oo, config_options, quiet),
Expand Down
2 changes: 1 addition & 1 deletion man/gdal_utils.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_gdalfootprint
Rcpp::LogicalVector CPL_gdalfootprint(Rcpp::CharacterVector src, Rcpp::CharacterVector dst, Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector co, bool quiet);
RcppExport SEXP _sf_CPL_gdalfootprint(SEXP srcSEXP, SEXP dstSEXP, SEXP optionsSEXP, SEXP ooSEXP, SEXP coSEXP, SEXP quietSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type src(srcSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type dst(dstSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type options(optionsSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type oo(ooSEXP);
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type co(coSEXP);
Rcpp::traits::input_parameter< bool >::type quiet(quietSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_gdalfootprint(src, dst, options, oo, co, quiet));
return rcpp_result_gen;
END_RCPP
}
// CPL_gdalvectortranslate
Rcpp::LogicalVector CPL_gdalvectortranslate(Rcpp::CharacterVector src, Rcpp::CharacterVector dst, Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector doo, Rcpp::CharacterVector co, bool quiet);
RcppExport SEXP _sf_CPL_gdalvectortranslate(SEXP srcSEXP, SEXP dstSEXP, SEXP optionsSEXP, SEXP ooSEXP, SEXP dooSEXP, SEXP coSEXP, SEXP quietSEXP) {
Expand Down Expand Up @@ -1356,7 +1372,7 @@ RcppExport SEXP _sf_CPL_read_wkb(SEXP wkb_listSEXP, SEXP EWKBSEXP, SEXP spatiali
if (rcpp_isError_gen) {
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
UNPROTECT(1);
Rf_error("%s", CHAR(rcpp_msgSEXP_gen));
Rf_error(CHAR(rcpp_msgSEXP_gen));
}
UNPROTECT(1);
return rcpp_result_gen;
Expand Down Expand Up @@ -1391,7 +1407,7 @@ RcppExport SEXP _sf_CPL_write_wkb(SEXP sfcSEXP, SEXP EWKBSEXP) {
if (rcpp_isError_gen) {
SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen);
UNPROTECT(1);
Rf_error("%s", CHAR(rcpp_msgSEXP_gen));
Rf_error(CHAR(rcpp_msgSEXP_gen));
}
UNPROTECT(1);
return rcpp_result_gen;
Expand Down Expand Up @@ -1473,6 +1489,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_gdalwarp", (DL_FUNC) &_sf_CPL_gdalwarp, 8},
{"_sf_CPL_gdalrasterize", (DL_FUNC) &_sf_CPL_gdalrasterize, 8},
{"_sf_CPL_gdaltranslate", (DL_FUNC) &_sf_CPL_gdaltranslate, 6},
{"_sf_CPL_gdalfootprint", (DL_FUNC) &_sf_CPL_gdalfootprint, 6},
{"_sf_CPL_gdalvectortranslate", (DL_FUNC) &_sf_CPL_gdalvectortranslate, 7},
{"_sf_CPL_gdalbuildvrt", (DL_FUNC) &_sf_CPL_gdalbuildvrt, 6},
{"_sf_CPL_gdaldemprocessing", (DL_FUNC) &_sf_CPL_gdaldemprocessing, 8},
Expand Down
36 changes: 36 additions & 0 deletions src/gdal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,42 @@ Rcpp::LogicalVector CPL_gdaltranslate(Rcpp::CharacterVector src, Rcpp::Character
return result == NULL || err;
}


// [[Rcpp::export]]
Rcpp::LogicalVector CPL_gdalfootprint(Rcpp::CharacterVector src, Rcpp::CharacterVector dst,
Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector co,
bool quiet = true) {

#if GDAL_VERSION_NUM < 3080000
Rcpp::stop("footprint util requires GDAL >= 3.8.0");
#else
int err = 0;
set_config_options(co);
std::vector <char *> options_char = create_options(options, true);
std::vector <char *> oo_char = create_options(oo, true);
GDALFootprintOptions* opt = GDALFootprintOptionsNew(options_char.data(), NULL);

if (opt == NULL)
Rcpp::stop("footprint: options error");

if (! quiet)
GDALFootprintOptionsSetProgress(opt, GDALRProgress, NULL);
GDALDatasetH src_pt = GDALOpenEx((const char *) src[0], GDAL_OF_RASTER | GA_ReadOnly,
NULL, oo_char.data(), NULL);
if (src_pt == NULL)
return 1; // #nocov
GDALDatasetH result = GDALFootprint((const char *) dst[0], NULL,src_pt, opt, &err);
GDALFootprintOptionsFree(opt);
// see https://github.com/r-spatial/sf/issues/1352:
if (result != NULL)
GDALClose(result);
if (src_pt != NULL)
GDALClose(src_pt);
unset_config_options(co);
return result == NULL || err;
#endif
}

// [[Rcpp::export]]
Rcpp::LogicalVector CPL_gdalvectortranslate(Rcpp::CharacterVector src, Rcpp::CharacterVector dst,
Rcpp::CharacterVector options, Rcpp::CharacterVector oo, Rcpp::CharacterVector doo,
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test_gdal.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ test_that('gdal_utils work', {
expect_warning(gdal_utils("buildvrt", sd2, tf3, c("-oo", "FOO=BAR"))) # fake opening options
expect_error(gdal_utils("buildvrt", "foo.tif", tf3, c("-oo", "FOO=BAR")), "cannot open source dataset")
expect_true(gdal_utils("demprocessing", sd2, tf, processing = "hillshade"))
# check gdalfootprint
skip_if_not(sf_extSoftVersion()[["GDAL"]] >= "3.8.0")
tif <- system.file("tif/geomatrix.tif", package="sf")
tf4 <- tempfile(fileext = ".gpkg")
expect_true(gdal_utils("footprint", tif, tf4))
})

# gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -overwrite NETCDF:avhrr-only-v2.19810901.nc:anom utm11.tif
Expand Down

0 comments on commit 26ebfea

Please sign in to comment.