Skip to content

Commit

Permalink
Reduce overhead of extent and res calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jun 27, 2022
1 parent 6523ad5 commit 5b88ecf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
8 changes: 4 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ CPP_coverage_fraction <- function(rast, wkb, crop) {
.Call('_exactextractr_CPP_coverage_fraction', PACKAGE = 'exactextractr', rast, wkb, crop)
}

CPP_exact_extract <- function(rast, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol) {
.Call('_exactextractr_CPP_exact_extract', PACKAGE = 'exactextractr', rast, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol)
CPP_exact_extract <- function(rast, rast_ext, rast_res, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol) {
.Call('_exactextractr_CPP_exact_extract', PACKAGE = 'exactextractr', rast, rast_ext, rast_res, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol)
}

CPP_stats <- function(rast, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles) {
.Call('_exactextractr_CPP_stats', PACKAGE = 'exactextractr', rast, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles)
CPP_stats <- function(rast, rast_ext, rast_res, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles) {
.Call('_exactextractr_CPP_stats', PACKAGE = 'exactextractr', rast, rast_ext, rast_res, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles)
}

CPP_update_max_coverage <- function(extent, res, max_coverage, max_coverage_index, tot_coverage, wkb, index) {
Expand Down
8 changes: 7 additions & 1 deletion R/exact_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ NULL
weights <- .eagerLoad(weights, geoms, max_cells_in_memory, message_on_fail = progress)
}

# Calculate extent and resolution outside of loop for performance (yes, really)
x_ext <- .extent(x)
x_res <- .res(x)

# At this point, if the data have not been preloaded into memory, either:
# - we don't have the terra package available
# - we can't fit the whole processing area in memory
Expand Down Expand Up @@ -497,7 +501,7 @@ NULL
}

results <- lapply(sf::st_as_binary(geoms, EWKB=TRUE), function(wkb) {
ret <- CPP_stats(x, weights, wkb, default_value, default_weight, coverage_area, area_method, fun, max_cells_in_memory, grid_compat_tol, quantiles)
ret <- CPP_stats(x, x_ext, x_res, weights, wkb, default_value, default_weight, coverage_area, area_method, fun, max_cells_in_memory, grid_compat_tol, quantiles)
update_progress()
return(ret)
})
Expand Down Expand Up @@ -590,6 +594,8 @@ NULL
warn_on_disaggregate <- feature_num == 1

col_list <- CPP_exact_extract(x,
x_ext,
x_res,
x_orig,
weights,
wkb,
Expand Down
2 changes: 1 addition & 1 deletion R/exact_extract_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
if (inherits(r, 'BasicRaster')) {
return(raster::inMemory(r))
} else if (inherits(r, 'SpatRaster')) {
return(terra::inMemory(r[[1]]))
return(terra::inMemory(r)[1])
} else {
stop('Unknown type: ', class(r))
}
Expand Down
20 changes: 12 additions & 8 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ BEGIN_RCPP
END_RCPP
}
// CPP_exact_extract
Rcpp::List CPP_exact_extract(Rcpp::S4& rast, Rcpp::Nullable<Rcpp::S4>& rast_uncropped, Rcpp::Nullable<Rcpp::S4>& weights, const Rcpp::RawVector& wkb, double default_value, double default_weight, bool include_xy, bool include_cell_number, bool include_area, bool area_weights, bool coverage_areas, Rcpp::Nullable<Rcpp::CharacterVector>& p_area_method, Rcpp::Nullable<Rcpp::List>& include_cols, Rcpp::CharacterVector& src_names, Rcpp::Nullable<Rcpp::CharacterVector>& p_weights_names, bool warn_on_disaggregate, double grid_compat_tol);
RcppExport SEXP _exactextractr_CPP_exact_extract(SEXP rastSEXP, SEXP rast_uncroppedSEXP, SEXP weightsSEXP, SEXP wkbSEXP, SEXP default_valueSEXP, SEXP default_weightSEXP, SEXP include_xySEXP, SEXP include_cell_numberSEXP, SEXP include_areaSEXP, SEXP area_weightsSEXP, SEXP coverage_areasSEXP, SEXP p_area_methodSEXP, SEXP include_colsSEXP, SEXP src_namesSEXP, SEXP p_weights_namesSEXP, SEXP warn_on_disaggregateSEXP, SEXP grid_compat_tolSEXP) {
Rcpp::List CPP_exact_extract(Rcpp::S4& rast, const Rcpp::NumericVector& rast_ext, const Rcpp::NumericVector& rast_res, Rcpp::Nullable<Rcpp::S4>& rast_uncropped, Rcpp::Nullable<Rcpp::S4>& weights, const Rcpp::RawVector& wkb, double default_value, double default_weight, bool include_xy, bool include_cell_number, bool include_area, bool area_weights, bool coverage_areas, Rcpp::Nullable<Rcpp::CharacterVector>& p_area_method, Rcpp::Nullable<Rcpp::List>& include_cols, Rcpp::CharacterVector& src_names, Rcpp::Nullable<Rcpp::CharacterVector>& p_weights_names, bool warn_on_disaggregate, double grid_compat_tol);
RcppExport SEXP _exactextractr_CPP_exact_extract(SEXP rastSEXP, SEXP rast_extSEXP, SEXP rast_resSEXP, SEXP rast_uncroppedSEXP, SEXP weightsSEXP, SEXP wkbSEXP, SEXP default_valueSEXP, SEXP default_weightSEXP, SEXP include_xySEXP, SEXP include_cell_numberSEXP, SEXP include_areaSEXP, SEXP area_weightsSEXP, SEXP coverage_areasSEXP, SEXP p_area_methodSEXP, SEXP include_colsSEXP, SEXP src_namesSEXP, SEXP p_weights_namesSEXP, SEXP warn_on_disaggregateSEXP, SEXP grid_compat_tolSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::S4& >::type rast(rastSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type rast_ext(rast_extSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type rast_res(rast_resSEXP);
Rcpp::traits::input_parameter< Rcpp::Nullable<Rcpp::S4>& >::type rast_uncropped(rast_uncroppedSEXP);
Rcpp::traits::input_parameter< Rcpp::Nullable<Rcpp::S4>& >::type weights(weightsSEXP);
Rcpp::traits::input_parameter< const Rcpp::RawVector& >::type wkb(wkbSEXP);
Expand All @@ -46,17 +48,19 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< Rcpp::Nullable<Rcpp::CharacterVector>& >::type p_weights_names(p_weights_namesSEXP);
Rcpp::traits::input_parameter< bool >::type warn_on_disaggregate(warn_on_disaggregateSEXP);
Rcpp::traits::input_parameter< double >::type grid_compat_tol(grid_compat_tolSEXP);
rcpp_result_gen = Rcpp::wrap(CPP_exact_extract(rast, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol));
rcpp_result_gen = Rcpp::wrap(CPP_exact_extract(rast, rast_ext, rast_res, rast_uncropped, weights, wkb, default_value, default_weight, include_xy, include_cell_number, include_area, area_weights, coverage_areas, p_area_method, include_cols, src_names, p_weights_names, warn_on_disaggregate, grid_compat_tol));
return rcpp_result_gen;
END_RCPP
}
// CPP_stats
Rcpp::NumericMatrix CPP_stats(Rcpp::S4& rast, Rcpp::Nullable<Rcpp::S4> weights, const Rcpp::RawVector& wkb, double default_value, double default_weight, bool coverage_areas, Rcpp::Nullable<Rcpp::CharacterVector>& p_area_method, const Rcpp::StringVector& stats, int max_cells_in_memory, double grid_compat_tol, const Rcpp::Nullable<Rcpp::NumericVector>& quantiles);
RcppExport SEXP _exactextractr_CPP_stats(SEXP rastSEXP, SEXP weightsSEXP, SEXP wkbSEXP, SEXP default_valueSEXP, SEXP default_weightSEXP, SEXP coverage_areasSEXP, SEXP p_area_methodSEXP, SEXP statsSEXP, SEXP max_cells_in_memorySEXP, SEXP grid_compat_tolSEXP, SEXP quantilesSEXP) {
Rcpp::NumericMatrix CPP_stats(Rcpp::S4& rast, const Rcpp::NumericVector& rast_ext, const Rcpp::NumericVector& rast_res, Rcpp::Nullable<Rcpp::S4> weights, const Rcpp::RawVector& wkb, double default_value, double default_weight, bool coverage_areas, Rcpp::Nullable<Rcpp::CharacterVector>& p_area_method, const Rcpp::StringVector& stats, int max_cells_in_memory, double grid_compat_tol, const Rcpp::Nullable<Rcpp::NumericVector>& quantiles);
RcppExport SEXP _exactextractr_CPP_stats(SEXP rastSEXP, SEXP rast_extSEXP, SEXP rast_resSEXP, SEXP weightsSEXP, SEXP wkbSEXP, SEXP default_valueSEXP, SEXP default_weightSEXP, SEXP coverage_areasSEXP, SEXP p_area_methodSEXP, SEXP statsSEXP, SEXP max_cells_in_memorySEXP, SEXP grid_compat_tolSEXP, SEXP quantilesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::S4& >::type rast(rastSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type rast_ext(rast_extSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type rast_res(rast_resSEXP);
Rcpp::traits::input_parameter< Rcpp::Nullable<Rcpp::S4> >::type weights(weightsSEXP);
Rcpp::traits::input_parameter< const Rcpp::RawVector& >::type wkb(wkbSEXP);
Rcpp::traits::input_parameter< double >::type default_value(default_valueSEXP);
Expand All @@ -67,7 +71,7 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< int >::type max_cells_in_memory(max_cells_in_memorySEXP);
Rcpp::traits::input_parameter< double >::type grid_compat_tol(grid_compat_tolSEXP);
Rcpp::traits::input_parameter< const Rcpp::Nullable<Rcpp::NumericVector>& >::type quantiles(quantilesSEXP);
rcpp_result_gen = Rcpp::wrap(CPP_stats(rast, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles));
rcpp_result_gen = Rcpp::wrap(CPP_stats(rast, rast_ext, rast_res, weights, wkb, default_value, default_weight, coverage_areas, p_area_method, stats, max_cells_in_memory, grid_compat_tol, quantiles));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -106,8 +110,8 @@ END_RCPP

static const R_CallMethodDef CallEntries[] = {
{"_exactextractr_CPP_coverage_fraction", (DL_FUNC) &_exactextractr_CPP_coverage_fraction, 3},
{"_exactextractr_CPP_exact_extract", (DL_FUNC) &_exactextractr_CPP_exact_extract, 17},
{"_exactextractr_CPP_stats", (DL_FUNC) &_exactextractr_CPP_stats, 11},
{"_exactextractr_CPP_exact_extract", (DL_FUNC) &_exactextractr_CPP_exact_extract, 19},
{"_exactextractr_CPP_stats", (DL_FUNC) &_exactextractr_CPP_stats, 13},
{"_exactextractr_CPP_update_max_coverage", (DL_FUNC) &_exactextractr_CPP_update_max_coverage, 7},
{"_exactextractr_CPP_resample", (DL_FUNC) &_exactextractr_CPP_resample, 6},
{NULL, NULL, 0}
Expand Down
8 changes: 6 additions & 2 deletions src/exact_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ using exactextract::RasterSource;

// [[Rcpp::export]]
Rcpp::List CPP_exact_extract(Rcpp::S4 & rast,
const Rcpp::NumericVector & rast_ext,
const Rcpp::NumericVector & rast_res,
Rcpp::Nullable<Rcpp::S4> & rast_uncropped,
Rcpp::Nullable<Rcpp::S4> & weights,
const Rcpp::RawVector & wkb,
Expand All @@ -68,7 +70,7 @@ Rcpp::List CPP_exact_extract(Rcpp::S4 & rast,
auto weights_grid = exactextract::Grid<bounded_extent>::make_empty();
auto common_grid = grid;

S4RasterSource rsrc(rast, default_value);
S4RasterSource rsrc(rast, rast_ext, rast_res, default_value);
int src_nlayers = get_nlayers(rast);

std::unique_ptr<S4RasterSource> rweights;
Expand Down Expand Up @@ -262,6 +264,8 @@ static int get_num_stats(const Rcpp::StringVector & stats,
// Return a matrix with one row per stat and one row per raster layer
// [[Rcpp::export]]
Rcpp::NumericMatrix CPP_stats(Rcpp::S4 & rast,
const Rcpp::NumericVector & rast_ext,
const Rcpp::NumericVector & rast_res,
Rcpp::Nullable<Rcpp::S4> weights,
const Rcpp::RawVector & wkb,
double default_value,
Expand All @@ -281,7 +285,7 @@ Rcpp::NumericMatrix CPP_stats(Rcpp::S4 & rast,

int nlayers = get_nlayers(rast);

S4RasterSource rsrc(rast, default_value);
S4RasterSource rsrc(rast, rast_ext, rast_res, default_value);

std::unique_ptr<S4RasterSource> rweights;
std::string area_method;
Expand Down
15 changes: 15 additions & 0 deletions src/s4_raster_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ class S4RasterSource {
m_grid = make_grid(rast);
}

S4RasterSource(SEXP rast,
const Rcpp::NumericVector & ext,
const Rcpp::NumericVector & res,
double default_value = std::numeric_limits<double>::quiet_NaN()) :
m_grid(exactextract::Grid<exactextract::bounded_extent>::make_empty()),
m_rast(rast),
m_last_box(std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN(),
std::numeric_limits<double>::quiet_NaN()),
m_default_value(default_value)
{
m_grid = make_grid(ext, res);
}

const exactextract::Grid<exactextract::bounded_extent> &grid() const {
return m_grid;
}
Expand Down

0 comments on commit 5b88ecf

Please sign in to comment.