yuliaUU
yuliaUU

Reputation: 1713

How to intersect two sf objects?

I have downloaded two .gdb files from here:

I saved both folders into data folder and unziped them

library(sf)

lots <- st_read("data/Lots_GDB/lots.gdb",layer= "lot")
lots <-st_transform(st_cast(lots , "MULTIPOLYGON"), "+proj=longlat +datum=WGS84")

buildings <- st_read("data/buildings_gdb/buildings.gdb",layer= "buildings")
buildings <-st_transform(st_cast(buildings , "MULTIPOLYGON"), "+proj=longlat +datum=WGS84") %>%  select( FACILITYID,FACILITY_TYPE, STATUS)

What I would like to obtain:

  1. I would like to leave only polygons from object lots that do not have inside them polygons from building objects ( ie I want lots with no buildings)
  2. same thing , but also get only lots that do have buildings inside them.

I used st_intersection() to overlap two objects

lots_building<- st_intersection(lots, buildings)

And then plot it on teh map:

ggmap() +
  geom_sf(data= lots_building,  alpha=0.9, inherit.aes = FALSE)

But somehow lots_building does not seem to be the right feature to achieve the task.

Upvotes: 0

Views: 1315

Answers (1)

yuliaUU
yuliaUU

Reputation: 1713

I tried many things, but this one is the only thing that worked:

diffPoly <-lots[lengths(st_intersects(lots,buildings))==0,]

Upvotes: 2

Related Questions