Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Nov 15, 2024
1 parent 867a78e commit 052bafe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 42 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: terra
Type: Package
Title: Spatial Data Analysis
Version: 1.8-0
Date: 2024-10-14
Version: 1.8-1
Date: 2024-11-15
Depends: R (>= 3.5.0)
Suggests: parallel, tinytest, ncdf4, sf (>= 0.9-8), deldir, XML, leaflet (>= 2.2.1), htmlwidgets
LinkingTo: Rcpp
Expand Down
13 changes: 11 additions & 2 deletions R/tapp.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function(x, index, fun, ..., cores=1, filename="", overwrite=FALSE, wopt=list())
out_tz <- "UTC"
if (length(index) == 1) {
if (is.character(index)) {
choices <- c("years", "months", "week", "days", "doy", "yearmonths", "yearweeks", "7days", "10days", "15days")
choices <- c("years", "months", "dekads", "weeks", "days", "doy", "yearmonths", "yeardekads", "yearweeks", "7days", "10days", "15days")
i <- pmatch(tolower(index), choices)
if (is.na(i)) {
error("tapp", paste("invalid time step. Use one of:", paste(choices, collapse=", ")))
Expand All @@ -25,7 +25,16 @@ function(x, index, fun, ..., cores=1, filename="", overwrite=FALSE, wopt=list())
# or POSIXlt$yday
index <- format(time(x, "days"), "%j")
prename <- "doy_"
} else if (choice == "week") {
} else if (choice == "dekads") {
index <- floor(as.integer(format(time(x, "days"), "%j")) / 10) + 1
prename <- "dekad_"
} else if (choice == "yeardekads") {
year <- time(res, "years")
year <- formatC(year, width=4, flag = "0")
dekad <- floor(as.integer(format(time(x, "days"), "%j")) / 10) + 1
index <- paste0(year, dekad)
prename <- "yd_"
} else if (choice == "weeks") {
index <- strftime(time(x, "days"), format = "%V")
prename <- "week_"
} else if (choice == "yearweeks") {
Expand Down
2 changes: 1 addition & 1 deletion man/tapp.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The number of layers in the output SpatRaster equals the number of unique values

For example, if you have a SpatRaster with 6 layers, you can use \code{index=c(1,1,1,2,2,2)} and \code{fun=sum}. This will return a SpatRaster with two layers. The first layer is the sum of the first three layers in the input SpatRaster, and the second layer is the sum of the last three layers in the input SpatRaster. Indices are recycled such that \code{index=c(1,2)} would also return a SpatRaster with two layers (one based on the odd layers (1,3,5), the other based on the even layers (2,4,6)).

The index can also be one of the following values to group by time period (if \code{x} has the appropriate \code{\link{time}} values): "years", "months", "yearmonths", "week" (the ISO 8601 week number, see Details), "yearweeks", "days", "doy" (day of the year), "7days" (seven-day periods starting at Jan 1 of each year), "10days", or "15days". It can also be a function that makes groups from time values.
The index can also be one of the following values to group by time period (if \code{x} has the appropriate \code{\link{time}} values): "years", "months", "yearmonths", "dekad", "yeardekad", "week" (the ISO 8601 week number, see Details), "yearweeks", "days", "doy" (day of the year), "7days" (seven-day periods starting at Jan 1 of each year), "10days", or "15days". It can also be a function that makes groups from time values.

See \code{\link{app}} or \code{\link{Summary-methods}} if you want to use a more efficient function that returns multiple layers based on \bold{all} layers in the SpatRaster.
}
Expand Down
50 changes: 14 additions & 36 deletions src/rasterize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,11 @@ SpatRaster SpatRaster::rasterize(SpatVector x, std::string field, std::vector<do
return out;
}

bool hasError = false;
SpatExtent e = temp.getExtent();
SpatRaster tmp;
for (size_t i = 0; i < out.bs.n; i++) {
SpatRaster tmp = temp;
tmp = temp;
if (out.bs.n > 1) {
SpatOptions topt(opt);
double halfres = tmp.yres() / 2;
Expand All @@ -726,10 +728,7 @@ SpatRaster SpatRaster::rasterize(SpatVector x, std::string field, std::vector<do
if ( err != CE_None ) {
tmp.setError("rasterization failed");
GDALClose(rstDS);
for (size_t i=0; i<ogrGeoms.size(); i++) {
OGR_G_DestroyGeometry(ogrGeoms[i]);
}
return tmp;
hasError = true; break;
}
//GDALFlushCache(rstDS);
// second time to fix the internal area
Expand Down Expand Up @@ -757,46 +756,25 @@ SpatRaster SpatRaster::rasterize(SpatVector x, std::string field, std::vector<do
if ( err != CE_None ) {
tmp.setError("rasterization failed");
GDALClose(rstDS);
return tmp;
hasError = true; break;
}

bool hasError = false;
// if (driver == "MEM") {
if (!tmp.from_gdalMEM(rstDS, false, true)) {
tmp.setError("rasterization failed");
GDALClose(rstDS);
return(tmp);
}
// }

// GDALRasterBandH band = GDALGetRasterBand(rstDS, 1);

// if (minmax) {
// double adfMinMax[2];
// GDALComputeRasterMinMax(band, false, adfMinMax);
// GDALSetRasterStatistics(band, adfMinMax[0], adfMinMax[1], -9999, -9999);
// }

if (!tmp.from_gdalMEM(rstDS, false, true)) {
tmp.setError("rasterization failed");
GDALClose(rstDS);
hasError = true; break;
}
GDALClose(rstDS);
//if (driver != "MEM") {
// out = SpatRaster(filename, {-1}, {""}, {}, {});
//} else {
// std::string fname = opt.get_filename();
// if (!fname.empty() && (!update)) {
// out = out.writeRaster(opt);
// }
//}

if (hasError) return tmp;

if (!out.writeBlock(tmp.source[0].values, i)) return out;
}

out.writeStop();
for (size_t i=0; i<ogrGeoms.size(); i++) {
OGR_G_DestroyGeometry(ogrGeoms[i]);
}
out.writeStop();

// if (update) {}
// if (rstDS != NULL) GDALClose(rstDS);
if (hasError) return tmp;

return out;
}
Expand Down
2 changes: 1 addition & 1 deletion src/read_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ bool getGCPs(GDALDataset *poDataset, SpatRasterSource &s) {
}


bool SpatRaster::constructFromFile(std::string fname, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> drivers, std::vector<std::string> options, bool flipped) {
bool SpatRaster::constructFromFile(std::string fname, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> drivers, std::vector<std::string> options) {

if (fname == "WCS:") {
// for https://github.com/rspatial/terra/issues/1505
Expand Down

0 comments on commit 052bafe

Please sign in to comment.