Skip to content

Commit dd86bb2

Browse files
committed
Merge branch 'components'
2 parents 876b6ba + bd19b4c commit dd86bb2

File tree

1 file changed

+53
-23
lines changed

1 file changed

+53
-23
lines changed

src/mdim.cpp

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,20 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
305305
auto arr(get_array(curGroup, name));
306306
dims.attr("names") = dim_names;
307307
dimensions.attr("names") = dim_names;
308-
NumericVector vec;
309308
if (! proxy) { // read the arrays:
310-
NumericVector vec_(nValues);
311-
if (debug)
312-
Rcout << "size of vec_: " << vec_.size() << "\n";
313-
bool ok = arr->Read(offst.data(),
314-
anCount.data(),
315-
stp.data(), /* step: defaults to 1,1,1 */
316-
nullptr, /* stride: default to row-major convention */
317-
GDALExtendedDataType::Create(GDT_Float64),
318-
vec_.begin());
319-
if (!ok) {
320-
Rcout << "Cannot read array into a Float64 buffer" << name << std::endl;
321-
for (size_t i = 0; i < nValues; i++)
322-
vec_[i] = NA_REAL;
323-
} else {
309+
auto data_type(arr->GetDataType());
310+
if (data_type.GetClass() == GEDTC_NUMERIC) {
311+
NumericVector vec(nValues);
312+
if (debug)
313+
Rcout << "size of vec: " << vec.size() << "\n";
314+
bool ok = arr->Read(offst.data(),
315+
anCount.data(),
316+
stp.data(), /* step: defaults to 1,1,1 */
317+
nullptr, /* stride: default to row-major convention */
318+
GDALExtendedDataType::Create(GDT_Float64),
319+
vec.begin());
320+
if (!ok)
321+
stop("Cannot read array into a Float64 buffer");
324322
bool has_offset = false;
325323
double offst = arr->GetOffset(&has_offset);
326324
if (!has_offset)
@@ -332,19 +330,51 @@ List CPL_read_mdim(CharacterVector file, CharacterVector array_names, CharacterV
332330
bool has_nodata = false;
333331
double nodata_value = arr->GetNoDataValueAsDouble(&has_nodata);
334332
if (has_offset || has_scale || has_nodata) {
335-
for (size_t i = 0; i < nValues; i++) {
336-
if (ISNAN(vec_[i]) || (has_nodata && vec_[i] == nodata_value))
337-
vec_[i] = NA_REAL;
333+
for (size_t j = 0; j < nValues; j++) {
334+
if (ISNAN(vec[j]) || (has_nodata && vec[j] == nodata_value))
335+
vec[j] = NA_REAL;
338336
else
339-
vec_[i] = vec_[i] * scale + offst;
337+
vec[j] = vec[j] * scale + offst;
340338
}
341339
}
340+
vec.attr("dim") = dims;
341+
vec.attr("units") = arr->GetUnit();
342+
vec_lst[i] = vec;
343+
} else if (data_type.GetClass() == GEDTC_COMPOUND) {
344+
const auto &components = data_type.GetComponents();
345+
size_t sz = data_type.GetSize();
346+
std::vector<GByte> buf(sz * nValues);
347+
bool ok = arr->Read(offst.data(),
348+
anCount.data(),
349+
stp.data(), /* step: defaults to 1,1,1 */
350+
nullptr, /* stride: default to row-major convention */
351+
data_type,
352+
&buf[0]);
353+
DataFrame tbl;
354+
GByte *v = buf.data();
355+
for (const auto &co: components) {
356+
auto t(co->GetType());
357+
if (t.GetClass() == GEDTC_NUMERIC) {
358+
NumericVector vec(nValues);
359+
for (int j = 0; j < nValues; j++)
360+
memcpy(&(vec[j]), v + j * sz + co->GetOffset(), sizeof(double));
361+
tbl.push_back(vec, co->GetName());
362+
} else if (t.GetClass() == GEDTC_STRING) {
363+
CharacterVector vec(nValues);
364+
const char *str;
365+
for (int j = 0; j < nValues; j++) {
366+
memcpy(&str, v + j * sz + co->GetOffset(), sizeof(const char *));
367+
vec[j] = str; // deep copy
368+
}
369+
tbl.push_back(vec, co->GetName());
370+
} else
371+
stop("unsupported type");
372+
}
373+
vec_lst[i] = tbl;
374+
} else { // GEDTC_STRING:
375+
stop("reading string data not implemented");
342376
}
343-
vec_.attr("dim") = dims;
344-
vec_.attr("units") = arr->GetUnit();
345-
vec = vec_;
346377
}
347-
vec_lst[i] = vec;
348378
}
349379
vec_lst.attr("names") = a_names;
350380
std::shared_ptr<OGRSpatialReference> srs = array->GetSpatialRef();

0 commit comments

Comments
 (0)