Extracts spatial features from the OpenStreetMaps server: features that are extracted are re-classified into broad categories:

  • osm_polygons: urban, agriculture, open, forest, water

  • osm_lines: roads, waterways

  • osm_points: city names

collect_osm_features(
  proj_bbox,
  download_features = "all",
  landuse_elements = "all",
  line_elements = "all"
)

Arguments

proj_bbox

A bbox. The bounding box for the project/ study area for which to extract osm features.

download_features

A character. "all" download all features. "polygons", "lines" and "points" to download only polygon, line or point features respectively. Combinations are also possible (e.g. c("polygons", "points")). Default is "all".

landuse_elements

A character. "all" to download all landuse classes. "urban", "agriculture", "open", "forest" and "water" to download only landuse classes of interest. Combinations are also possible (e.g. c("urban", "forest", "water")) Default is "all".

line_elements

A character. "all" to download all line elements. "road", "water" to download only roads and rivers, streams etc. respectively. Default is "all"

Value

a named list of 3 sf data frames: osm_polygons, osm_lines, osm_points. Each sf data frame contains the corresponding geometry types.

Details

A function to collect custom osm features for a project

dplyr and osmdata are automatically installed when missing from your system.

See also

Author

Martijn Bollen

Examples

if (FALSE) { # \dontrun{

# extract the bounding box (WGS84) for the Project
proj_sf <- st_sfc(st_polygon(list(drg_example$spatial$coordinates[1,,])), crs = 4326)
proj_bbox <- st_bbox(proj_sf)
class(proj_bbox)
# extract selected OSM features
osm <- collect_osm_features(proj_bbox)
# extract only polygon OSM features
osm_polygons <- collect_osm_features(proj_bbox, download_features = "polygons")
osm_lines <- collect_osm_features(proj_bbox, download_features = "lines")
osm_points <- collect_osm_features(proj_bbox, download_features = "points")
osm_forest_water <-
 collect_osm_features(proj_bbox, download_features = c("polygons", "points"),
                      landuse_elements = c("forest", "water"))
# extract combination of OSM features, subset line elements
osm_polygons_roads <-
 collect_osm_features(proj_bbox, download_features = c("polygons", "lines"),
                      line_elements = "road")

# calculate the area of each landuse class within the bbox
landuse <- osm_polygons$osm_polygons %>%
 st_make_valid() %>%
 mutate(area = set_units(st_area(.), "km^2")) %>%
 group_by(landuse) %>%
 summarise(area = sum(area))

# plot
## polygons
(p1 <- ggplot(osm_polygons$osm_polygons %>% filter(!is.na(landuse)) %>% arrange(landuse)) +
   geom_sf(aes(fill = landuse), col = NA) +
   scale_fill_manual(values = unique(arrange(osm$osm_polygons, landuse)$osm_fill)) +
   theme_void() + theme(legend.position = "right"))

## lines
(p2 <- ggplot(osm_lines$osm_lines) +
   geom_sf(aes(col = line_element)) +
   scale_color_manual(values = c("grey50", "#0092da")) +
   theme_void() + theme(legend.position = "right"))

## points
(p3 <- ggplot(osm_points$osm_points) + geom_sf_label(aes(label = name)))

## combine features
p1 + geom_sf(data = osm$osm_lines, aes(col = line_element)) +
 geom_sf_label(data = osm$osm_points, aes(label = name)) +
 scale_color_manual(values = c("grey20", "#0092da")) +
 coord_sf(xlim = proj_bbox[c("xmin", "xmax")],
          ylim = proj_bbox[c("ymin", "ymax")])
} # }