par(mar =rep(0, 4))# plot(districts)# plot(districts$geometry, col = "grey")plot(st_geometry(districts), col ="grey")plot(pol, col ="purple", add =TRUE)plot(roads_rpj, col ='red', add =TRUE)
st_intersects
Similar to “Select by Spatial Location”
Finds indices of second object that intersect each element of first object
st_intersects(x = roads_rpj, y = districts)
st_intersects
highway <- roads_rpj[401, ]dists_int <- districts %>%slice(st_intersects(x = highway, y = .)[[1]])# unlist(st_intersects(x = highway, y = districts))#> although coordinates are longitude/latitude, st_intersects assumes that they are planardists_intplot(st_geometry(dists_int), col ='grey')plot(highway, col ='red', add =TRUE)
st_intersection()
finds intersecting pairs between two objects
Each row in result is one “intersection object” (i.e. an object from x and y that intersect)
pol_int_dists <-st_intersection(x = pol, y = districts)par(mfrow =c(1, 2), mar =rep(0, 4))plot(st_geometry(districts), col ="grey")plot(st_geometry(pol_int_dists), col =rainbow(n =nrow(pol_int_dists)), add =TRUE)plot(st_geometry(districts), col ="grey")districts %>%slice(unlist(st_intersects(x = pol, y = .))) %>%plot(add =TRUE)
st_difference
Similar to “erase” in ArcGIS
Returns first object without second object.
d1 <-st_difference(x = districts, y = pol)d2 <-st_difference(x = pol, y = districts)par(mfrow =c(1, 2), mar =c(0, 0, 1, 0))d1 %>% st_geometry %>%plot(col ="grey")d2 %>% st_geometry %>%plot(col ="grey")
st_union
Combines objects without resolving borders.
Really takes all possible combinations of first and 2nd object
Switching order of inputs changes column order
uni1 <-st_union(x = pol, y = districts)uni1uni2 <-st_union(x = districts, y = pol)uni2par(mfrow =c(1, 2), mar =c(0, 0, 1, 0))plot(uni1[2], reset =FALSE, main ="Union of pol with districts")plot(uni2[1], reset =FALSE, main ="Union of districts with pol")
st_buffer()
Best to use projected coordinates (e.g. Albers)
## project everything to Albersfarmers_alb <- farmers_sf %>%st_transform(st_crs(roads))long_roads <- roads %>%filter(as.numeric(st_length(.)) >500000)districts_alb <- districts %>%st_transform(st_crs(roads))roads_buff_20km <-st_buffer(long_roads, 20000)plot(st_geometry(districts_alb), col ="grey")plot(st_geometry(roads_buff_20km), col ="blue", add =TRUE)plot(st_geometry(long_roads), col ="red", add =TRUE)