Networks for OSM Boundaries

Last update

October 15, 2024

If you want to download a street network for a specific OSM boundary then you can use osmnx to download the boundary, which can then be passed to cityseer for generating a network.

Download Boundary

Download the boundary. Go to OSM and select the relation ID for the boundary that you’d like to download and pass it to ox.geocode_to_gdf().

import osmnx as ox

bounds_gdf = ox.geocode_to_gdf(
    "R1536593",  # OSM relation ID
    by_osmid=True,
)
# optionally use to_crs() to project to a target CRS
# returns a geoDataFrame - union for the geom
bounds_geom = bounds_gdf.union_all()
bounds_geom

Tip

You can optionally project to a required CRS by using geopandas to_crs() methodo before taking the union. This is generally recommended if you want to apply a buffer to the geometry (assuming a unit such as metres).

Buffer and Simplify

Buffer and simplify depending on preferences. Make sure your distances correspond to the unit of the current CRS!

# buffer if wanted
bounds_geom = bounds_geom.buffer(0.01)  # WGS - 4326
# some simplification is recommended
# otherwise the request to the underlying API used by io.osm_graph_from_poly()
# might complain that the boundary description is too long
bounds_geom = bounds_geom.simplify(
    0.001  # WGS - 4326
)  # here the CRS unit is degrees hence the small number
bounds_geom

Generate Network

Now you can pass the boundary to cityseer to download and create the network.

from cityseer.tools import io, plot

G_nx = io.osm_graph_from_poly(
    bounds_geom,
    # 4326 by default - use projected CRS instead if you projected your GDF
    poly_crs_code=4326,
    simplify=False,  # set to True for automatic simplification
)
plot.plot_nx(G_nx, plot_geoms=True)
INFO:cityseer.tools.io:Converting networkX graph from EPSG code 4326 to EPSG code 32631.
INFO:cityseer.tools.io:Processing node x, y coordinates.
  0%|          | 0/26684 [00:00<?, ?it/s]100%|██████████| 26684/26684 [00:00<00:00, 669587.06it/s]
INFO:cityseer.tools.io:Processing edge geom coordinates, if present.
  0%|          | 0/28861 [00:00<?, ?it/s]100%|██████████| 28861/28861 [00:00<00:00, 1323303.21it/s]
INFO:cityseer.tools.graphs:Generating interpolated edge geometries.
  0%|          | 0/28861 [00:00<?, ?it/s] 41%|████▏     | 11972/28861 [00:00<00:00, 119705.56it/s] 84%|████████▎ | 24156/28861 [00:00<00:00, 120955.01it/s]100%|██████████| 28861/28861 [00:00<00:00, 120723.92it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
  0%|          | 0/26684 [00:00<?, ?it/s]  4%|▎         | 993/26684 [00:00<00:02, 9916.63it/s]  8%|▊         | 2132/26684 [00:00<00:02, 10762.60it/s] 13%|█▎        | 3363/26684 [00:00<00:02, 11454.43it/s] 17%|█▋        | 4509/26684 [00:00<00:01, 11349.75it/s] 21%|██        | 5648/26684 [00:00<00:01, 11341.59it/s] 26%|██▌       | 6859/26684 [00:00<00:01, 11554.59it/s] 30%|███       | 8015/26684 [00:00<00:01, 11527.70it/s] 35%|███▌      | 9393/26684 [00:00<00:01, 12235.42it/s] 40%|████      | 10696/26684 [00:00<00:01, 12394.32it/s] 45%|████▌     | 12016/26684 [00:01<00:01, 12628.44it/s] 50%|████▉     | 13290/26684 [00:01<00:01, 12656.46it/s] 55%|█████▍    | 14556/26684 [00:01<00:00, 12459.00it/s] 59%|█████▉    | 15803/26684 [00:01<00:00, 12130.56it/s] 64%|██████▍   | 17075/26684 [00:01<00:00, 12298.91it/s] 69%|██████▊   | 18307/26684 [00:01<00:00, 11980.23it/s] 73%|███████▎  | 19508/26684 [00:01<00:00, 11911.32it/s] 78%|███████▊  | 20774/26684 [00:01<00:00, 12125.35it/s] 82%|████████▏ | 22001/26684 [00:01<00:00, 12161.26it/s] 87%|████████▋ | 23219/26684 [00:01<00:00, 12068.45it/s] 92%|█████████▏| 24427/26684 [00:02<00:00, 11699.73it/s] 96%|█████████▌| 25614/26684 [00:02<00:00, 11652.84it/s]100%|██████████| 26684/26684 [00:02<00:00, 12001.73it/s]
INFO:cityseer.tools.graphs:Ironing edges.
  0%|          | 0/8985 [00:00<?, ?it/s]  6%|▋         | 571/8985 [00:00<00:01, 5695.52it/s] 13%|█▎        | 1153/8985 [00:00<00:01, 5766.16it/s] 19%|█▉        | 1730/8985 [00:00<00:01, 5707.55it/s] 26%|██▌       | 2325/8985 [00:00<00:01, 5800.04it/s] 33%|███▎      | 2967/8985 [00:00<00:00, 6020.63it/s] 40%|████      | 3603/8985 [00:00<00:00, 6134.30it/s] 47%|████▋     | 4224/8985 [00:00<00:00, 6158.71it/s] 54%|█████▍    | 4846/8985 [00:00<00:00, 6177.35it/s] 61%|██████    | 5485/8985 [00:00<00:00, 6241.64it/s] 68%|██████▊   | 6110/8985 [00:01<00:00, 6193.76it/s] 75%|███████▌  | 6744/8985 [00:01<00:00, 6237.13it/s] 82%|████████▏ | 7368/8985 [00:01<00:00, 6170.33it/s] 89%|████████▉ | 7991/8985 [00:01<00:00, 6188.07it/s] 96%|█████████▌| 8610/8985 [00:01<00:00, 6063.10it/s]100%|██████████| 8985/8985 [00:01<00:00, 6054.42it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 1.
  0%|          | 0/8985 [00:00<?, ?it/s]100%|██████████| 8985/8985 [00:00<00:00, 203190.93it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
  0%|          | 0/8974 [00:00<?, ?it/s] 28%|██▊       | 2555/8974 [00:00<00:00, 25542.90it/s] 57%|█████▋    | 5110/8974 [00:00<00:00, 15200.34it/s] 76%|███████▋  | 6859/8974 [00:00<00:00, 11694.01it/s] 91%|█████████▏| 8191/8974 [00:00<00:00, 9825.46it/s] 100%|██████████| 8974/8974 [00:00<00:00, 10616.48it/s]