Skip to content

Commit 99d6565

Browse files
committed
read_mdim: add groups
r-spatial/stars#659
1 parent 96d98e7 commit 99d6565

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ CPL_raw_to_hex <- function(raw) {
269269
.Call(`_sf_CPL_raw_to_hex`, raw)
270270
}
271271

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

276276
CPL_write_mdim <- function(name, driver, dimensions, variables, wkt, xy, RootGroupOptions, CreationOptions, as_float = TRUE) {

R/stars.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,16 +348,17 @@ gdal_extract = function(f, pts, bilinear = FALSE) {
348348
#' @rdname gdal
349349
#' @param file file name
350350
#' @param array_name array name
351+
#' @param groups character vector with groups (if any)
351352
#' @param offset offset (pixels)
352353
#' @param count number of pixels to read
353354
#' @param step step size (pixels)
354355
#' @param proxy logical; return proxy object?
355356
#' @param debug logical; print debug messages?
356357
#' @export
357-
gdal_read_mdim = function(file, array_name = character(0), options = character(0),
358+
gdal_read_mdim = function(file, array_name = character(0), groups = character(0), options = character(0),
358359
offset = integer(0), count = integer(0), step = integer(0),
359360
proxy = FALSE, debug = FALSE) {
360-
CPL_read_mdim(file, array_name, options, offset, count, step, proxy, debug)
361+
CPL_read_mdim(file, array_name, groups, options, offset, count, step, proxy, debug)
361362
}
362363

363364
#' @rdname gdal

man/gdal.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -923,20 +923,21 @@ BEGIN_RCPP
923923
END_RCPP
924924
}
925925
// CPL_read_mdim
926-
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
927-
RcppExport SEXP _sf_CPL_read_mdim(SEXP fileSEXP, SEXP array_namesSEXP, SEXP ooSEXP, SEXP offsetSEXP, SEXP countSEXP, SEXP stepSEXP, SEXP proxySEXP, SEXP debugSEXP) {
926+
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups, CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step, bool proxy, bool debug);
927+
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) {
928928
BEGIN_RCPP
929929
Rcpp::RObject rcpp_result_gen;
930930
Rcpp::RNGScope rcpp_rngScope_gen;
931931
Rcpp::traits::input_parameter< CharacterVector >::type file(fileSEXP);
932932
Rcpp::traits::input_parameter< CharacterVector >::type array_names(array_namesSEXP);
933+
Rcpp::traits::input_parameter< CharacterVector >::type groups(groupsSEXP);
933934
Rcpp::traits::input_parameter< CharacterVector >::type oo(ooSEXP);
934935
Rcpp::traits::input_parameter< IntegerVector >::type offset(offsetSEXP);
935936
Rcpp::traits::input_parameter< IntegerVector >::type count(countSEXP);
936937
Rcpp::traits::input_parameter< IntegerVector >::type step(stepSEXP);
937938
Rcpp::traits::input_parameter< bool >::type proxy(proxySEXP);
938939
Rcpp::traits::input_parameter< bool >::type debug(debugSEXP);
939-
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, oo, offset, count, step, proxy, debug));
940+
rcpp_result_gen = Rcpp::wrap(CPL_read_mdim(file, array_names, groups, oo, offset, count, step, proxy, debug));
940941
return rcpp_result_gen;
941942
END_RCPP
942943
}
@@ -1523,7 +1524,7 @@ static const R_CallMethodDef CallEntries[] = {
15231524
{"_sf_CPL_line_interpolate", (DL_FUNC) &_sf_CPL_line_interpolate, 3},
15241525
{"_sf_CPL_hex_to_raw", (DL_FUNC) &_sf_CPL_hex_to_raw, 1},
15251526
{"_sf_CPL_raw_to_hex", (DL_FUNC) &_sf_CPL_raw_to_hex, 1},
1526-
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 8},
1527+
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 9},
15271528
{"_sf_CPL_write_mdim", (DL_FUNC) &_sf_CPL_write_mdim, 9},
15281529
{"_sf_opp_sfc", (DL_FUNC) &_sf_opp_sfc, 4},
15291530
{"_sf_normalize_sfc", (DL_FUNC) &_sf_normalize_sfc, 4},

src/mdim.cpp

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,41 @@ List get_geometry(std::shared_ptr<GDALGroup> curGroup) {
147147
return(lst);
148148
}
149149

