Bus lines/routes from OpenStreetMaps using OSMnx

93 views Asked by At

I am trying to obtain bus lines/routes from OpenStreetMap using OSMnx. For example busline 284 in Nootdorp, Netherlands: https://www.openstreetmap.org/relation/2024408#map=15/52.0463/4.3967&layers=T

How do I obtain the exact route (i.e. as a list of nodes) as shown in OSM?

I tried using osmnx.features.features_from_place:

import osmnx as ox

gdf_busline = ox.features.features_from_place("Nootdorp, Netherlands", tags={'type': 'route', 'route': 'bus'})

print(gdf_busline)

But it returns an Empty Dataframe. (I also tried a some other combinations of tags, but no success)

1

There are 1 answers

1
gboeing On

From the OSMnx Getting Started guide:

Using OSMnx’s features module, you can search for and download geospatial features (such as building footprints, grocery stores, schools, public parks, transit stops, etc) from the OpenStreetMap Overpass API as a GeoPandas GeoDataFrame. This uses OpenStreetMap tags to search for matching elements.

Accordingly, remember that you are searching for matching elements, using tags. That is, if you seek the nodes of the route, then the elements you are searching for are nodes, not relations, and you seek them by their tags.

Also from the features module documentation:

You can use this module to query for nodes, ways, and relations (the latter of type “multipolygon” or “boundary” only) by passing a dictionary of desired OSM tags.

So if you do want a relation itself, it must be of type multipolygon or boundary.

For example, if you want all the bus stops (i.e., the nodes of a bus line):

import osmnx as ox
place = "Nootdorp, Netherlands"
tags = {"highway": "bus_stop"}
gdf = ox.features.features_from_place(place, tags)
gdf.shape  # (27, 12)

Also, if you know the OSM ID of the relation you want to retrieve, you can use the geocoder module to retrieve it directly. See the docs. This uses the Nominatim API under the hood. However, be aware that Nominatim doesn't support all relations. For example, searching Nominatim for Nootdorp by ID works but searching for Bus 284 by ID doesn't because it cannot be found in Nominatim:

gdf1 = ox.geocode_to_gdf("R162256", by_osmid=True)  # succeeds
gdf2 = ox.geocode_to_gdf("R2024408", by_osmid=True) # fails

See also this issue at Nominatim.