class: center, middle, inverse, title-slide # Working with Geospatial Data ## Spatial aggregation ### Ben Baumer ### SDS 192April 15th, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_geo_09-aggregation.html
) --- class: center, middle, inverse # Spatial aggregation --- ## Spatial aggregation - Use `group_by()` and `summarize()` -- - Spatial aggregation is **implied** - `st_union()` by default -- - `POINT` geometries may become `MULTIPOINT` - ~~`leaflet` doesn't plot `MULTIPOINT`~~ - ~~Use `st_cast()` to convert `MULTIPOINT` to `LINESTRING`~~ -- - `POLYGON` geometries may become `MULTIPOLYGON` --- ## Trails ```r library(sf) library(macleish) trails <- macleish_layers %>% pluck("trails") trails ``` ``` ## Simple feature collection with 15 features and 2 fields ## geometry type: LINESTRING ## dimension: XY ## bbox: xmin: -72.68513 ymin: 42.44177 xmax: -72.67731 ymax: 42.4618 ## CRS: EPSG:4326 ## First 10 features: ## name color geometry ## 1 Porcupine Trail White LINESTRING (-72.68291 42.45... ## 2 Western Loop Red LINESTRING (-72.68111 42.45... ## 3 Poplar Hill Road Road LINESTRING (-72.68155 42.45... ## 4 Vernal Pool Loop Yellow LINESTRING (-72.68265 42.44... ## 5 Eastern Loop Blue LINESTRING (-72.68113 42.45... ## 6 Western Loop Red LINESTRING (-72.68401 42.45... ## 7 Western Loop Red LINESTRING (-72.68088 42.44... ## 8 entry trail - LINESTRING (-72.68088 42.44... ## 9 Eastern Loop Blue LINESTRING (-72.68063 42.45... ## 10 Easy Out Red LINESTRING (-72.67962 42.45... ``` --- ## Aggregated trails ```r trails %>% group_by(name) %>% summarize(N = n()) ``` ``` ## Simple feature collection with 9 features and 2 fields ## geometry type: GEOMETRY ## dimension: XY ## bbox: xmin: -72.68513 ymin: 42.44177 xmax: -72.67731 ymax: 42.4618 ## CRS: EPSG:4326 ## # A tibble: 9 x 3 ## name N geometry ## <fct> <int> <GEOMETRY [°]> ## 1 Driveway 1 LINESTRING (-72.68088 42.44878, -72.68082 42.44882, -72.6… ## 2 Eastern Loop 2 MULTILINESTRING ((-72.68063 42.45033, -72.68055 42.45041,… ## 3 Easy Out 2 MULTILINESTRING ((-72.67962 42.45643, -72.67965 42.45646,… ## 4 entry trail 1 LINESTRING (-72.68088 42.44783, -72.68072 42.44787, -72.6… ## 5 Poplar Hill … 2 MULTILINESTRING ((-72.68087 42.44863, -72.68088 42.44855,… ## 6 Porcupine Tr… 1 LINESTRING (-72.68291 42.45021, -72.68296 42.45036, -72.6… ## 7 Snowmobile T… 2 MULTILINESTRING ((-72.68053 42.45106, -72.68035 42.45098,… ## 8 Vernal Pool … 1 LINESTRING (-72.68265 42.44944, -72.68274 42.44941, -72.6… ## 9 Western Loop 3 MULTILINESTRING ((-72.68111 42.45526, -72.68129 42.45519,… ``` --- ## Compare lengths ```r trails %>% st_length() ``` ``` ## Units: [m] ## [1] 699.63680 969.28430 899.85448 360.51939 831.63070 193.83152 ## [7] 187.50997 208.10487 1108.46789 66.91166 139.69878 69.13609 ## [13] 1347.04310 1228.17373 173.43296 ``` ```r trails %>% group_by(name) %>% summarize(N = n()) %>% st_length() ``` ``` ## Units: [m] ## [1] 173.4330 1940.0986 136.0478 208.1049 1039.5533 699.6368 2575.2168 ## [8] 360.5194 1350.6258 ``` --- ## `MULTIPOINT`, `MULTIPOLYGON` ```r michigan <- maps::map('state', regions = "Michigan", fill = TRUE) %>% maptools::map2SpatialPolygons(IDs = 1:2) %>% st_as_sf() %>% mutate(name = c("upper peninsula", "lower peninsula")) ``` ![](figures/michigan-1.png)<!-- --> --- ## From two features to one ```r # two POLYGON features michigan ``` ``` ## Simple feature collection with 2 features and 1 field ## geometry type: POLYGON ## dimension: XY ## bbox: xmin: -90.41273 ymin: 41.71133 xmax: -82.44289 ymax: 47.48101 ## CRS: NA ## geometry name ## 1 POLYGON ((-90.41273 46.5585... upper peninsula ## 2 POLYGON ((-83.44557 41.7514... lower peninsula ``` ```r # one MULTIPOLYGON feature michigan_as_one <- michigan %>% summarize(name = "just michigan", num_features = n()) michigan_as_one ``` ``` ## Simple feature collection with 1 feature and 2 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: -90.41273 ymin: 41.71133 xmax: -82.44289 ymax: 47.48101 ## CRS: NA ## name num_features geometry ## 1 just michigan 2 MULTIPOLYGON (((-83.44557 4... ``` --- ## Leaflet with many `POLYGON`s ```r library(leaflet) leaflet() %>% addTiles() %>% addPolygons(data = michigan, popup = ~name) ```
--- ## Leaflet with `MULTIPOLYGON` ```r library(leaflet) leaflet() %>% addTiles() %>% addPolygons(data = michigan_as_one, popup = ~name) ```