R Buffer Selection – How to Select Non-Overlapping Buffers in R

bufferoverlapping-featuresrsf

I have a dataset with amenities. I've created a buffer of 200 meters around each amenity.

m_circles <- st_buffer(m, dist = 200)

It looks like this:

map

How can I keep only the buffers (polygons) that do not touch or overlap other buffers?

Best Answer

You can use a geometry predicate test, for example st_intersects, to see which features features from another feature set intersect with. With one argument you get a test of each feature with every feature including itself, so any feature that only intersects with one feature is intersecting with no other features. Subset on that.

Example:

> set.seed(1234)

Make 26 points in a unit square:

> pts = st_as_sf(data.frame(x=runif(26),y=runif(26),n=LETTERS), coords=1:2)

Make some buffers:

> bufs = st_buffer(pts, .05)
> plot(st_geometry(bufs))

enter image description here

Subset the buffers that don't intersect anything apart from themselves:

> nocross = bufs[lengths(st_intersects(bufs)) == 1,]
> plot(st_geometry(nocross), col="red", add=TRUE)

enter image description here

Note that this is equivalent to finding all points with nearest neighbour greater than the buffer distance away from them. If what you really want to do is find points where the nearest point is greater than some threshold then look at nearest neighbour computation packages which are computationally much more efficient than computing buffers and doing intersection tests.

Related Question