Skip to content

Commit

Permalink
options for vrt
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Jul 20, 2023
1 parent 2d00c29 commit 35139e0
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: vapour
Title: Access to the 'Geospatial Data Abstraction Library' ('GDAL')
Version: 0.9.5.9004
Version: 0.9.5.9005
Authors@R: c(person("Michael", "Sumner", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2471-7511")),
person("Simon", "Wotherspoon", role = "ctb", comment = "RasterIO configuration for resampling options"),
person("Mark", "Padgham", role = "ctb", comment = "helped get started :)"),
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# vapour dev

* `vapour_vrt()` gains 'options' argument, so we can in particular do `options = c("-expand", "rgb", "-ot", "Byte")` to warp
16-bit integer colour palettes from GTiff to PNG. :)

* `gdal_raster_dsn()` can now write to non-GTiff/COG via `options = c("-of", DRIVER)`.

* New function `vapour_geolocation()` retrieves any existing geolocation information.

* `vapour_vrt()` called with geolocation arrays now scrubs the geotransform from the output (which ensures the
warper uses the arrays without setting '-geoloc').
warper uses the arrays without setting '-geoloc').

* `vapour_create()` gets creation options, data type options, and driver options.

Expand Down
13 changes: 8 additions & 5 deletions R/00_warpgeneral.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ gdal_raster_data <- function(dsn, target_crs = NULL, target_dim = NULL, target_e
#' @name gdal_raster_data
#' @export
gdal_raster_dsn <- function(dsn, target_crs = NULL, target_dim = NULL, target_ext = NULL, target_res = NULL,
resample = "near", bands = NULL, band_output_type = NULL, options = character(), out_dsn = tempfile(fileext = ".tif")) {
resample = "near", bands = NULL, band_output_type = NULL, options = character(), out_dsn = tempfile(fileext = ".tif"), include_meta = TRUE) {

if (is.null(target_crs)) target_crs <- ""
if (is.null(target_ext)) {
Expand Down Expand Up @@ -133,15 +133,18 @@ gdal_raster_dsn <- function(dsn, target_crs = NULL, target_dim = NULL, target_ex
if (is.null(band_output_type)) band_output_type <- "Float64"
#if (grepl("tif$", out_dsn)) {
## we'll have to do some work here
## currently always COG
options <- c(options, "-of", "COG")

if (!any(options == "-of")) {
## currently always COG
options <- c(options, "-of", "COG")

}
if (!is.null(bands) || (is.integer(bands) && !length(bands) == 1 && bands[1] > 0)) {
stop("bands cannot be set for gdal_raster_dsn, please use an upfront call to 'vapour_vrt(dsn, bands = )' to create the dsn")
} else {
bands <- -1
}

include_meta <- isTRUE(include_meta)
warp_general_cpp(dsn, target_crs,
target_ext,
target_dim,
Expand All @@ -150,7 +153,7 @@ gdal_raster_dsn <- function(dsn, target_crs = NULL, target_dim = NULL, target_ex
resample = resample,
silent = FALSE, band_output_type = band_output_type,
options = options,
dsn_outname = out_dsn[1L])
dsn_outname = out_dsn[1L], include_meta = include_meta)
}

#' @name gdal_raster_data
Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ raster_gdalinfo_app_cpp <- function(dsn, options) {
.Call('_vapour_raster_gdalinfo_app_cpp', PACKAGE = 'vapour', dsn, options)
}

raster_vrt_cpp <- function(dsn, extent, projection, sds, bands, geolocation, nomd, overview) {
.Call('_vapour_raster_vrt_cpp', PACKAGE = 'vapour', dsn, extent, projection, sds, bands, geolocation, nomd, overview)
raster_vrt_cpp <- function(dsn, extent, projection, sds, bands, geolocation, nomd, overview, options) {
.Call('_vapour_raster_vrt_cpp', PACKAGE = 'vapour', dsn, extent, projection, sds, bands, geolocation, nomd, overview, options)
}

raster_warp_file_cpp <- function(source_filename, target_crs, target_extent, target_dim, target_filename, bands, resample, silent, band_output_type, warp_options, transformation_options) {
Expand Down
5 changes: 3 additions & 2 deletions R/vapour_vrt.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
#'
#' vapour_vrt(tif, bands = c(1, 1))
#'
vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = NULL, geolocation = NULL, ..., relative_to_vrt = FALSE, nomd = FALSE, overview = -1L) {
vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = NULL, geolocation = NULL, ...,
relative_to_vrt = FALSE, nomd = FALSE, overview = -1L, options = character()) {
x <- .check_dsn_multiple_naok(x)

if (!relative_to_vrt) {
Expand Down Expand Up @@ -136,7 +137,7 @@ vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = N
overview <- as.integer(overview[1])
if (is.na(overview)) overview <- -1L

out <- raster_vrt_cpp(x, extent, projection[1L], sds, bands, geolocation, nomd, overview)
out <- raster_vrt_cpp(x, extent, projection[1L], sds, bands, geolocation, nomd, overview, options)
## scrub any transform, because of #210
if (nzchar(geolocation[1L])) {
out <- gsub("<GeoTransform.*GeoTransform>", "", out)
Expand Down
18 changes: 11 additions & 7 deletions inst/include/gdalraster/gdalraster.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ inline GDALDatasetH gdalH_open_avrt(const char* dsn,
NumericVector extent,
CharacterVector projection,
IntegerVector sds, IntegerVector bands, CharacterVector geolocation,
IntegerVector overview) {
IntegerVector overview,
CharacterVector options) {

CPLStringList translate_argv;
translate_argv.AddString("-of");
Expand Down Expand Up @@ -185,7 +186,9 @@ inline GDALDatasetH gdalH_open_avrt(const char* dsn,
}
}


for (int iopt = 0; iopt < options.size(); iopt++) {
translate_argv.AddString(options[iopt]);
}
GDALTranslateOptions* psTransOptions = GDALTranslateOptionsNew(translate_argv.List(), nullptr);

GDALDatasetH a_DS = GDALTranslate("", oDS, psTransOptions, nullptr);
Expand All @@ -204,12 +207,12 @@ inline GDALDatasetH* gdalH_open_multiple(CharacterVector dsn, IntegerVector sds)
return poHDS;
}
inline GDALDatasetH* gdalH_open_avrt_multiple(CharacterVector dsn, NumericVector extent,
CharacterVector projection, IntegerVector sds, IntegerVector bands) {
CharacterVector projection, IntegerVector sds, IntegerVector bands, CharacterVector options) {

GDALDatasetH* poHDS;
// whoever calls this will have to CPLFree() this
poHDS = static_cast<GDALDatasetH *>(CPLMalloc(sizeof(GDALDatasetH) * static_cast<size_t>(dsn.size())));
for (int i = 0; i < dsn.size(); i++) poHDS[i] = gdalH_open_avrt(dsn[i], extent, projection, sds, bands, "", -1);
for (int i = 0; i < dsn.size(); i++) poHDS[i] = gdalH_open_avrt(dsn[i], extent, projection, sds, bands, "", -1, options);
return poHDS;
}
// convert an opened GDALDataset to chunk-of-text VRT, if it is VRT you get it direct
Expand Down Expand Up @@ -245,12 +248,13 @@ inline const char* gdal_vrt_text(GDALDataset* poSrcDS, LogicalVector nomd) {
inline CharacterVector gdal_dsn_vrt(CharacterVector dsn, NumericVector extent, CharacterVector projection,
IntegerVector sds, IntegerVector bands,
CharacterVector geolocation, LogicalVector nomd,
IntegerVector overview) {
IntegerVector overview, CharacterVector options) {

CharacterVector out(dsn.size());
GDALDatasetH DS;
for (int i = 0; i < out.size(); i++) {
if (extent.size() == 4 || (!projection[0].empty()) || bands[0] > 0 || (!geolocation[0].empty() ) || sds[0] > 1 || overview[0] > -1) {
DS = gdalH_open_avrt(dsn[i], extent, projection, sds, bands, geolocation, overview);
if (extent.size() == 4 || (!projection[0].empty()) || bands[0] > 0 || (!geolocation[0].empty() ) || sds[0] > 1 || overview[0] > -1 || options.size() > 0) {
DS = gdalH_open_avrt(dsn[i], extent, projection, sds, bands, geolocation, overview, options);
} else {
DS = gdalH_open_dsn(dsn[i], sds);
}
Expand Down
15 changes: 12 additions & 3 deletions inst/include/gdalwarpgeneral/gdalwarpgeneral.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ inline List gdal_warp_general(CharacterVector dsn,
papszArg = CSLAddString(papszArg, "MEM");

} else {
papszArg = CSLAddString(papszArg, "-of");
papszArg = CSLAddString(papszArg, "GTiff");

// no need for this because GTiff is the default (we don't get auto-choose)
// bool do_format = true;
// // first check if user requests a format
// for (int iopt = 0; iopt < options.size(); iopt++) {
// if (EQUAL(options[iopt], "-of")) {
// do_format = false;
// }
// }
// if (do_format) {
// papszArg = CSLAddString(papszArg, "-of");
// papszArg = CSLAddString(papszArg, "GTiff");
// }
write_dsn = true;
}

Expand Down
3 changes: 2 additions & 1 deletion inst/include/gdalwarpmem/gdalwarpmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ inline List gdal_warp_in_memory(CharacterVector source_filename,
bool augment;
augment = !source_WKT[0].empty() || source_extent.size() == 4;

CharacterVector emptyoptions(0);

for (int i = 0; i < source_filename.size(); i++) {
if (augment) {
// not dealing with subdatasets here atm
// not dealing with source bands here, bands applies at read beloew
poSrcDS[i] = gdalraster::gdalH_open_avrt(source_filename[i], source_extent, source_WKT, 0, 0, "", overview);
poSrcDS[i] = gdalraster::gdalH_open_avrt(source_filename[i], source_extent, source_WKT, 0, 0, "", overview, emptyoptions);
} else {
poSrcDS[i] = gdalraster::gdalH_open_dsn(source_filename[i], 0);

Expand Down
3 changes: 2 additions & 1 deletion man/gdal_raster_data.Rd

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

3 changes: 2 additions & 1 deletion man/vapour_vrt.Rd

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

4 changes: 2 additions & 2 deletions src/00_vrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ using namespace Rcpp;
CharacterVector raster_vrt_cpp(CharacterVector dsn,
NumericVector extent,
CharacterVector projection,
IntegerVector sds, IntegerVector bands, CharacterVector geolocation, LogicalVector nomd, IntegerVector overview) {
return gdalraster::gdal_dsn_vrt(dsn, extent, projection, sds, bands, geolocation, nomd, overview);
IntegerVector sds, IntegerVector bands, CharacterVector geolocation, LogicalVector nomd, IntegerVector overview, CharacterVector options) {
return gdalraster::gdal_dsn_vrt(dsn, extent, projection, sds, bands, geolocation, nomd, overview, options);
}


9 changes: 5 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ BEGIN_RCPP
END_RCPP
}
// raster_vrt_cpp
CharacterVector raster_vrt_cpp(CharacterVector dsn, NumericVector extent, CharacterVector projection, IntegerVector sds, IntegerVector bands, CharacterVector geolocation, LogicalVector nomd, IntegerVector overview);
RcppExport SEXP _vapour_raster_vrt_cpp(SEXP dsnSEXP, SEXP extentSEXP, SEXP projectionSEXP, SEXP sdsSEXP, SEXP bandsSEXP, SEXP geolocationSEXP, SEXP nomdSEXP, SEXP overviewSEXP) {
CharacterVector raster_vrt_cpp(CharacterVector dsn, NumericVector extent, CharacterVector projection, IntegerVector sds, IntegerVector bands, CharacterVector geolocation, LogicalVector nomd, IntegerVector overview, CharacterVector options);
RcppExport SEXP _vapour_raster_vrt_cpp(SEXP dsnSEXP, SEXP extentSEXP, SEXP projectionSEXP, SEXP sdsSEXP, SEXP bandsSEXP, SEXP geolocationSEXP, SEXP nomdSEXP, SEXP overviewSEXP, SEXP optionsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Expand All @@ -732,7 +732,8 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< CharacterVector >::type geolocation(geolocationSEXP);
Rcpp::traits::input_parameter< LogicalVector >::type nomd(nomdSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type overview(overviewSEXP);
rcpp_result_gen = Rcpp::wrap(raster_vrt_cpp(dsn, extent, projection, sds, bands, geolocation, nomd, overview));
Rcpp::traits::input_parameter< CharacterVector >::type options(optionsSEXP);
rcpp_result_gen = Rcpp::wrap(raster_vrt_cpp(dsn, extent, projection, sds, bands, geolocation, nomd, overview, options));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -810,7 +811,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_vapour_blocks_cpp1", (DL_FUNC) &_vapour_blocks_cpp1, 3},
{"_vapour_blocks_cpp", (DL_FUNC) &_vapour_blocks_cpp, 3},
{"_vapour_raster_gdalinfo_app_cpp", (DL_FUNC) &_vapour_raster_gdalinfo_app_cpp, 2},
{"_vapour_raster_vrt_cpp", (DL_FUNC) &_vapour_raster_vrt_cpp, 8},
{"_vapour_raster_vrt_cpp", (DL_FUNC) &_vapour_raster_vrt_cpp, 9},
{"_vapour_raster_warp_file_cpp", (DL_FUNC) &_vapour_raster_warp_file_cpp, 11},
{NULL, NULL, 0}
};
Expand Down

0 comments on commit 35139e0

Please sign in to comment.