Skip to content

Commit

Permalink
read_mdim: add groups
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jan 4, 2024
1 parent 96d98e7 commit 99d6565
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ CPL_raw_to_hex <- function(raw) {
.Call(`_sf_CPL_raw_to_hex`, raw)
}

CPL_read_mdim <- function(file, array_names, oo, offset, count, step, proxy = FALSE, debug = FALSE) {
.Call(`_sf_CPL_read_mdim`, file, array_names, oo, offset, count, step, proxy, debug)
CPL_read_mdim <- function(file, array_names, groups, oo, offset, count, step, proxy = FALSE, debug = FALSE) {
.Call(`_sf_CPL_read_mdim`, file, array_names, groups, oo, offset, count, step, proxy, debug)
}

CPL_write_mdim <- function(name, driver, dimensions, variables, wkt, xy, RootGroupOptions, CreationOptions, as_float = TRUE) {
Expand Down
5 changes: 3 additions & 2 deletions R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ gdal_extract = function(f, pts, bilinear = FALSE) {
#' @rdname gdal
#' @param file file name
#' @param array_name array name
#' @param groups character vector with groups (if any)
#' @param offset offset (pixels)
#' @param count number of pixels to read
#' @param step step size (pixels)
#' @param proxy logical; return proxy object?
#' @param debug logical; print debug messages?
#' @export
gdal_read_mdim = function(file, array_name = character(0), options = character(0),
gdal_read_mdim = function(file, array_name = character(0), groups = character(0), options = character(0),
offset = integer(0), count = integer(0), step = integer(0),
proxy = FALSE, debug = FALSE) {
CPL_read_mdim(file, array_name, options, offset, count, step, proxy, debug)
CPL_read_mdim(file, array_name, groups, options, offset, count, step, proxy, debug)
}

#' @rdname gdal
Expand Down
3 changes: 3 additions & 0 deletions man/gdal.Rd

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

9 changes: 5 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,20 +923,21 @@ BEGIN_RCPP
END_RCPP
}
// CPL_read_mdim
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
RcppExport SEXP _sf_CPL_read_mdim(SEXP fileSEXP, SEXP array_namesSEXP, SEXP ooSEXP, SEXP offsetSEXP, SEXP countSEXP, SEXP stepSEXP, SEXP proxySEXP, SEXP debugSEXP) {
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
RcppExport SEXP _sf_CPL_read_mdim(SEXP fileSEXP, SEXP array_namesSEXP, SEXP groupsSEXP, SEXP ooSEXP, SEXP offsetSEXP, SEXP countSEXP, SEXP stepSEXP, SEXP proxySEXP, SEXP debugSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< CharacterVector >::type file(fileSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type array_names(array_namesSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type groups(groupsSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type oo(ooSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type offset(offsetSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type count(countSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type step(stepSEXP);
Rcpp::traits::input_parameter< bool >::type proxy(proxySEXP);
Rcpp::traits::input_parameter< bool >::type debug(debugSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, oo, offset, count, step, proxy, debug));
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, groups, oo, offset, count, step, proxy, debug));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -1523,7 +1524,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_line_interpolate", (DL_FUNC) &_sf_CPL_line_interpolate, 3},
{"_sf_CPL_hex_to_raw", (DL_FUNC) &_sf_CPL_hex_to_raw, 1},
{"_sf_CPL_raw_to_hex", (DL_FUNC) &_sf_CPL_raw_to_hex, 1},
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 8},
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 9},
{"_sf_CPL_write_mdim", (DL_FUNC) &_sf_CPL_write_mdim, 9},
{"_sf_opp_sfc", (DL_FUNC) &_sf_opp_sfc, 4},
{"_sf_normalize_sfc", (DL_FUNC) &_sf_normalize_sfc, 4},
Expand Down
88 changes: 71 additions & 17 deletions src/mdim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,41 @@ List get_geometry(std::shared_ptr<GDALGroup> curGroup) {
return(lst);
}

List get_all_arrays(std::shared_ptr<GDALGroup> curGroup, List ret, std::string name) {
auto array_names(curGroup->GetMDArrayNames());
// for (size_t i = 0 i < array_names
CharacterVector a(array_names.size());
// ret needs to be a _named_ list, so that na is never null
CharacterVector na = ret.attr("names");
if (a.size() > 0) { // group with array(s):
for (int i = 0; i < a.size(); i++)
a[i] = array_names[i];
ret.push_back(a);
CharacterVector gn;
// gn.push_back("");
std::string group_name;
if (name == "/")
group_name = name;
else
group_name = name + "/";
na.push_back(group_name);
}
ret.attr("names") = na;
auto gn(curGroup->GetGroupNames());
for (const auto &gn: curGroup->GetGroupNames()) { // iterate over groups:
std::string slash;
if (name == "/")
slash = "";
else
slash = "/";
ret = get_all_arrays(curGroup->OpenGroup(gn), ret, name + slash + gn);
}
return ret;
}

// [[Rcpp::export]]
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo,
IntegerVector offset, IntegerVector count, IntegerVector step,
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups,
CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step,
bool proxy = false, bool debug = false) {

std::vector <char *> oo_char = create_options(oo, true); // open options
Expand All @@ -163,22 +195,41 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
if( !poRootGroup )
stop("cannot open root group");

if (array_names.size() == 1 && array_names[0] == "?") {
List l;
l.attr("names") = CharacterVector::create();
return get_all_arrays(poRootGroup, l, poRootGroup->GetName());
}

auto curGroup = poRootGroup;
auto groupNames = poRootGroup->GetGroupNames();
if (groupNames.size() > 0) {
curGroup = curGroup->OpenGroup(groupNames[0]);
if (!curGroup) {
Rcout << "group: " << groupNames[0] << ";" << std::endl;
stop("Cannot find group");
}
if (debug && groupNames.size() > 1) {
Rcout << "ignored groups: ";
for (size_t i = 1; i < groupNames.size(); i++)
Rcout << groupNames[i] << " ";
Rcout << std::endl;
if (groups.size() > 0) {
for (int i = 0; i < groups.size(); i++) {
curGroup = curGroup->OpenGroup(Rcpp::as<std::string>(groups[i]));
if (curGroup == nullptr) {
Rcout << "group: " << groups[i] << " ";
stop("cannot open group: does it exist?");
}
if (debug)
Rcout << groups[i] << " ";
}
} else if (debug)
Rcout << "using root group" << std::endl;
Rcout << std::endl;
} else {
auto groupNames = poRootGroup->GetGroupNames();
if (groupNames.size() > 0) {
curGroup = curGroup->OpenGroup(groupNames[0]);
if (!curGroup) {
Rcout << "group: " << groupNames[0] << ";" << std::endl;
stop("Cannot find group");
}
if (debug && groupNames.size() > 1) {
Rcout << "ignored groups: ";
for (size_t i = 1; i < groupNames.size(); i++)
Rcout << groupNames[i] << " ";
Rcout << std::endl;
}
} else if (debug)
Rcout << "using root group" << std::endl;
}

// find possible vector geometry array, and construct
List geometry = get_geometry(curGroup);
Expand All @@ -199,11 +250,14 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
if (ndim == largest_size)
array_names.push_back(an);
}
if (array_names.size() == 0)
stop("no array names found");
}
int n = array_names.size();

const char *name = array_names[0];
auto array(curGroup->OpenMDArray(name));
std::shared_ptr<GDALMDArray> array;
array = curGroup->OpenMDArray(name);
if (!array)
stop("Cannot find array");
if (offset.size() != 0 && (size_t) offset.size() != array->GetDimensionCount())
Expand Down

0 comments on commit 99d6565

Please sign in to comment.