Skip to content

Commit 72c259f

Browse files
author
Dewey Dunnington
authored
Merge pull request #115 from r-spatial/dewey-dev
Expose edge tessellator as a wk filter
2 parents 8dbd305 + c56d300 commit 72c259f

File tree

12 files changed

+910
-0
lines changed

12 files changed

+910
-0
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ export(s2_perimeter)
151151
export(s2_point)
152152
export(s2_project)
153153
export(s2_project_normalized)
154+
export(s2_projection_filter)
155+
export(s2_projection_mercator)
156+
export(s2_projection_plate_carree)
154157
export(s2_rebuild)
155158
export(s2_rebuild_agg)
156159
export(s2_simplify)
@@ -164,6 +167,7 @@ export(s2_touches)
164167
export(s2_touches_matrix)
165168
export(s2_union)
166169
export(s2_union_agg)
170+
export(s2_unprojection_filter)
167171
export(s2_within)
168172
export(s2_within_matrix)
169173
export(s2_x)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# s2 (development version)
22

3+
* Added `s2_projection_filter()` and `s2_unprojection_filter()` to
4+
expose the S2 edge tessellator, which can be used to make Cartesian
5+
or great circle assumptions of line segments explicit by adding
6+
points where necessary (#115).
37
* Added an `s2_cell()` vector class to expose a subset of the S2
48
indexing system to R users (#85, #114).
59
* Added `s2_closest_edges()` to make k-nearest neighbours calculation

R/wk-utils.R

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#' Low-level wk filters and handlers
3+
#'
4+
#' @inheritParams wk::wk_handle
5+
#' @param projection One of [s2_projection_plate_carree()] or
6+
#' [s2_projection_mercator()]
7+
#' @param tessellate_tol An angle in radians. Points will not be added
8+
#' if a line segment is within this distance of a point.
9+
#'
10+
#' @return
11+
#' - `s2_unprojection_filter()`, `s2_projection_filter()`: A `new_wk_handler()`
12+
#' - `s2_projection_plate_carree()`, `s2_projection_mercator()`: An external pointer
13+
#' to an S2 projection.
14+
#' @export
15+
#'
16+
#' @examples
17+
#' library(wk)
18+
#'
19+
#' # simple conversion of individual coordinates *to* unit sphere
20+
#' # space
21+
#' wk_handle(
22+
#' wkt("LINESTRING (0 0, 0 45, -60 45)"),
23+
#' s2_unprojection_filter(wkt_format_handler(5))
24+
#' )
25+
#'
26+
#' # simple conversion of individual coordinates *from* unit sphere
27+
#' # space
28+
#' wk_handle(
29+
#' wkt("LINESTRING Z (1 0 0, 0.7071 0 0.7071, 0.3536 -0.6124 0.7071)"),
30+
#' s2_projection_filter(wkt_format_handler(5))
31+
#' )
32+
#'
33+
#' # use tessellate_tol to force points to be added to an edge
34+
#' # unprojection will ensure an edge maintains its cartesian
35+
#' # assumption when transformed to the unit sphere
36+
#' # (i.e., what you probably want when importing a geography)
37+
#' wk_handle(
38+
#' wkt("LINESTRING (0 0, 0 45, -60 45)"),
39+
#' s2_unprojection_filter(wkt_format_handler(5), tessellate_tol = 0.001)
40+
#' )
41+
#'
42+
#' # projection will ensure an edge maintains its geodesic
43+
#' # assumption when transformed to projected space
44+
#' # (i.e., what you probably want when exporting a geography)
45+
#' wk_handle(
46+
#' wkt("LINESTRING Z (1 0 0, 0.7071 0 0.7071, 0.3536 -0.6124 0.7071)"),
47+
#' s2_projection_filter(wkt_format_handler(5), tessellate_tol = 0.001)
48+
#' )
49+
#'
50+
s2_unprojection_filter <- function(handler, projection = s2_projection_plate_carree(),
51+
tessellate_tol = Inf) {
52+
wk::new_wk_handler(
53+
.Call(c_s2_coord_filter_new, handler, projection, TRUE, tessellate_tol),
54+
subclass = "s2_coord_filter"
55+
)
56+
}
57+
58+
#' @rdname s2_unprojection_filter
59+
#' @export
60+
s2_projection_filter <- function(handler, projection = s2_projection_plate_carree(),
61+
tessellate_tol = Inf) {
62+
wk::new_wk_handler(
63+
.Call(c_s2_coord_filter_new, handler, projection, FALSE, tessellate_tol),
64+
subclass = "s2_coord_filter"
65+
)
66+
}
67+
68+
#' @rdname s2_unprojection_filter
69+
#' @export
70+
s2_projection_plate_carree <- function() {
71+
.Call(c_s2_projection_plate_carree)
72+
}
73+
74+
#' @rdname s2_unprojection_filter
75+
#' @export
76+
s2_projection_mercator <- function() {
77+
.Call(c_s2_projection_mercator)
78+
}

_pkgdown.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ reference:
6666
contents:
6767
- s2_closest_feature
6868

69+
- title: Linear Referencing
70+
contents:
71+
- s2_interpolate
72+
6973
- title: Utility Functions
7074
contents:
7175
- s2_earth_radius_meters
@@ -75,3 +79,10 @@ reference:
7579
desc: Useful data for testing and demonstrating s2 functions
7680
contents:
7781
- starts_with("s2_data")
82+
83+
- title: Low-level Details
84+
desc: Interact with spherical geometry at a low level
85+
contents:
86+
- s2_cell
87+
- s2_cell_is_valid
88+
- s2_unprojection_filter

man/s2_unprojection_filter.Rd

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

src/Makevars.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ OBJECTS = cpp-compat.o \
1717
s2-matrix.o \
1818
s2-point.o \
1919
s2-xptr.o \
20+
wk-impl.o \
21+
wk-c-utils.o \
22+
s2-c-api.o \
2023
s2/base/stringprintf.o \
2124
s2/base/strtoint.o \
2225
s2/encoded_s2cell_id_vector.o \

src/Makevars.win

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ OBJECTS = cpp-compat.o \
1717
s2-matrix.o \
1818
s2-point.o \
1919
s2-xptr.o \
20+
wk-impl.o \
21+
wk-c-utils.o \
22+
s2-c-api.o \
2023
s2/base/stringprintf.o \
2124
s2/base/strtoint.o \
2225
s2/encoded_s2cell_id_vector.o \

src/RcppExports.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,10 @@ BEGIN_RCPP
12691269
END_RCPP
12701270
}
12711271

1272+
RcppExport SEXP c_s2_coord_filter_new(SEXP, SEXP, SEXP, SEXP);
1273+
RcppExport SEXP c_s2_projection_mercator();
1274+
RcppExport SEXP c_s2_projection_plate_carree();
1275+
12721276
static const R_CallMethodDef CallEntries[] = {
12731277
{"_s2_cpp_s2_init", (DL_FUNC) &_s2_cpp_s2_init, 0},
12741278
{"_s2_cpp_s2_is_collection", (DL_FUNC) &_s2_cpp_s2_is_collection, 1},
@@ -1375,6 +1379,9 @@ static const R_CallMethodDef CallEntries[] = {
13751379
{"_s2_cpp_s2_buffer_cells", (DL_FUNC) &_s2_cpp_s2_buffer_cells, 4},
13761380
{"_s2_s2_xptr_test", (DL_FUNC) &_s2_s2_xptr_test, 1},
13771381
{"_s2_s2_xptr_test_op", (DL_FUNC) &_s2_s2_xptr_test_op, 1},
1382+
{"c_s2_coord_filter_new", (DL_FUNC) &c_s2_coord_filter_new, 4},
1383+
{"c_s2_projection_mercator", (DL_FUNC) &c_s2_projection_mercator, 0},
1384+
{"c_s2_projection_plate_carree", (DL_FUNC) &c_s2_projection_plate_carree, 0},
13781385
{NULL, NULL, 0}
13791386
};
13801387

0 commit comments

Comments
 (0)