'Open street map data - electoral wards

Im struggling to find the correct key and value to plot electoral wards in R's osmdata.

library(tidyverse)
library(osmdata)

ward <- getbb("Sheffield United Kingdom") %>%
  opq() %>%
  add_osm_feature(boundary="political", political_division="ward") %>%
 osmdata_sf() 

key is specified as an input typically of the nature "highway" and value would in such case be motorway but the docs don't clarify what can be used in this use case.



Solution 1:[1]

The issue is with passing the key and value to add_osm_feature() function

library(osmdata)
library(tidyverse)
library(sf)

ward <- getbb("Sheffield United Kingdom") %>%
  opq() %>%
  add_osm_feature(key = "boundary", value ="political") %>%
  add_osm_feature(key = "political_division", value ="ward") %>%
  osmdata_sf() 

sheffield <- getbb("Sheffield United Kingdom") %>%
  opq() %>%
  add_osm_feature(key = "boundary", value ="administrative") %>%
  osmdata_sf()

d8 <- st_as_sf(sheffield$osm_multipolygons) |>
  filter(admin_level == 8 & name == "Sheffield") |>
  select(osm_id, name, geometry)

plot(d8$geometry, col = "gray")

The objects returned with osmdata are long lists, consisting of osm_points, osm_lines, osm_polygons (created from closed lines) and osm_multipolygons (created from osm relations), therefore we can suspect wards in osm_polygon and osm_multipolygon. You can st_cast() multipolygons to polygons and then merge both features.

plot(st_as_sf(ward$osm_multipolygons$geometry), add=TRUE, col = "yellow")
plot(st_as_sf(ward$osm_polygons$geometry), add=TRUE, col = "lightyellow")

Created on 2022-02-10 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grzegorz Sapijaszko