Skip to content

Commit a93a499

Browse files
committed
Separate binary operations documentation.
1 parent 7dd5448 commit a93a499

File tree

9 files changed

+271
-113
lines changed

9 files changed

+271
-113
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Collate:
133133
'wkb.R'
134134
'wkt.R'
135135
'plot.R'
136+
'geos_binary_pred.R'
136137
'geom-measures.R'
137138
'geom-predicates.R'
138139
'geom-transformers.R'

R/geom-predicates.R

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ st_relate = function(x, y, pattern = NA_character_, sparse = !is.na(pattern)) {
107107
st_geos_binop("relate", x, y, sparse = FALSE)
108108
}
109109

110-
#' Geometric binary predicates on pairs of simple feature geometry sets
110+
#' Identify if `x` and `y` share any space
111111
#'
112-
#' Geometric binary predicates on pairs of simple feature geometry sets
113-
#' @name geos_binary_pred
112+
#'
114113
#' @param x object of class \code{sf}, \code{sfc} or \code{sfg}
115114
#' @param y object of class \code{sf}, \code{sfc} or \code{sfg}; if missing, \code{x} is used
116115
#' @param sparse logical; should a sparse index list be returned (`TRUE`) or a dense logical matrix? See below.
@@ -121,12 +120,13 @@ st_relate = function(x, y, pattern = NA_character_, sparse = !is.na(pattern)) {
121120
#' @details For most predicates, a spatial index is built on argument \code{x}; see \url{https://r-spatial.org/r/2017/06/22/spatial-index.html}.
122121
#' Specifically, \code{st_intersects}, \code{st_disjoint}, \code{st_touches} \code{st_crosses}, \code{st_within}, \code{st_contains}, \code{st_contains_properly}, \code{st_overlaps}, \code{st_equals}, \code{st_covers} and \code{st_covered_by} all build spatial indexes for more efficient geometry calculations. \code{st_relate}, \code{st_equals_exact}, and do not; \code{st_is_within_distance} uses a spatial index for geographic coordinates when \code{sf_use_s2()} is true.
123122
#'
124-
#' If \code{y} is missing, `st_predicate(x, x)` is effectively called, and a square matrix is returned with diagonal elements `st_predicate(x[i], x[i])`.
123+
#' If \code{y} is missing, `st_<predicate>(x, x)` is effectively called, and a square matrix is returned with diagonal elements `st_predicate(x[i], x[i])`.
125124
#'
126125
#' Sparse geometry binary predicate (\code{\link{sgbp}}) lists have the following attributes: \code{region.id} with the \code{row.names} of \code{x} (if any, else \code{1:n}), \code{ncol} with the number of features in \code{y}, and \code{predicate} with the name of the predicate used.
127126
#'
128127
#' @note For intersection on pairs of simple feature geometries, use
129128
#' the function \code{\link{st_intersection}} instead of \code{st_intersects}.
129+
#' @family geometric binary predicates for two spatial objects
130130
#'
131131
#' @examples
132132
#' pts = st_sfc(st_point(c(.5,.5)), st_point(c(1.5, 1.5)), st_point(c(2.5, 2.5)))
@@ -193,18 +193,22 @@ st_crosses = function(x, y, sparse = TRUE, prepared = TRUE, ...)
193193
st_within = function(x, y, sparse = TRUE, prepared = TRUE, ...)
194194
st_geos_binop("within", x, y, sparse = sparse, prepared = prepared, ...)
195195

196-
#' @name geos_binary_pred
196+
#' Identify if y is within x
197+
#'
198+
#' * `st_contains()` is true if
199+
#' * `st_contains_properly(x, y)` is true if `x` intersects `y`'s interior, but not its edges or exterior; `x` contains `x`, but `x` does not properly contain `x`.
197200
#' @param model character; polygon/polyline model; one of
198201
#' "open", "semi-open" or "closed"; see Details.
199202
#' @details for \code{model}, see https://github.com/r-spatial/s2/issues/32
203+
#' @inheritParams st_intersects
200204
#' @export
205+
#' @family geometric binary predicates for two spatial objects
201206
st_contains = function(x, y, sparse = TRUE, prepared = TRUE, ..., model = "open")
202207
st_geos_binop("contains", x, y, sparse = sparse, prepared = prepared, ..., model = model)
203208

204-
#' @name geos_binary_pred
209+
#' @rdname st_contains
205210
#' @export
206-
#' @details `st_contains_properly(A,B)` is true if A intersects B's interior, but not its edges or exterior; A contains A, but A does not properly contain A.
207-
#'
211+
#' @details
208212
#' See also \link{st_relate} and \url{https://en.wikipedia.org/wiki/DE-9IM} for a more detailed description of the underlying algorithms.
209213
st_contains_properly = function(x, y, sparse = TRUE, prepared = TRUE, ...) {
210214
if (! prepared)
@@ -217,10 +221,20 @@ st_contains_properly = function(x, y, sparse = TRUE, prepared = TRUE, ...) {
217221
st_overlaps = function(x, y, sparse = TRUE, prepared = TRUE, ...)
218222
st_geos_binop("overlaps", x, y, sparse = sparse, prepared = prepared, ...)
219223

220-
#' @name geos_binary_pred
221-
#' @param retain_unique logical; if `TRUE` (and `y` is missing) return only indexes of points larger than the current index; this can be used to select unique geometries, see examples. This argument can be used for all geometry predicates; see also \link{distinct.sf} to find records where geometries AND attributes are distinct.
224+
225+
#' Verify if geographies are equal
226+
#'
227+
#' * `st_equals()` validate if x and y are equal.
228+
#' * `st_equals_exact()` returns true for two geometries of the same type and their vertices corresponding by index are equal up to a specified tolerance.
229+
#'
230+
#' @inheritParams st_intersects
231+
#' @param retain_unique logical; if `TRUE` (and `y` is missing) return only
232+
#' indexes of points larger than the current index; this can be used to select
233+
#' unique geometries, see examples. This argument can be used for all geometry predicates;
234+
#' see also \link{distinct.sf} to find records where geometries AND attributes are distinct.
222235
#' @param remove_self logical; if `TRUE` (and `y` is missing) return only indexes of geometries different from the current index; this can be used to omit self-intersections; see examples. This argument can be used for all geometry predicates
223236
#' @export
237+
#' @family geometric binary predicates for two spatial objects
224238
st_equals = function(x, y, sparse = TRUE, prepared = FALSE, ...,
225239
retain_unique = FALSE, remove_self = FALSE) {
226240
if (prepared)
@@ -241,10 +255,9 @@ st_covered_by = function(x, y = x, sparse = TRUE, prepared = TRUE, ..., model =
241255
st_geos_binop("covered_by", x, y, sparse = sparse, prepared = prepared, ...)
242256

243257

244-
#' @name geos_binary_pred
258+
#' @rdname st_equals
245259
#' @export
246260
#' @param par numeric; parameter used for "equals_exact" (margin);
247-
#' @details \code{st_equals_exact} returns true for two geometries of the same type and their vertices corresponding by index are equal up to a specified tolerance.
248261
st_equals_exact = function(x, y, par, sparse = TRUE, prepared = FALSE, ...) {
249262
if (prepared)
250263
stop("prepared geometries not supported for st_equals_exact")

R/geos_binary_pred.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#' Geometric binary predicates on pairs of simple feature geometry sets
2+
#'
3+
#' @description
4+
#'
5+
#' For most predicates, a spatial index is built on argument `x`;
6+
#' see \url{https://r-spatial.org/r/2017/06/22/spatial-index.html}.
7+
#'
8+
#' If `prepared = TRUE`, `x` contains POINT geometries, and `y` contains polygons,
9+
#' then the polygon geometries are prepared, rather than the points.
10+
#' @name geos_binary_pred
11+
#' @family geometric binary predicates for two spatial objects
12+
#' @examples
13+
#' pts = st_sfc(st_point(c(.5,.5)), st_point(c(1.5, 1.5)), st_point(c(2.5, 2.5)))
14+
#' pol = st_polygon(list(rbind(c(0,0), c(2,0), c(2,2), c(0,2), c(0,0))))
15+
#' (lst = st_intersects(pts, pol))
16+
#' (mat = st_intersects(pts, pol, sparse = FALSE))
17+
#' # which points fall inside a polygon?
18+
#' apply(mat, 1, any)
19+
#' lengths(lst) > 0
20+
#' # which points fall inside the first polygon?
21+
#' st_intersects(pol, pts)[[1]]
22+
#' # remove duplicate geometries:
23+
#' p1 = st_point(0:1)
24+
#' p2 = st_point(2:1)
25+
#' p = st_sf(a = letters[1:8], geom = st_sfc(p1, p1, p2, p1, p1, p2, p2, p1))
26+
#' st_equals(p)
27+
#' st_equals(p, remove_self = TRUE)
28+
#' (u = st_equals(p, retain_unique = TRUE))
29+
#' # retain the records with unique geometries:
30+
#' p[-unlist(u),]
31+
NULL

R/sgbp.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ sgbp = function(x, predicate, region.id, ncol, sparse = TRUE, remove_self = FALS
2121
ret
2222
}
2323

24-
#' Methods for dealing with sparse geometry binary predicate lists
24+
#' Sparse Geometry Binary Predicate Lists.
2525
#'
26+
#' @description
2627
#' Methods for dealing with sparse geometry binary predicate lists
28+
#' \code{sgbp} are sparse matrices, stored as a list with integer vectors holding
29+
#' the ordered \code{TRUE} indices of each row. This means that for a dense,
30+
#' \eqn{m \times n}{m x n} matrix \code{Q} and a list \code{L},
31+
#' if \code{Q[i,j]} is \code{TRUE} then \eqn{j} is an element of \code{L[[i]]}.
32+
#'
33+
#' Reversed: when \eqn{k} is the value of \code{L[[i]][j]}, then \code{Q[i,k]} is \code{TRUE}.
2734
#' @name sgbp
2835
#' @export
2936
#' @param x object of class \code{sgbp}
3037
#' @param ... ignored
3138
#' @param n integer; maximum number of items to print
3239
#' @param max_nb integer; maximum number of neighbours to print for each item
33-
#' @details \code{sgbp} are sparse matrices, stored as a list with integer vectors holding the ordered \code{TRUE} indices of each row. This means that for a dense, \eqn{m \times n}{m x n} matrix \code{Q} and a list \code{L}, if \code{Q[i,j]} is \code{TRUE} then \eqn{j} is an element of \code{L[[i]]}. Reversed: when \eqn{k} is the value of \code{L[[i]][j]}, then \code{Q[i,k]} is \code{TRUE}.
3440
print.sgbp = function(x, ..., n = 10, max_nb = 10) {
3541
n = min(length(x), n)
3642
hd = paste0("Sparse geometry binary predicate list of length ", length(x), ", ",

man/geos_binary_pred.Rd

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

man/sgbp.Rd

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

man/st_contains.Rd

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

0 commit comments

Comments
 (0)