class: center, middle, inverse, title-slide # Working with Geospatial Data ## Geometric operations ### Ben Baumer ### SDS 192April 13th, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_geo_08-intersections.html
) --- class: center, middle, inverse # Spatial operations --- ## Geometric operations .footnote[https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations-1] .pull-left[ - Combine spatial layers - `st_intersection()`, `st_union()`, `st_difference()`, `st_sym_difference()` - Return another `sf` object OR - Detect connections/overlaps in spatial layers - `st_intersects()`, `st_touches()`, `st_within()`, etc. - Return `TRUE/FALSE` ] -- .pull-right[ ![](figures/geo-ops-1.png)<!-- --> ] --- ## Intersection of two layers .pull-left[ ```r base + geom_sf( * data = st_intersection(x, y), fill = "black", alpha = 0.5 ) ``` ] .pull-right[ ![](figures/st-intersection-1.png) ] --- ## Union of two layers .pull-left[ ```r base + geom_sf( * data = st_union(x, y), fill = "black", alpha = 0.5 ) ``` ] .pull-right[ ![](figures/st-union-1.png) ] --- ## Combine multiple operations .pull-left[ ```r base + geom_sf( data = x %>% st_union(y) %>% st_combine() %>% * st_convex_hull(), fill = "black", alpha = 0.5 ) ``` ] .pull-right[ ![](figures/st-convex-hull-1.png) ] --- background-image: url(https://r-spatial.github.io/sf/articles/sf3_files/figure-html/unnamed-chunk-29-1.png) background-size: contain --- ## Detection: Do `x` and `y` intersect? ```r st_intersects(x, y) ``` ``` ## Sparse geometry binary predicate list of length 3, where the predicate was `intersects' ## 1: 1, 3 ## 2: 2, 3 ## 3: 3 ``` ```r st_intersects(x, y, sparse = FALSE) ``` ``` ## [,1] [,2] [,3] [,4] ## [1,] TRUE FALSE TRUE FALSE ## [2,] FALSE TRUE TRUE FALSE ## [3,] FALSE FALSE TRUE FALSE ``` ```r st_intersects(y, x) ``` ``` ## Sparse geometry binary predicate list of length 4, where the predicate was `intersects' ## 1: 1 ## 2: 2 ## 3: 1, 2, 3 ## 4: (empty) ``` .footnote[https://r-spatial.github.io/sf/articles/sf3.html#binary-logical-operations-] --- ## Example: lines inside a boundary - Q: Streams *pass through* the MacLeish property. Can we find only the parts of those streams within the boundary? -- ```r library(macleish) boundary <- macleish_layers %>% pluck("boundary") streams <- macleish_layers %>% pluck("streams") ``` -- ```r nrow(streams) ``` ``` ## [1] 13 ``` ```r interior_streams <- boundary %>% st_intersection(streams) nrow(interior_streams) ``` ``` ## [1] 11 ``` --- ## Example: interior streams ```r library(leaflet) mac <- leaflet() %>% addTiles() %>% addPolygons(data = boundary, color = "black") mac %>% addPolylines(data = streams) %>% addPolylines(data = interior_streams, color = "red") ```
--- ## Example 2: two lines crossing - Q: Can we find all the **bridges**? All the places where trails cross streams? ```r trails <- macleish_layers %>% pluck("trails") crosses <- trails %>% st_intersection(streams) %>% st_cast("POINT") ``` ``` ## although coordinates are longitude/latitude, st_intersection assumes that they are planar ``` ``` ## Warning: attribute variables are assumed to be spatially constant throughout all ## geometries ``` ``` ## Warning in st_cast.MULTIPOINT(X[[i]], ...): point from first coordinate only ``` --- ## Example 2: trails crossing streams ```r mac %>% addPolylines(data = streams) %>% addPolylines(data = trails, color = "brown") %>% addMarkers(data = crosses) ```