File: s2-predicates.R

package info (click to toggle)
r-cran-s2 1.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,540 kB
  • sloc: cpp: 124,506; ansic: 2,639; pascal: 1,495; python: 354; sh: 139; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 5,686 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

#' S2 Geography Predicates
#'
#' These functions operate two geography vectors (pairwise), and return
#' a logical vector.
#'
#' @inheritParams s2_is_collection
#' @inheritParams s2_boundary
#' @param distance A distance on the surface of the earth in the same units
#'   as `radius`.
#' @param lng1,lat1,lng2,lat2 A latitude/longitude range
#' @param detail The number of points with which to approximate
#'   non-geodesic edges.
#'
#' @inheritSection s2_options Model
#'
#' @export
#'
#' @seealso
#' Matrix versions of these predicates (e.g., [s2_intersects_matrix()]).
#'
#' BigQuery's geography function reference:
#'
#' - [ST_CONTAINS](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_contains)
#' - [ST_COVEREDBY](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_coveredby)
#' - [ST_COVERS](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_covers)
#' - [ST_DISJOINT](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_disjoint)
#' - [ST_EQUALS](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_equals)
#' - [ST_INTERSECTS](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_intersects)
#' - [ST_INTERSECTSBOX](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_intersectsbox)
#' - [ST_TOUCHES](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_touches)
#' - [ST_WITHIN](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_within)
#' - [ST_DWITHIN](https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions#st_dwithin)
#'
#' @examples
#' s2_contains(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_within(
#'   c("POINT (5 5)", "POINT (-1 1)"),
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))"
#' )
#'
#' s2_covered_by(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_covers(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_disjoint(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_intersects(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_equals(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c(
#'     "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'     "POLYGON ((10 0, 10 10, 0 10, 0 0, 10 0))",
#'     "POLYGON ((-1 -1, 10 0, 10 10, 0 10, -1 -1))"
#'   )
#' )
#'
#' s2_intersects(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)")
#' )
#'
#' s2_intersects_box(
#'   c("POINT (5 5)", "POINT (-1 1)"),
#'   0, 0, 10, 10
#' )
#'
#' s2_touches(
#'   "POLYGON ((0 0, 0 1, 1 1, 0 0))",
#'   c("POINT (0 0)", "POINT (0.5 0.75)", "POINT (0 0.5)")
#' )
#'
#' s2_dwithin(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)"),
#'   0 # distance in meters
#' )
#'
#' s2_dwithin(
#'   "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
#'   c("POINT (5 5)", "POINT (-1 1)"),
#'   1e6 # distance in meters
#' )
#'
s2_contains <- function(x, y, options = s2_options(model = "open")) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y))
  cpp_s2_contains(recycled[[1]], recycled[[2]], options)
}

#' @rdname s2_contains
#' @export
s2_within <- function(x, y, options = s2_options(model = "open")) {
  s2_contains(y, x, options)
}

#' @rdname s2_contains
#' @export
s2_covered_by <- function(x, y, options = s2_options(model = "closed")) {
  s2_covers(y, x, options)
}

#' @rdname s2_contains
#' @export
s2_covers <- function(x, y, options = s2_options(model = "closed")) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y))
  cpp_s2_contains(recycled[[1]], recycled[[2]], options)
}

#' @rdname s2_contains
#' @export
s2_disjoint <- function(x, y, options = s2_options()) {
  !s2_intersects(x, y, options)
}

#' @rdname s2_contains
#' @export
s2_intersects <- function(x, y, options = s2_options()) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y))
  cpp_s2_intersects(recycled[[1]], recycled[[2]], options)
}

#' @rdname s2_contains
#' @export
s2_equals <- function(x, y, options = s2_options()) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y))
  cpp_s2_equals(recycled[[1]], recycled[[2]], options)
}

#' @rdname s2_contains
#' @export
s2_intersects_box <- function(x, lng1, lat1, lng2, lat2, detail = 1000, options = s2_options()) {
  recycled <- recycle_common(as_s2_geography(x), lng1, lat1, lng2, lat2, detail)
  cpp_s2_intersects_box(
    recycled[[1]],
    recycled[[2]], recycled[[3]],
    recycled[[4]], recycled[[5]],
    detail = recycled[[6]],
	  s2options = options
  )
}

#' @rdname s2_contains
#' @export
s2_touches <- function(x, y, options = s2_options()) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y))
  cpp_s2_touches(recycled[[1]], recycled[[2]], options)
}

#' @rdname s2_contains
#' @export
s2_dwithin <- function(x, y, distance, radius = s2_earth_radius_meters()) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y), distance / radius)
  cpp_s2_dwithin(recycled[[1]], recycled[[2]], recycled[[3]])
}

#' @rdname s2_contains
#' @export
s2_prepared_dwithin <- function(x, y, distance, radius = s2_earth_radius_meters()) {
  recycled <- recycle_common(as_s2_geography(x), as_s2_geography(y), distance / radius)
  cpp_s2_prepared_dwithin(recycled[[1]], recycled[[2]], recycled[[3]])
}