Open Street Map using OSMNX: how to retrieve the Hannover subway network?

Viewed 1329
import osmnx as ox
ox.__version__ # '0.13.0'

I would like to show the subway in Hannover as known in the German subway OSM data on a map using the great OSMNX module. But unlike the New York example no results are returned for:

G = ox.graph_from_place('Hannover, Germany',
                        retain_all=False, truncate_by_edge=True, simplify=True,
                        network_type='none', custom_filter='["railway"~"subway"]')

# EmptyOverpassResponse: There are no data elements in the response JSON

I do get results for other similar queries using 'Hannover, Germany' as region. I also do not get subway results for Paris or London. And I do not get results for similar queries like custom_filter='["railway"~"tram"]' or '["railway"~"s-bahn"]' or '["network"~"metro"]'.

Also, if I use the infrastructure keyword argument to select "railway", an extensive gdf is returned:

G = ox.graph_from_place('Hannover, Germany', retain_all=False, truncate_by_edge=True, simplify=True,
                        network_type='none', infrastructure='way["railway"]')
gdfox = ox.graph_to_gdfs(G, nodes=False, edges=True, node_geometry=True, fill_edge_geometry=True)
gdfox.shape # (4422, 14)

But I cannot identify the subway using the columns returned?:

['u', 'v', 'key', 'osmid', 'service', 'oneway', 'length',
   'geometry', 'name', 'maxspeed', 'ref', 'bridge', 'tunnel',
   'access']

What I also find strange is that there are only 2 LINESTRINGS returned if I (try to) retrieve all railways using the custom_filter:

G = ox.graph_from_place('Hannover, Germany', retain_all=False, truncate_by_edge=True,
                        simplify=True, network_type=None, custom_filter='["railway"~""]')
gdfox = ox.graph_to_gdfs(G, nodes=False, edges=True, node_geometry=True, fill_edge_geometry=True)
gdfox.shape # (2, 10) # returns only 2 LINESTRINGS: Altenbekener Damm
1 Answers

I am in the process of removing the infrastructure parameter in favor of a more consistent custom_filter parameter. Will be done in a couple days: https://github.com/gboeing/osmnx/pull/477 (EDIT: done and released in v0.14.0; code snippet below edited accordingly.)

In the meantime, I am not familiar with Hannover but it appears that its passenger rail system is tagged as "tram" and "rail" rather than "subway". Something like this seems to capture it:

import osmnx as ox
ox.config(use_cache=False,
          log_console=True,
          useful_tags_way=ox.settings.useful_tags_way + ['railway'])

G = ox.graph_from_place('Hannover, Germany',
                        retain_all=False, truncate_by_edge=True, simplify=True,
                        custom_filter='["railway"~"tram|rail"]')
len(G) #1776
Related