11
11
# ' @inheritParams s2_contains
12
12
# ' @param x,y Geography vectors, coerced using [as_s2_geography()].
13
13
# ' `x` is considered the source, where as `y` is considered the target.
14
+ # ' @param max_edges_per_cell For [s2_may_intersect_matrix()],
15
+ # ' this values controls the nature of the index on `y`, with higher values
16
+ # ' leading to coarser index. Values should be between 10 and 50; the default
17
+ # ' of 50 is adequate for most use cases, but for specialized operations users
18
+ # ' may wish to use a lower value to increase performance.
19
+ # ' @param max_feature_cells For [s2_may_intersect_matrix()], this value
20
+ # ' controls the approximation of `x` used to identify potential intersections
21
+ # ' on `y`. The default value of 4 gives the best performance for most operations,
22
+ # ' but for specialized operations users may wish to use a higher value to increase
23
+ # ' performance.
14
24
# '
15
25
# ' @return A vector of length `x`.
16
26
# ' @export
19
29
# ' See pairwise predicate functions (e.g., [s2_intersects()]).
20
30
# '
21
31
# ' @examples
22
- # ' city_names <- c("Vatican City", "San Marino", "Luxembourg", "Palikir" )
32
+ # ' city_names <- c("Vatican City", "San Marino", "Luxembourg")
23
33
# ' cities <- s2_data_cities(city_names)
24
34
# ' country_names <- s2_data_tbl_countries$name
25
35
# ' countries <- s2_data_countries()
32
42
# ' # for each feature in x
33
43
# ' country_names[s2_farthest_feature(cities, countries)]
34
44
# '
45
+ # ' # predicate matrices
46
+ # ' country_names[s2_intersects_matrix(cities, countries)[[1]]]
47
+ # '
35
48
# ' # distance matrices
36
49
# ' s2_distance_matrix(cities, cities)
37
50
# ' s2_max_distance_matrix(cities, countries[1:4])
@@ -60,13 +73,13 @@ s2_max_distance_matrix <- function(x, y, radius = s2_earth_radius_meters()) {
60
73
61
74
# ' @rdname s2_closest_feature
62
75
# ' @export
63
- s2_contains_matrix <- function (x , y , options = s2_options()) {
76
+ s2_contains_matrix <- function (x , y , options = s2_options(model = 0 )) {
64
77
cpp_s2_contains_matrix(as_s2_geography(x ), as_s2_geography(y ), options )
65
78
}
66
79
67
80
# ' @rdname s2_closest_feature
68
81
# ' @export
69
- s2_within_matrix <- function (x , y , options = s2_options()) {
82
+ s2_within_matrix <- function (x , y , options = s2_options(model = 0 )) {
70
83
cpp_s2_within_matrix(as_s2_geography(x ), as_s2_geography(y ), options )
71
84
}
72
85
@@ -91,7 +104,11 @@ s2_intersects_matrix <- function(x, y, options = s2_options()) {
91
104
# ' @rdname s2_closest_feature
92
105
# ' @export
93
106
s2_disjoint_matrix <- function (x , y , options = s2_options()) {
94
- cpp_s2_disjoint_matrix(as_s2_geography(x ), as_s2_geography(y ), options )
107
+ # disjoint is the odd one out, in that it requires a negation of intersects
108
+ # this is inconvenient to do on the C++ level, and is easier to maintain
109
+ # with setdiff() here (unless somebody complains that this is slow)
110
+ intersection <- cpp_s2_intersects_matrix(as_s2_geography(x ), as_s2_geography(y ), options )
111
+ Map(setdiff , list (seq_along(y )), intersection )
95
112
}
96
113
97
114
# ' @rdname s2_closest_feature
@@ -103,24 +120,51 @@ s2_equals_matrix <- function(x, y, options = s2_options()) {
103
120
# ' @rdname s2_closest_feature
104
121
# ' @export
105
122
s2_touches_matrix <- function (x , y , options = s2_options()) {
106
- x <- as_s2_geography(x )
107
- y <- as_s2_geography(y )
108
-
109
- options_closed <- options
110
- options_closed $ polygon_model <- 2
111
- options_closed $ polyline_model <- 2
112
-
113
- options_open <- options
114
- options_open $ polygon_model <- 0
115
- options_open $ polyline_model <- 0
116
-
117
- intersects_closed <- cpp_s2_intersects_matrix(x , y , options_closed )
118
- intersects_open <- cpp_s2_intersects_matrix(x , y , options_open )
119
- Map(setdiff , intersects_closed , intersects_open )
123
+ cpp_s2_touches_matrix(as_s2_geography(x ), as_s2_geography(y ), options )
120
124
}
121
125
122
126
# ' @rdname s2_closest_feature
123
127
# ' @export
124
128
s2_dwithin_matrix <- function (x , y , distance , radius = s2_earth_radius_meters()) {
125
129
cpp_s2_dwithin_matrix(as_s2_geography(x ), as_s2_geography(y ), distance / radius )
126
130
}
131
+
132
+ # ' @rdname s2_closest_feature
133
+ # ' @export
134
+ s2_may_intersect_matrix <- function (x , y , max_edges_per_cell = 50 , max_feature_cells = 4 ) {
135
+ cpp_s2_may_intersect_matrix(
136
+ as_s2_geography(x ), as_s2_geography(y ),
137
+ max_edges_per_cell , max_feature_cells ,
138
+ s2_options()
139
+ )
140
+ }
141
+
142
+ # ------- for testing, non-indexed versions of matrix operators -------
143
+
144
+ s2_contains_matrix_brute_force <- function (x , y , options = s2_options()) {
145
+ cpp_s2_contains_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
146
+ }
147
+
148
+ s2_within_matrix_brute_force <- function (x , y , options = s2_options()) {
149
+ cpp_s2_within_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
150
+ }
151
+
152
+ s2_covers_matrix_brute_force <- function (x , y , options = s2_options(model = 2 )) {
153
+ cpp_s2_contains_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
154
+ }
155
+
156
+ s2_covered_by_matrix_brute_force <- function (x , y , options = s2_options(model = 2 )) {
157
+ cpp_s2_within_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
158
+ }
159
+
160
+ s2_intersects_matrix_brute_force <- function (x , y , options = s2_options()) {
161
+ cpp_s2_intersects_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
162
+ }
163
+
164
+ s2_disjoint_matrix_brute_force <- function (x , y , options = s2_options()) {
165
+ cpp_s2_disjoint_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
166
+ }
167
+
168
+ s2_equals_matrix_brute_force <- function (x , y , options = s2_options()) {
169
+ cpp_s2_equals_matrix_brute_force(as_s2_geography(x ), as_s2_geography(y ), options )
170
+ }
0 commit comments