150+
List get_all_arrays(std::shared_ptr<GDALGroup> curGroup, List ret, std::string name) {
151+
auto array_names(curGroup->GetMDArrayNames());
152+
// for (size_t i = 0 i < array_names
153+
CharacterVector a(array_names.size());
154+
// ret needs to be a _named_ list, so that na is never null
155+
CharacterVector na = ret.attr("names");
156+
if (a.size() > 0) { // group with array(s):
157+
for (int i = 0; i < a.size(); i++)
158+
a[i] = array_names[i];
159+
ret.push_back(a);
160+
CharacterVector gn;
161+
// gn.push_back("");
162+
std::string group_name;
163+
if (name == "/")
164+
group_name = name;
165+
else
166+
group_name = name + "/";
167+
na.push_back(group_name);
168+
}
169+
ret.attr("names") = na;
170+
auto gn(curGroup->GetGroupNames());
171+
for (const auto &gn: curGroup->GetGroupNames()) { // iterate over groups:
172+
std::string slash;
173+
if (name == "/")
174+
slash = "";
175+
else
176+
slash = "/";
177+
ret = get_all_arrays(curGroup->OpenGroup(gn), ret, name + slash + gn);
178+
}
179+
return ret;
180+
}
181+
150182
// [[Rcpp::export]]
151-
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector oo,
152-
IntegerVector offset, IntegerVector count, IntegerVector step,
183+
List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterVector groups,
184+
CharacterVector oo, IntegerVector offset, IntegerVector count, IntegerVector step,
153185
bool proxy = false, bool debug = false) {
154186

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

198+
if (array_names.size() == 1 && array_names[0] == "?") {
199+
List l;
200+
l.attr("names") = CharacterVector::create();
201+
return get_all_arrays(poRootGroup, l, poRootGroup->GetName());
202+
}
203+
166204
auto curGroup = poRootGroup;
167-
auto groupNames = poRootGroup->GetGroupNames();
168-
if (groupNames.size() > 0) {
169-
curGroup = curGroup->OpenGroup(groupNames[0]);
170-
if (!curGroup) {
171-
Rcout << "group: " << groupNames[0] << ";" << std::endl;
172-
stop("Cannot find group");
173-
}
174-
if (debug && groupNames.size() > 1) {
175-
Rcout << "ignored groups: ";
176-
for (size_t i = 1; i < groupNames.size(); i++)
177-
Rcout << groupNames[i] << " ";
178-
Rcout << std::endl;
205+
if (groups.size() > 0) {
206+
for (int i = 0; i < groups.size(); i++) {
207+
curGroup = curGroup->OpenGroup(Rcpp::as<std::string>(groups[i]));
208+
if (curGroup == nullptr) {
209+
Rcout << "group: " << groups[i] << " ";
210+
stop("cannot open group: does it exist?");
211+
}
212+
if (debug)
213+
Rcout << groups[i] << " ";
179214
}
180-
} else if (debug)
181-
Rcout << "using root group" << std::endl;
215+
Rcout << std::endl;
216+
} else {
217+
auto groupNames = poRootGroup->GetGroupNames();
218+
if (groupNames.size() > 0) {
219+
curGroup = curGroup->OpenGroup(groupNames[0]);
220+
if (!curGroup) {
221+
Rcout << "group: " << groupNames[0] << ";" << std::endl;
222+
stop("Cannot find group");
223+
}
224+
if (debug && groupNames.size() > 1) {
225+
Rcout << "ignored groups: ";
226+
for (size_t i = 1; i < groupNames.size(); i++)
227+
Rcout << groupNames[i] << " ";
228+
Rcout << std::endl;
229+
}
230+
} else if (debug)
231+
Rcout << "using root group" << std::endl;
232+
}
182233

183234
// find possible vector geometry array, and construct
184235
List geometry = get_geometry(curGroup);
@@ -199,11 +250,14 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
199250
if (ndim == largest_size)
200251
array_names.push_back(an);
201252
}
253+
if (array_names.size() == 0)
254+
stop("no array names found");
202255
}
203256
int n = array_names.size();
204257

205258
const char *name = array_names[0];
206-
auto array(curGroup->OpenMDArray(name));
259+
std::shared_ptr<GDALMDArray> array;
260+
array = curGroup->OpenMDArray(name);
207261
if (!array)
208262
stop("Cannot find array");
209263
if (offset.size() != 0 && (size_t) offset.size() != array->GetDimensionCount())

0 commit comments

Comments
 (0)