Skip to content

Commit

Permalink
avoid GDAL error handler issues when sf package is loaded (see #48)
Browse files Browse the repository at this point in the history
  • Loading branch information
appelmar committed Feb 7, 2022
1 parent 5bcb483 commit e79f4ed
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 73 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export(gdalcubes_version)
export(image_collection)
export(image_mask)
export(join_bands)
export(json_cube)
export(memsize)
export(nbands)
export(ncdf_cube)
Expand Down
16 changes: 8 additions & 8 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ libgdalcubes_copy_cube <- function(pin) {
.Call('_gdalcubes_libgdalcubes_copy_cube', PACKAGE = 'gdalcubes', pin)
}

libgdalcubes_from_json_file <- function(path) {
.Call('_gdalcubes_libgdalcubes_from_json_file', PACKAGE = 'gdalcubes', path)
}

libgdalcubes_from_json_string <- function(json) {
.Call('_gdalcubes_libgdalcubes_from_json_string', PACKAGE = 'gdalcubes', json)
}

libgdalcubes_create_rename_bands_cube <- function(pin, names_old, names_new) {
.Call('_gdalcubes_libgdalcubes_create_rename_bands_cube', PACKAGE = 'gdalcubes', pin, names_old, names_new)
}
Expand Down Expand Up @@ -233,14 +241,6 @@ libgdalcubes_set_use_overviews <- function(use_overviews) {
invisible(.Call('_gdalcubes_libgdalcubes_set_use_overviews', PACKAGE = 'gdalcubes', use_overviews))
}

libgdalcubes_translate_cog <- function(collection, out_dir, nthreads, overwrite, creation_options) {
.Call('_gdalcubes_libgdalcubes_translate_cog', PACKAGE = 'gdalcubes', collection, out_dir, nthreads, overwrite, creation_options)
}

libgdalcubes_translate_gtiff <- function(collection, out_dir, nthreads, overwrite, creation_options) {
.Call('_gdalcubes_libgdalcubes_translate_gtiff', PACKAGE = 'gdalcubes', collection, out_dir, nthreads, overwrite, creation_options)
}

libgdalcubes_simple_hash <- function(instr) {
.Call('_gdalcubes_libgdalcubes_simple_hash', PACKAGE = 'gdalcubes', instr)
}
Expand Down
58 changes: 57 additions & 1 deletion R/cube.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ stack_cube <- function(x, datetime_values, bands = NULL, band_names = NULL, chun
#' Copy a data cube proxy object without copying any data
#'
#' @param cube source data cube proxy object
#' @return copied data proxy object
#' @return copied data cube proxy object
#' @details
#' This internal function copies the complete processing chain / graph of a data cube but does not copy any data
#' It is used internally to avoid in-place modification for operations with potential side effects on source data cubes.
Expand All @@ -179,6 +179,62 @@ stack_cube <- function(x, datetime_values, bands = NULL, band_names = NULL, chun
return(cube)
}

#' Read a data cube from a json description file
#'
#' @param json length-one character vector with a valid json data cube description
#' @param path source data cube proxy object
#' @return data cube proxy object
#' @details
#' Data cubes can be stored as JSON description files. These files do not store any data but the recipe
#' how a data cube is consructed, i.e., the chain (or graph) of processes involved.
#'
#' Since data cube objects (as returned from \code{\link{raster_cube}}) cannot be saved with normal R methods,
#' the combination of \code{\link{as_json}} and \code{\link{json_cube}} provides a cheap way to save data cube
#' objects across several R sessions, as in the examples.
#'
#' @examples{
#' # create image collection from example Landsat data only
#' # if not already done in other examples
#' if (!file.exists(file.path(tempdir(), "L8.db"))) {
#' L8_files <- list.files(system.file("L8NY18", package = "gdalcubes"),
#' ".TIF", recursive = TRUE, full.names = TRUE)
#' create_image_collection(L8_files, "L8_L1TP", file.path(tempdir(), "L8.db"))
#' }
#'
#' L8.col = image_collection(file.path(tempdir(), "L8.db"))
#' v = cube_view(extent=list(left=388941.2, right=766552.4,
#' bottom=4345299, top=4744931, t0="2018-01", t1="2018-12"),
#' srs="EPSG:32618", nx = 497, ny=526, dt="P1M")
#' cube = raster_cube(L8.col, v)
#'
#' # save
#' fname = tempfile()
#' writeLines(as_json(cube), fname)
#'
#' # load
#' json_cube(path = fname)
#' }
#'
#' @export
json_cube <- function(json, path = NULL) {
if (!missing(json)) {
if (!is.null(path)) {
warning("Expected only one of arguments 'json' and 'path'; path will be ignored")
}
cube = libgdalcubes_from_json_string(json)
}
else {
if (!is.null(path)) {
cube = libgdalcubes_from_json_file(path)
}
else {
stop("Missing argument, please provide either a JSON string, or a path to a JSON file")
}
}
class(cube) <- "cube" # TODO: any way to derive exact cube type here?
return(cube)
}




Expand Down
2 changes: 1 addition & 1 deletion man/dot-copy_cube.Rd

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

18 changes: 18 additions & 0 deletions man/gdalcubes_set_gdal_config.Rd

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

52 changes: 52 additions & 0 deletions man/json_cube.Rd

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

56 changes: 24 additions & 32 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,28 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// libgdalcubes_from_json_file
SEXP libgdalcubes_from_json_file(std::string path);
RcppExport SEXP _gdalcubes_libgdalcubes_from_json_file(SEXP pathSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type path(pathSEXP);
rcpp_result_gen = Rcpp::wrap(libgdalcubes_from_json_file(path));
return rcpp_result_gen;
END_RCPP
}
// libgdalcubes_from_json_string
SEXP libgdalcubes_from_json_string(std::string json);
RcppExport SEXP _gdalcubes_libgdalcubes_from_json_string(SEXP jsonSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type json(jsonSEXP);
rcpp_result_gen = Rcpp::wrap(libgdalcubes_from_json_string(json));
return rcpp_result_gen;
END_RCPP
}
// libgdalcubes_create_rename_bands_cube
SEXP libgdalcubes_create_rename_bands_cube(SEXP pin, std::vector<std::string> names_old, std::vector<std::string> names_new);
RcppExport SEXP _gdalcubes_libgdalcubes_create_rename_bands_cube(SEXP pinSEXP, SEXP names_oldSEXP, SEXP names_newSEXP) {
Expand Down Expand Up @@ -730,36 +752,6 @@ BEGIN_RCPP
return R_NilValue;
END_RCPP
}
// libgdalcubes_translate_cog
std::string libgdalcubes_translate_cog(SEXP collection, std::string out_dir, uint16_t nthreads, bool overwrite, std::vector<std::string> creation_options);
RcppExport SEXP _gdalcubes_libgdalcubes_translate_cog(SEXP collectionSEXP, SEXP out_dirSEXP, SEXP nthreadsSEXP, SEXP overwriteSEXP, SEXP creation_optionsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type collection(collectionSEXP);
Rcpp::traits::input_parameter< std::string >::type out_dir(out_dirSEXP);
Rcpp::traits::input_parameter< uint16_t >::type nthreads(nthreadsSEXP);
Rcpp::traits::input_parameter< bool >::type overwrite(overwriteSEXP);
Rcpp::traits::input_parameter< std::vector<std::string> >::type creation_options(creation_optionsSEXP);
rcpp_result_gen = Rcpp::wrap(libgdalcubes_translate_cog(collection, out_dir, nthreads, overwrite, creation_options));
return rcpp_result_gen;
END_RCPP
}
// libgdalcubes_translate_gtiff
std::string libgdalcubes_translate_gtiff(SEXP collection, std::string out_dir, uint16_t nthreads, bool overwrite, std::vector<std::string> creation_options);
RcppExport SEXP _gdalcubes_libgdalcubes_translate_gtiff(SEXP collectionSEXP, SEXP out_dirSEXP, SEXP nthreadsSEXP, SEXP overwriteSEXP, SEXP creation_optionsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type collection(collectionSEXP);
Rcpp::traits::input_parameter< std::string >::type out_dir(out_dirSEXP);
Rcpp::traits::input_parameter< uint16_t >::type nthreads(nthreadsSEXP);
Rcpp::traits::input_parameter< bool >::type overwrite(overwriteSEXP);
Rcpp::traits::input_parameter< std::vector<std::string> >::type creation_options(creation_optionsSEXP);
rcpp_result_gen = Rcpp::wrap(libgdalcubes_translate_gtiff(collection, out_dir, nthreads, overwrite, creation_options));
return rcpp_result_gen;
END_RCPP
}
// libgdalcubes_simple_hash
std::string libgdalcubes_simple_hash(std::string instr);
RcppExport SEXP _gdalcubes_libgdalcubes_simple_hash(SEXP instrSEXP) {
Expand Down Expand Up @@ -813,6 +805,8 @@ static const R_CallMethodDef CallEntries[] = {
{"_gdalcubes_libgdalcubes_create_ncdf_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_create_ncdf_cube, 3},
{"_gdalcubes_libgdalcubes_create_dummy_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_create_dummy_cube, 4},
{"_gdalcubes_libgdalcubes_copy_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_copy_cube, 1},
{"_gdalcubes_libgdalcubes_from_json_file", (DL_FUNC) &_gdalcubes_libgdalcubes_from_json_file, 1},
{"_gdalcubes_libgdalcubes_from_json_string", (DL_FUNC) &_gdalcubes_libgdalcubes_from_json_string, 1},
{"_gdalcubes_libgdalcubes_create_rename_bands_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_create_rename_bands_cube, 3},
{"_gdalcubes_libgdalcubes_create_reduce_time_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_create_reduce_time_cube, 3},
{"_gdalcubes_libgdalcubes_create_stream_reduce_time_cube", (DL_FUNC) &_gdalcubes_libgdalcubes_create_stream_reduce_time_cube, 4},
Expand Down Expand Up @@ -845,8 +839,6 @@ static const R_CallMethodDef CallEntries[] = {
{"_gdalcubes_libgdalcubes_set_threads", (DL_FUNC) &_gdalcubes_libgdalcubes_set_threads, 1},
{"_gdalcubes_libgdalcubes_set_progress", (DL_FUNC) &_gdalcubes_libgdalcubes_set_progress, 1},
{"_gdalcubes_libgdalcubes_set_use_overviews", (DL_FUNC) &_gdalcubes_libgdalcubes_set_use_overviews, 1},
{"_gdalcubes_libgdalcubes_translate_cog", (DL_FUNC) &_gdalcubes_libgdalcubes_translate_cog, 5},
{"_gdalcubes_libgdalcubes_translate_gtiff", (DL_FUNC) &_gdalcubes_libgdalcubes_translate_gtiff, 5},
{"_gdalcubes_libgdalcubes_simple_hash", (DL_FUNC) &_gdalcubes_libgdalcubes_simple_hash, 1},
{"_gdalcubes_libgdalcubes_create_stac_collection", (DL_FUNC) &_gdalcubes_libgdalcubes_create_stac_collection, 5},
{NULL, NULL, 0}
Expand Down
2 changes: 1 addition & 1 deletion src/gdalcubes

0 comments on commit e79f4ed

Please sign in to comment.