Networks for OSM Boundaries

Last update

November 30, 2024

If you want to download a street network for a specific OSM boundary then you can use osmnx to download the boundary. The boundary 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/29652 [00:00<?, ?it/s]100%|██████████| 29652/29652 [00:00<00:00, 532711.55it/s]
INFO:cityseer.tools.io:Processing edge geom coordinates, if present.
  0%|          | 0/32489 [00:00<?, ?it/s]100%|██████████| 32489/32489 [00:00<00:00, 1041738.28it/s]
INFO:cityseer.tools.graphs:Generating interpolated edge geometries.
  0%|          | 0/32489 [00:00<?, ?it/s] 32%|███▏      | 10373/32489 [00:00<00:00, 103720.95it/s] 64%|██████▍   | 20776/32489 [00:00<00:00, 103898.42it/s] 96%|█████████▌| 31196/32489 [00:00<00:00, 104031.75it/s]100%|██████████| 32489/32489 [00:00<00:00, 103230.14it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
  0%|          | 0/29652 [00:00<?, ?it/s]  3%|▎         | 785/29652 [00:00<00:03, 7812.23it/s]  5%|▌         | 1567/29652 [00:00<00:04, 6620.79it/s]  8%|▊         | 2241/29652 [00:00<00:04, 5797.84it/s] 10%|▉         | 2871/29652 [00:00<00:04, 5967.66it/s] 12%|█▏        | 3478/29652 [00:00<00:04, 5609.02it/s] 14%|█▍        | 4082/29652 [00:00<00:04, 5723.81it/s] 16%|█▌        | 4703/29652 [00:00<00:04, 5851.41it/s] 18%|█▊        | 5300/29652 [00:00<00:04, 5886.77it/s] 21%|██        | 6198/29652 [00:00<00:03, 6803.80it/s] 23%|██▎       | 6885/29652 [00:01<00:03, 6237.42it/s] 25%|██▌       | 7521/29652 [00:01<00:03, 6178.78it/s] 28%|██▊       | 8174/29652 [00:01<00:03, 6260.59it/s] 30%|██▉       | 8878/29652 [00:01<00:03, 6461.21it/s] 33%|███▎      | 9784/29652 [00:01<00:02, 7189.07it/s] 36%|███▌      | 10574/29652 [00:01<00:02, 7396.01it/s] 39%|███▉      | 11691/29652 [00:01<00:02, 8500.40it/s] 42%|████▏     | 12547/29652 [00:01<00:02, 7579.41it/s] 45%|████▍     | 13327/29652 [00:01<00:02, 7389.33it/s] 47%|████▋     | 14081/29652 [00:02<00:02, 7392.28it/s] 51%|█████     | 15037/29652 [00:02<00:01, 7958.89it/s] 54%|█████▍    | 15944/29652 [00:02<00:01, 8240.31it/s] 57%|█████▋    | 17042/29652 [00:02<00:01, 9019.25it/s] 61%|██████    | 17972/29652 [00:02<00:01, 9073.90it/s] 65%|██████▍   | 19183/29652 [00:02<00:01, 9939.04it/s] 68%|██████▊   | 20183/29652 [00:02<00:00, 9629.21it/s] 71%|███████▏  | 21152/29652 [00:02<00:00, 9418.39it/s] 76%|███████▌  | 22452/29652 [00:02<00:00, 10442.54it/s] 79%|███████▉  | 23504/29652 [00:03<00:00, 9704.57it/s]  84%|████████▍ | 24839/29652 [00:03<00:00, 10717.54it/s] 88%|████████▊ | 26021/29652 [00:03<00:00, 11028.52it/s] 92%|█████████▏| 27139/29652 [00:03<00:00, 10638.53it/s] 95%|█████████▌| 28292/29652 [00:03<00:00, 10880.33it/s]100%|██████████| 29652/29652 [00:03<00:00, 8379.67it/s] 
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
  0%|          | 0/11399 [00:00<?, ?it/s] 18%|█▊        | 2106/11399 [00:00<00:00, 19951.76it/s] 36%|███▌      | 4102/11399 [00:00<00:00, 13513.75it/s] 49%|████▉     | 5566/11399 [00:00<00:00, 11819.53it/s] 60%|█████▉    | 6808/11399 [00:00<00:00, 9870.82it/s]  69%|██████▉   | 7846/11399 [00:00<00:00, 8614.72it/s] 77%|███████▋  | 8742/11399 [00:00<00:00, 7740.06it/s] 84%|████████▎ | 9535/11399 [00:01<00:00, 7129.05it/s] 90%|████████▉ | 10256/11399 [00:01<00:00, 6675.83it/s] 96%|█████████▌| 10924/11399 [00:01<00:00, 6193.87it/s]100%|██████████| 11399/11399 [00:01<00:00, 8016.47it/s]