GISR

Spatial selection with SF

Today we will show you how to use the sf library for spatial selection. For our example, we will use the roads and railways layer you are already familiar with. The example is again available as a video. Have fun watching it.

Code:

library(sf)

rail <- read_sf("d:/GIS_in_R/rail.shp")
road <- read_sf("d:/GIS_in_R/roads.shp")

plot(rail$geometry)
plot(road$geometry,col="red",add=T)

int <- st_intersection(rail,road)

plot(int$geometry,col="green",add=T,pch=20)

ind <- st_intersects(rail,road)
ind <- apply(ind, 1, any)
ind

plot(rail$geometry[ind])

Leave a Reply

Your email address will not be published. Required fields are marked *