Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Mar 2, 2020
1 parent 56fb8e1 commit 5ac8e12
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/stars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ NumericVector read_gdal_data(GDALDataset *poDataset,
if (! NumericVector::is_na(nodatavalue[0]) || has_offset || has_scale) {


// for (R_xlen_t j = 0; j < Rf_xlength(vec); j++) {
// for (R_xlen_t j = 0; j < Rf_xlength(vec); j++)
for (R_xlen_t j = i * (((R_xlen_t) nBufXSize) * nBufYSize); // start of band i
j < (i + 1) * (((R_xlen_t) nBufXSize) * nBufYSize); // end of band i
j++) {
Expand Down Expand Up @@ -202,6 +202,24 @@ int get_from_list(List lst, const char *name, int otherwise) {
return(otherwise);
}

NumericMatrix get_color_table(GDALColorTable *tbl) {
int n = tbl->GetColorEntryCount();
NumericMatrix t(n, 4);
for (int i = 0; i < n; i++) {
const GDALColorEntry *ce = tbl->GetColorEntry(i);
t(i, 0) = ce->c1;
t(i, 1) = ce->c2;
t(i, 2) = ce->c3;
t(i, 3) = ce->c4;
}
// const char *interp = GDALGetColorInterpretationName(
// GDALGetRasterColorInterpretation(tbl->ToHandle(tbl)));
// t.attr("interpretation") = CharacterVector::create(interp);
int i = (int) tbl->GetPaletteInterpretation();
t.attr("interpretation") = IntegerVector::create(i);
return t;
}

// [[Rcpp::export]]
List CPL_read_gdal(CharacterVector fname, CharacterVector options, CharacterVector driver,
bool read_data, NumericVector NA_value, List RasterIO_parameters) {
Expand Down Expand Up @@ -250,6 +268,7 @@ List CPL_read_gdal(CharacterVector fname, CharacterVector options, CharacterVect

GDALRasterBand *poBand = NULL;
NumericVector nodatavalue = NumericVector::create(NA_REAL);

if (poDataset->GetRasterCount()) {
poBand = poDataset->GetRasterBand( 1 );
int set = 0;
Expand All @@ -262,6 +281,13 @@ List CPL_read_gdal(CharacterVector fname, CharacterVector options, CharacterVect
nodatavalue[0] = poBand->GetNoDataValue(NULL); // #nocov
}

List colorTables(poDataset->GetRasterCount());
for (int i; i < poDataset->GetRasterCount(); i++) {
poBand = poDataset->GetRasterBand(i + 1);
if (poBand->GetColorTable() != NULL)
colorTables(i) = get_color_table(poBand->GetColorTable());
}

CharacterVector items = get_meta_data((GDALDatasetH) poDataset, NA_STRING);
CharacterVector sub = NA_STRING;
for (int i = 0; i < items.size(); i++) {
Expand Down Expand Up @@ -330,7 +356,8 @@ List CPL_read_gdal(CharacterVector fname, CharacterVector options, CharacterVect
CharacterVector::create(NA_STRING),
_["sub"] = sub,
_["meta"] = get_meta_data(poDataset, CharacterVector::create()),
_["band_meta"] = get_band_meta_data(poDataset)
_["band_meta"] = get_band_meta_data(poDataset),
_["color_tables"] = colorTables
);
if (read_data) {
ReturnList.attr("data") = read_gdal_data(poDataset, nodatavalue, nXOff, nYOff,
Expand Down

0 comments on commit 5ac8e12

Please sign in to comment.