A. Quinones
A. Quinones

Reputation: 69

How to select points that are outside a buffer in R?

I am using a point dataset of class sf and a road network of class sf. I created a buffer with the road network using the st_buffer() function and I can successfully select the points that are within the roads by using the following: points_within_roads <- st_intersection(points_shp, roads_buffer)

I need to do the opposite. I want to select the points that are outside the roads. Is there a function that allows me to do that? Thank you in advance.

Upvotes: 1

Views: 800

Answers (2)

Nathan Mack
Nathan Mack

Reputation: 77

Could use of ! answer this question? Below is assuming you have an unique identifier column of some sort (ID).

points_outside_buffer <- filter(points_shp, !points_within_roads$ID %in% points_shp$ID)

I just did the below with some data I am working on and it worked fine

too_far <- filter(stations_sf_r, !stations_sf_r$StationCode %in% repeats$StationCode)

Upvotes: 0

agila
agila

Reputation: 3472

You may want to check the sf::st_disjoint function. For example:

# packages
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

# create some fake data
set.seed(1234)
my_line <- st_linestring(rbind(c(-1, -1), c(1, 1)))
my_points <- st_cast(st_sfc(st_multipoint(matrix(runif(100, -1, 1), ncol = 2))), "POINT")
my_buffer <- st_buffer(my_line, 0.1)

# plot 
par(mar = rep(0, 4))
plot(st_boundary(my_buffer), col = "darkgrey")
plot(my_line, add = TRUE)
plot(my_points[my_buffer, op = st_intersects], add = TRUE, col = "darkred")
plot(my_points[my_buffer, op = st_disjoint], add = TRUE, col = "darkblue")

Created on 2020-04-29 by the reprex package (v0.3.0)

Moreover, I think you misspelt sf::st_intersection where it should be sf::st_intersects.

Upvotes: 2

Related Questions