Skip to content

Commit

Permalink
fixes #1412
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 29, 2024
1 parent d91575e commit 69f3867
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
7 changes: 5 additions & 2 deletions R/plot_raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,12 @@ prettyNumbs <- function(x, digits) {
if (nlyr(alpha) > 1) {
alpha <- alpha[[1]]
}
alpha <- spatSample(alpha, maxcell, method="regular", as.raster=TRUE, warn=FALSE)
# alpha <- spatSample(alpha, maxcell, method="regular", as.raster=TRUE, warn=FALSE)
alpha <- sampleRaster(alpha, maxcell, method="regular", replace=FALSE, ext=NULL, warn=FALSE, overview=TRUE)
}
x <- spatSample(x, maxcell, method="regular", as.raster=TRUE, warn=FALSE)
# x <- spatSample(x, maxcell, method="regular", as.raster=TRUE, warn=FALSE)
x <- sampleRaster(x, maxcell, method="regular", replace=FALSE, ext=NULL, warn=FALSE, overview=TRUE)

out$lim <- out$ext <- as.vector(ext(x))
}

Expand Down
6 changes: 4 additions & 2 deletions src/read_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,7 @@ std::vector<double> SpatRaster::readValuesGDAL(size_t src, size_t row, size_t nr



std::vector<double> SpatRaster::readGDALsample(size_t src, size_t srows, size_t scols) {
std::vector<double> SpatRaster::readGDALsample(size_t src, size_t srows, size_t scols, bool overview) {

std::vector<double> errout;
if (source[src].rotated) {
Expand All @@ -1553,7 +1553,9 @@ std::vector<double> SpatRaster::readGDALsample(size_t src, size_t srows, size_t
#if GDAL_VERSION_MAJOR <= 3 && GDAL_VERSION_MINOR < 3
// do nothing
#else
openops.push_back("OVERVIEW_LEVEL=NONE");
if (!overview) {
openops.push_back("OVERVIEW_LEVEL=NONE");
}
#endif

GDALDataset *poDataset = openGDAL(source[src].filename, GDAL_OF_RASTER | GDAL_OF_READONLY, source[src].open_drivers, openops);
Expand Down
10 changes: 5 additions & 5 deletions src/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ std::vector<double> SpatRaster::readSample(size_t src, size_t srows, size_t scol
}


SpatRaster SpatRaster::sampleRegularRaster(double size) {
SpatRaster SpatRaster::sampleRegularRaster(double size, bool overview) {

if (size >= ncell()) {
return( *this );
Expand Down Expand Up @@ -119,7 +119,7 @@ SpatRaster SpatRaster::sampleRegularRaster(double size) {
// v = readSampleBinary(src, nr, nc);
} else {
#ifdef useGDAL
v = readGDALsample(src, nr, nc);
v = readGDALsample(src, nr, nc, overview);
#endif
}
if (hasError()) return out;
Expand Down Expand Up @@ -171,7 +171,7 @@ SpatRaster SpatRaster::sampleRowColRaster(size_t nr, size_t nc, bool warn) {
// v = readSampleBinary(src, nr, nc);
} else {
#ifdef useGDAL
v = readGDALsample(src, nr, nc);
v = readGDALsample(src, nr, nc, false);
#endif
}
if (hasError()) return out;
Expand Down Expand Up @@ -218,7 +218,7 @@ std::vector<std::vector<double>> SpatRaster::sampleRegularValues(double size, Sp
// v = readSampleBinary(src, nr, nc);
} else {
#ifdef useGDAL
v = readGDALsample(src, nr, nc);
v = readGDALsample(src, nr, nc, false);
#endif
}
if (hasError()) return out;
Expand Down Expand Up @@ -262,7 +262,7 @@ std::vector<std::vector<double>> SpatRaster::sampleRowColValues(size_t nr, size_
v = readSample(src, nr, nc);
} else {
#ifdef useGDAL
v = readGDALsample(src, nr, nc);
v = readGDALsample(src, nr, nc, false);
#endif
}
if (hasError()) return out;
Expand Down
20 changes: 11 additions & 9 deletions src/spatRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class SpatRaster {

// gdal source
std::vector<double> readValuesGDAL(size_t src, size_t row, size_t nrows, size_t col, size_t ncols, int lyr = -1);
std::vector<double> readGDALsample(size_t src, size_t srows, size_t scols);
std::vector<double> readGDALsample(size_t src, size_t srows, size_t scols, bool overview);
std::vector<std::vector<double>> readRowColGDAL(size_t src, std::vector<int_64> &rows, const std::vector<int_64> &cols);
std::vector<double> readRowColGDALFlat(size_t src, std::vector<int_64> &rows, const std::vector<int_64> &cols);

Expand Down Expand Up @@ -797,7 +797,7 @@ class SpatRaster {
SpatRaster rotate(bool left, SpatOptions &opt);

std::vector<size_t> sampleCells(double size, std::string method, bool replace, unsigned seed);
SpatRaster sampleRegularRaster(double size);
SpatRaster sampleRegularRaster(double size, bool overview);
SpatRaster sampleRowColRaster(size_t nr, size_t nc, bool warn);
SpatRaster sampleRandomRaster(double size, bool replace, unsigned seed);
std::vector<std::vector<double>> sampleRegularValues(double size, SpatOptions &opt);
Expand All @@ -812,13 +812,15 @@ class SpatRaster {
SpatRaster similarity(std::vector<double> x, SpatOptions &opt);

SpatRaster terrain(std::vector<std::string> v, unsigned neighbors, bool degrees, unsigned seed, SpatOptions &opt);
// watershed2 extension
SpatRaster watershed2(int pp_offset,SpatOptions &opt); // modified ecor 20210317 // EC 20210702
SpatRaster pitfinder2(SpatOptions &opt); // modified // EC 20220809
SpatRaster NIDP2(SpatOptions &opt); // modified // EC 20231031
SpatRaster flowAccu2(SpatOptions &opt); // modified // EC 20231108
SpatRaster flowAccu2_weight(SpatRaster weight,SpatOptions &opt); // modified // EC 20231108
// END watershed2 extension

// watershed2 ecor 20210317; EC 20210702
SpatRaster watershed2(int pp_offset,SpatOptions &opt);
SpatRaster pitfinder2(SpatOptions &opt);
SpatRaster NIDP2(SpatOptions &opt);
SpatRaster flowAccu2(SpatOptions &opt);
SpatRaster flowAccu2_weight(SpatRaster weight,SpatOptions &opt);
// END watershed2

SpatRaster hillshade(SpatRaster aspect, std::vector<double> angle, std::vector<double> direction, bool normalize, SpatOptions &opt);

SpatRaster selRange(SpatRaster x, int z, int recycleby, SpatOptions &opt);
Expand Down

0 comments on commit 69f3867

Please sign in to comment.