Class 13
osmdata to query some vectorslibrary(dplyr)
library(osmdata) # has to install first
q <- opq(bbox = st_bbox(
c(xmin = -74.448598, xmax = -74.429830,
ymax = 40.532277, ymin = 40.518258), crs = st_crs(4326)))
# Building
buildings <- q %>%
add_osm_feature(key = 'building') %>%
osmdata_sf() %>% `[[`('osm_polygons') %>%
filter(!is.na(name))
# Streets
streets <- q %>%
add_osm_feature(key = 'highway') %>%
osmdata_sf() %>% `[[`('osm_lines') %>%
filter(!is.na(name))
# Trees
trees <- q %>%
add_osm_feature(key = c('natural')) %>%
osmdata_sf() %>% `[[`('osm_points')sf is a data.frame, so all base R and dplyr syntax works on it. E.g.
st_drop_geometry() to drop geometry column
Tasks:
streets that are residential or path.streets by types.# A tibble: 6 × 2
highway count
<chr> <int>
1 path 8
2 residential 50
3 secondary 11
4 service 2
5 tertiary 19
6 unclassified 1
st_areast_lengthst_distancest_areabuildings <- buildings %>%
mutate(area = st_area(.)) %>%
select(name, area)
head(buildings, 3) # Observe column areaSimple feature collection with 3 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -74.43991 ymin: 40.5225 xmax: -74.4353 ymax: 40.52598
Geodetic CRS: WGS 84
name area geometry
221488774 Livingston Apartments C 4513.754 [m^2] POLYGON ((-74.4367 40.52485...
221488775 Livingston Apartments B 3942.836 [m^2] POLYGON ((-74.43778 40.5256...
251381247 Beck Hall 1841.321 [m^2] POLYGON ((-74.43915 40.5225...
units::set_units()
10 [km^2]
[1] 10
Simple feature collection with 3 features and 2 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -74.43991 ymin: 40.5225 xmax: -74.4353 ymax: 40.52598
Geodetic CRS: WGS 84
name area geometry
221488774 Livingston Apartments C 0.4513754 [ha] POLYGON ((-74.4367 40.52485...
221488775 Livingston Apartments B 0.3942836 [ha] POLYGON ((-74.43778 40.5256...
251381247 Beck Hall 0.1841321 [ha] POLYGON ((-74.43915 40.5225...
streets:
streets.trees: Calculate the distance between the first two tree in miles.
# A tibble: 6 × 2
highway avg_length
<chr> [km]
1 path 0.649
2 residential 0.285
3 secondary 0.122
4 service 0.165
5 tertiary 0.157
6 unclassified 0.486
st_centroid (get the centroid of objects)st_buffer (add buffer to any objects)st_coordinates (get coordinates of objects)st_simplify (reduce the complexity of objects)st_bbox (get the bounding box of objects)Practice 3
Clipping/overlapping
st_intersectionst_difference & st_sym_differencest_union: return sfc# Your building from osmdata
building_osm <- buildings %>% filter(name == '...')
# building polygon you created
building_mine <- ...
# try these operations
bld_itst <- st_intersection(building_mine, building_osm)
dif_osm_bld <- st_difference(building_osm, building_mine)
dif_bld_osm <- st_difference(building_mine, building_osm)
bld_union <- st_union(building_mine, building_osm)
bld_bbox <- st_bbox(bld_union) %>%
st_as_sfc() %>% st_sf()
# plot the results and learn each function
plot(bld_bbox$geometry, col = 'lightblue')
plot(building_osm$geometry, border = "black", add = T)
plot(building_mine$geometry, border = "brown", add = T)
plot(dif_osm_bld$geometry, col = 'red', add = T)
plot(dif_bld_osm$geometry, col = 'blue', add = T)
plot(bld_itst$geometry, col = 'yellow', add = T)