Skip to content

Commit

Permalink
Merge pull request #264 from r-spatial/buffer
Browse files Browse the repository at this point in the history
allow max_cells and min_level to be set by feature
  • Loading branch information
edzer authored Jan 7, 2025
2 parents b495b0d + e995c1c commit cc2150e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# s2 (development version)

* `s2_buffer_cells()` recycles `max_dist` and `min_level` arguments, allowing
to specify these by feature; #264 and https://github.com/r-spatial/sf/issues/2488

* The Abseil dependency is resolved using pkg-config where possible.
Where this is not possible, a vendored version of Abseil will be built using
CMake (#258).

# s2 1.1.7

# s2 1.1.6

* Fix CRAN warning (#254).
Expand Down
4 changes: 2 additions & 2 deletions R/s2-transformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ s2_rebuild <- function(x, options = s2_options()) {
#' @export
s2_buffer_cells <- function(x, distance, max_cells = 1000, min_level = -1,
radius = s2_earth_radius_meters()) {
recycled <- recycle_common(as_s2_geography(x), distance / radius)
new_s2_geography(cpp_s2_buffer_cells(recycled[[1]], recycled[[2]], max_cells, min_level))
recycled <- recycle_common(as_s2_geography(x), distance / radius, max_cells, min_level)
new_s2_geography(cpp_s2_buffer_cells(recycled[[1]], recycled[[2]], recycled[[3]], recycled[[4]]))
}

#' @rdname s2_boundary
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ nc_s2 %>%

## Acknowledgment

This project gratefully acknowledges financial [support](https://www.r-consortium.org/) from the
This project gratefully acknowledges financial [support](https://r-consortium.org/) from the

<a href="https://www.r-consortium.org/">
<a href="https://r-consortium.org/">
<img src="man/figures/rc300.png" width="300" />
</a>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ nc_s2 %>%
## Acknowledgment

This project gratefully acknowledges financial
[support](https://www.r-consortium.org/) from the
[support](https://r-consortium.org/) from the

<a href="https://www.r-consortium.org/">
<a href="https://r-consortium.org/">
<img src="man/figures/rc300.png" width="300" /> </a>
6 changes: 3 additions & 3 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,15 +1307,15 @@ BEGIN_RCPP
END_RCPP
}
// cpp_s2_buffer_cells
List cpp_s2_buffer_cells(List geog, NumericVector distance, int maxCells, int minLevel);
List cpp_s2_buffer_cells(List geog, NumericVector distance, IntegerVector maxCells, IntegerVector minLevel);
RcppExport SEXP _s2_cpp_s2_buffer_cells(SEXP geogSEXP, SEXP distanceSEXP, SEXP maxCellsSEXP, SEXP minLevelSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< List >::type geog(geogSEXP);
Rcpp::traits::input_parameter< NumericVector >::type distance(distanceSEXP);
Rcpp::traits::input_parameter< int >::type maxCells(maxCellsSEXP);
Rcpp::traits::input_parameter< int >::type minLevel(minLevelSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type maxCells(maxCellsSEXP);
Rcpp::traits::input_parameter< IntegerVector >::type minLevel(minLevelSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_s2_buffer_cells(geog, distance, maxCells, minLevel));
return rcpp_result_gen;
END_RCPP
Expand Down
15 changes: 9 additions & 6 deletions src/s2-transformers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,23 @@ List cpp_s2_interpolate_normalized(List geog, NumericVector distanceNormalized)
}

// [[Rcpp::export]]
List cpp_s2_buffer_cells(List geog, NumericVector distance, int maxCells, int minLevel) {
List cpp_s2_buffer_cells(List geog, NumericVector distance, IntegerVector maxCells, IntegerVector minLevel) {
class Op: public UnaryGeographyOperator<List, SEXP> {
public:
NumericVector distance;
IntegerVector maxCells, minLevel;
S2RegionCoverer coverer;

Op(NumericVector distance, int maxCells, int minLevel): distance(distance) {
this->coverer.mutable_options()->set_max_cells(maxCells);
if (minLevel > 0) {
this->coverer.mutable_options()->set_min_level(minLevel);
}
Op(NumericVector distance, IntegerVector maxC, IntegerVector minL): distance(distance) {
maxCells = maxC;
minLevel = minL;
}

SEXP processFeature(XPtr<RGeography> feature, R_xlen_t i) {
this->coverer.mutable_options()->set_max_cells(this->maxCells[i]);
if (this->minLevel[i] > 0) {
this->coverer.mutable_options()->set_min_level(this->minLevel[i]);
}
S2ShapeIndexBufferedRegion region;
region.Init(&feature->Index().ShapeIndex(), S1ChordAngle::Radians(this->distance[i]));

Expand Down

0 comments on commit cc2150e

Please sign in to comment.