Skip to content

Commit 08cbc06

Browse files
authored
Merge pull request #63 from r-spatial/edzer-cap
add s2_cap and s2_lat_lng_rect functions
2 parents d1cf8b4 + 9cee17d commit 08cbc06

15 files changed

+240
-2
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export(s2_as_binary)
5353
export(s2_as_text)
5454
export(s2_boundary)
5555
export(s2_buffer_cells)
56+
export(s2_cap)
5657
export(s2_centroid)
5758
export(s2_centroid_agg)
5859
export(s2_closest_feature)
@@ -87,6 +88,7 @@ export(s2_intersects_box)
8788
export(s2_intersects_matrix)
8889
export(s2_is_collection)
8990
export(s2_is_empty)
91+
export(s2_lat_lng_rect)
9092
export(s2_latlng)
9193
export(s2_length)
9294
export(s2_make_line)

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ cpp_s2_max_distance <- function(geog1, geog2) {
4949
.Call(`_s2_cpp_s2_max_distance`, geog1, geog2)
5050
}
5151

52+
cpp_s2_cap <- function(geog) {
53+
.Call(`_s2_cpp_s2_cap`, geog)
54+
}
55+
56+
cpp_s2_lat_lng_rect <- function(geog) {
57+
.Call(`_s2_cpp_s2_lat_lng_rect`, geog)
58+
}
59+
5260
cpp_s2_geog_point <- function(x, y) {
5361
.Call(`_s2_cpp_s2_geog_point`, x, y)
5462
}

R/s2-cap.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
#' compute S2Cap or S2LatLngRect of each geography
4+
#'
5+
#' @param x object of class s2_geography
6+
#' @export
7+
#' @return Both functions return a \code{data.frame} with parameters;
8+
#' \code{s2_cap} returns for each geography a bounding spherical cap that contains the region.
9+
#' For points, polylines and geometry collections the bound may not be tight.
10+
#' @examples
11+
#' s2_cap(s2_data_countries("Antarctica"))
12+
#' s2_cap(s2_data_countries("Netherlands"))
13+
#' s2_cap(s2_data_countries("Fiji"))
14+
s2_cap <- function(x) {
15+
cpp_s2_cap(as_s2_geography(x))
16+
}
17+
18+
#' @name s2_cap
19+
#' @export
20+
#' @return \code{s2_lat_lng_rect} returns a bounding latitude-longitude
21+
#' rectangle that contains the region. The bound may not be tight for points,
22+
#' polylines and geometry collections. The rectangle returned may depend on
23+
#' the order of points or polylines. \code{lng_lo} values larger than \code{lng_hi} indicate
24+
#' regions that span the antimeridian, see the Fiji example.
25+
#' @examples
26+
#' s2_lat_lng_rect(s2_data_countries("Antarctica"))
27+
#' s2_lat_lng_rect(s2_data_countries("Netherlands"))
28+
#' s2_lat_lng_rect(s2_data_countries("Fiji"))
29+
s2_lat_lng_rect <- function(x) {
30+
cpp_s2_lat_lng_rect(as_s2_geography(x))
31+
}

man/as_s2_geography.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/s2_cap.Rd

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/s2_geog_point.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Makevars.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CXX_STD = CXX11
66
OBJECTS = cpp-compat.o \
77
s2-accessors.o \
88
s2-constructors-formatters.o \
9+
s2-cap.o \
910
s2-predicates.o \
1011
s2-transformers.o \
1112
init.o \

src/Makevars.win

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CXX_STD = CXX11
55

66
OBJECTS = cpp-compat.o \
77
s2-accessors.o \
8+
s2-cap.o \
89
s2-constructors-formatters.o \
910
s2-predicates.o \
1011
s2-transformers.o \

src/RcppExports.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ BEGIN_RCPP
137137
return rcpp_result_gen;
138138
END_RCPP
139139
}
140+
// cpp_s2_cap
141+
DataFrame cpp_s2_cap(List geog);
142+
RcppExport SEXP _s2_cpp_s2_cap(SEXP geogSEXP) {
143+
BEGIN_RCPP
144+
Rcpp::RObject rcpp_result_gen;
145+
Rcpp::RNGScope rcpp_rngScope_gen;
146+
Rcpp::traits::input_parameter< List >::type geog(geogSEXP);
147+
rcpp_result_gen = Rcpp::wrap(cpp_s2_cap(geog));
148+
return rcpp_result_gen;
149+
END_RCPP
150+
}
151+
// cpp_s2_lat_lng_rect
152+
DataFrame cpp_s2_lat_lng_rect(List geog);
153+
RcppExport SEXP _s2_cpp_s2_lat_lng_rect(SEXP geogSEXP) {
154+
BEGIN_RCPP
155+
Rcpp::RObject rcpp_result_gen;
156+
Rcpp::RNGScope rcpp_rngScope_gen;
157+
Rcpp::traits::input_parameter< List >::type geog(geogSEXP);
158+
rcpp_result_gen = Rcpp::wrap(cpp_s2_lat_lng_rect(geog));
159+
return rcpp_result_gen;
160+
END_RCPP
161+
}
140162
// cpp_s2_geog_point
141163
List cpp_s2_geog_point(NumericVector x, NumericVector y);
142164
RcppExport SEXP _s2_cpp_s2_geog_point(SEXP xSEXP, SEXP ySEXP) {
@@ -781,6 +803,8 @@ static const R_CallMethodDef CallEntries[] = {
781803
{"_s2_cpp_s2_y", (DL_FUNC) &_s2_cpp_s2_y, 1},
782804
{"_s2_cpp_s2_distance", (DL_FUNC) &_s2_cpp_s2_distance, 2},
783805
{"_s2_cpp_s2_max_distance", (DL_FUNC) &_s2_cpp_s2_max_distance, 2},
806+
{"_s2_cpp_s2_cap", (DL_FUNC) &_s2_cpp_s2_cap, 1},
807+
{"_s2_cpp_s2_lat_lng_rect", (DL_FUNC) &_s2_cpp_s2_lat_lng_rect, 1},
784808
{"_s2_cpp_s2_geog_point", (DL_FUNC) &_s2_cpp_s2_geog_point, 2},
785809
{"_s2_cpp_s2_make_line", (DL_FUNC) &_s2_cpp_s2_make_line, 3},
786810
{"_s2_cpp_s2_make_polygon", (DL_FUNC) &_s2_cpp_s2_make_polygon, 6},

src/geography.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include "s2/s2polyline.h"
88
#include "s2/s2polygon.h"
99
#include "s2/s2shape_index.h"
10+
#include "s2/s2shape_index_region.h"
1011
#include "s2/mutable_s2shape_index.h"
1112
#include "s2/s2point_vector_shape.h"
13+
#include "s2/s2cap.h"
1214
#include "wk/geometry-handler.hpp"
1315
#include <Rcpp.h>
1416

@@ -58,6 +60,19 @@ class Geography {
5860
return &this->shape_index_;
5961
}
6062

63+
virtual S2ShapeIndexRegion<S2ShapeIndex> ShapeIndexRegion() {
64+
S2ShapeIndex *ix = this->ShapeIndex();
65+
return MakeS2ShapeIndexRegion(ix);
66+
}
67+
68+
virtual S2Cap GetCapBound() {
69+
return this->ShapeIndexRegion().GetCapBound();
70+
}
71+
72+
virtual S2LatLngRect GetRectBound() {
73+
return this->ShapeIndexRegion().GetRectBound();
74+
}
75+
6176
protected:
6277
MutableS2ShapeIndex shape_index_;
6378
bool hasIndex;

0 commit comments

Comments
 (0)