Perform geometric set operations with simple feature geometry collections
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
object of class
object of class
tolerance values used for
A spatial index is built on argument
x; see http://r-spatial.org/r/2017/06/22/spatial-index.html. The reference for the STR tree algorithm is: Leutenegger, Scott T., Mario A. Lopez, and Jeffrey Edgington. "STR: A simple and efficient algorithm for R-tree packing." Data Engineering, 1997. Proceedings. 13th international conference on. IEEE, 1997. For the pdf, search Google Scholar.
When called with missing
sfc method for
st_intersection returns all non-empty intersections of the geometries of
x; an attribute
idx contains a list-column with the indexes of contributing geometries.
when called with a missing
sf method for
st_intersection returns an
sf object with attributes taken from the contributing feature with lowest index; two fields are added:
n.overlaps with the number of overlapping features in
x, and a list-column
origins with indexes of all overlapping features.
st_difference is called with a single argument,
overlapping areas are erased from geometries that are indexed at greater
numbers in the argument to
x; geometries that are empty
or contained fully inside geometries with higher priority are removed entirely.
st_difference.sfc method with a single argument returns an object with
"idx" attribute with the orginal index for returned geometries.
The intersection, difference or symmetric difference between two sets of geometries.
The returned object has the same class as that of the first argument (
x) with the non-empty geometries resulting from applying the operation to all geometry pairs in
y. In case
x is of class
sf, the matching attributes of the original object(s) are added. The
sfc geometry list-column returned carries an attribute
idx, which is an
n-by-2 matrix with every row the index of the corresponding entries of
To find whether pairs of simple feature geometries intersect, use
st_intersects instead of
st_union for the union of simple features collections; intersect and setdiff for the base R set operations.
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
set.seed(131) library(sf) m = rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)) p = st_polygon(list(m)) n = 100 l = vector("list", n) for (i in 1:n) l[[i]] = p + 10 * runif(2) s = st_sfc(l) plot(s, col = sf.colors(categorical = TRUE, alpha = .5)) title("overlapping squares") d = st_difference(s) # sequential differences: s1, s2-s1, s3-s2-s1, ... plot(d, col = sf.colors(categorical = TRUE, alpha = .5)) title("non-overlapping differences") i = st_intersection(s) # all intersections plot(i, col = sf.colors(categorical = TRUE, alpha = .5)) title("non-overlapping intersections") summary(lengths(st_overlaps(s, s))) # includes self-counts! summary(lengths(st_overlaps(d, d))) summary(lengths(st_overlaps(i, i))) sf = st_sf(s) i = st_intersection(sf) # all intersections plot(i["n.overlaps"]) summary(i$n.overlaps - lengths(i$origins)) # A helper function that erases all of y from x: st_erase = function(x, y) st_difference(x, st_union(st_combine(y)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.