Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* handle 0 and gaps in attribute and color tables
  • Loading branch information
edzer committed Jun 16, 2022
1 parent cdb917c commit c4a617b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
32 changes: 26 additions & 6 deletions R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,42 @@ gdal_write = function(x, ..., file, driver = "GTiff", options = character(0), ty
from = c(0, 0) # nocov end
} else {
mat = x[[1]]
dm = dim(mat)
if (is.factor(mat)) {
rgba = NULL
ex = attr(mat, "exclude")
if (is.null(ex))
lev = c("", levels(mat)) # add "" for value 0: R factors start at 1
else {
if (any(ex)) {
lev = vector("character", length(ex)) # fills with ""
lev[!ex] = levels(mat)
rgba = if (!is.null(co <- attr(mat, "rgba"))) {
n = length(ex)
coltab = cbind(rep(0., n), rep(0, n), rep(0, n), rep(255, n))
coltab[!ex,] = co
coltab
}
values = which(!ex) - 1
mat = values[as.numeric(mat)]
} else
lev = levels(mat)
}
mat = structure(mat, class = NULL, levels = lev, dim = dm, rgba = rgba)
}
only_create = FALSE # write x too
if (! update) {
if (!all(from == 0))
stop("cannot write sub-rasters only")
if (!all(dims == dim(mat)))
if (!all(dims == dm))
stop("dimensions don't match")
}
dm = dim(mat)
dim(mat) = c(dm[1], prod(dm[-1])) # flatten to 2-D matrix
}
if (length(dims) == 2)
dims = c(dims, 1) # one band
else { # add band descriptions?
if (is.character(d[[3]]$values))
attr(mat, "descriptions") = d[[3]]$values
}
else if (is.character(d[[3]]$values)) # add band descriptions?
attr(mat, "descriptions") = d[[3]]$values

CPL_write_gdal(mat, file, driver, options, type, dims, from, geotransform,
st_crs(x)[[2]], as.double(NA_value), create = !update, only_create = only_create)
Expand Down
10 changes: 2 additions & 8 deletions src/stars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,9 @@ void CPL_write_gdal(NumericMatrix x, CharacterVector fname, CharacterVector driv
// write factor levels to CategoryNames:
if (x.attr("levels") != R_NilValue) {
Rcpp::CharacterVector levels = x.attr("levels");
Rcpp::CharacterVector l(levels.size() + 1);
l[0] = ""; // levels start at 1, CategoryNames at 0.
for (int i = 0; i < levels.size(); i++)
l[i+1] = levels[i];
for (int band = 1; band <= dims(2); band++) {
GDALRasterBand *poBand = poDstDS->GetRasterBand( band );
if (poBand->SetCategoryNames(create_options(l).data()) != CE_None)
if (poBand->SetCategoryNames(create_options(levels).data()) != CE_None)
warning("error writing factor levels to raster band");
}
}
Expand All @@ -604,14 +600,12 @@ void CPL_write_gdal(NumericMatrix x, CharacterVector fname, CharacterVector driv
Rcpp::NumericMatrix co = x.attr("rgba"); // r g b alpha in columns; levels in rows
GDALColorTable ct = GDALColorTable(GPI_RGB);
GDALColorEntry ce;
ce.c1 = 0; ce.c2 = 0; ce.c3 = 0; ce.c4 = 0;
ct.SetColorEntry(0, &ce);
for (int i = 0; i < co.nrow(); i++) {
ce.c1 = co(i, 0);
ce.c2 = co(i, 1);
ce.c3 = co(i, 2);
ce.c4 = co(i, 3);
ct.SetColorEntry(i + 1, &ce);
ct.SetColorEntry(i, &ce);
}
GDALRasterBand *poBand = poDstDS->GetRasterBand( 1 ); // can only set CT for band 1
if (poBand->SetColorTable(&ct) != CE_None)
Expand Down

0 comments on commit c4a617b

Please sign in to comment.