Skip to content

Commit a24b7a1

Browse files
committed
Guard against unsupported operations in exact_resample
References #59
1 parent 55420d8 commit a24b7a1

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

R/exact_resample.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ setMethod('exact_resample',
4545
}
4646
}
4747

48+
if (length(fun) != 1) {
49+
stop("Only a single operation may be used for resampling.")
50+
}
51+
52+
if (startsWith(fun, 'weighted')) {
53+
stop("Weighted operations cannot be used for resampling.")
54+
}
55+
4856
x <- raster::readStart(x)
4957
tryCatch({
5058
CPP_resample(x, y, fun)

src/RcppExports.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
using namespace Rcpp;
77

8+
#ifdef RCPP_USE_GLOBAL_ROSTREAM
9+
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
10+
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
11+
#endif
12+
813
// CPP_coverage_fraction
914
Rcpp::S4 CPP_coverage_fraction(Rcpp::S4& rast, const Rcpp::RawVector& wkb, bool crop);
1015
RcppExport SEXP _exactextractr_CPP_coverage_fraction(SEXP rastSEXP, SEXP wkbSEXP, SEXP cropSEXP) {

src/resample.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ Rcpp::S4 CPP_resample(Rcpp::S4 & rast_in,
5656
Rcpp::Function rasterFn = raster["raster"];
5757
Rcpp::Function valuesFn = raster["values<-"];
5858

59-
if (stat.size() != 1) {
60-
Rcpp::stop("Only a single operation may be used for resampling.");
61-
}
62-
6359
S4RasterSource rsrc(rast_in);
6460

6561
Rcpp::S4 out = rasterFn(rast_out);

tests/testthat/test_exact_resample.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ test_that("error thrown if multiple stats provided", {
5151
'Only a single')
5252
})
5353

54+
test_that("error thrown if weighted stat provided", {
55+
r <- raster::raster(resolution = 2)
56+
target <- raster::shift(r, 2.5, 1)
57+
58+
expect_error(
59+
exact_resample(r, target, fun = "weighted_mean"),
60+
'cannot be used for resampling'
61+
)
62+
})
63+
5464
test_that("error thrown if rasters have different CRS", {
5565
src <- make_square_raster(1:100, crs='+init=epsg:4326')
5666
dst <- make_square_raster(1:100, crs='+init=epsg:4269')

0 commit comments

Comments
 (0)