Each function compares every row of x against every row of y and returns
the row numbers of y for which the named DE-9IM relationship holds. This is
the sparse form of the pairwise predicates in ga_intersects(), and the
shape ga_join() needs.
Usage
ga_sparse_intersects(x, y)
ga_sparse_contains(x, y)
ga_sparse_contains_properly(x, y)
ga_sparse_within(x, y)
ga_sparse_covers(x, y)
ga_sparse_covered_by(x, y)
ga_sparse_touches(x, y)
ga_sparse_crosses(x, y)
ga_sparse_overlaps(x, y)
ga_sparse_equals_topo(x, y)Details
These functions do not compare the full cross product. Each function indexes
y in a packed Hilbert R-tree and runs the relate test only on the rows
whose bounding boxes overlap, so the cost scales with the number of
candidates rather than with length(x) * length(y).
Each function returns a zero length element, not a null, for a row of x
that matches nothing. It returns a null element for a null or empty geometry
in x, and never returns one in y.
ga_disjoint() has no sparse form. Disjointness is the one relationship you
cannot narrow with a bounding box, so the answer is almost every row of y
and the result is denser than the input.
Examples
nc <- as.data.frame(read_shapefile(
system.file("shape/nc.shp", package = "sf")
))
# which counties each county touches
nbrs <- as.vector(ga_sparse_touches(nc$geometry, nc$geometry))
nbrs[[1]]
#> [1] 2 18 19
# how many neighbours each has
summary(lengths(nbrs))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 2.0 4.0 5.0 4.9 6.0 9.0