1

I'm trying to retrieve a network of the major roads in Norway. However, the "int_ref" and "ref" labels are inconsistent and are resulting in gaps in the road. When looking at the road in OpenStreetMap I can see that the 'relation' tag under 'Part of' is exactly what I need. Is there any way to retrieve this using OSMnx? Is there any other way to retrieve a full road? I'm using the following line of code when filtering one specific road based on int_ref:

G1 = ox.graph_from_place(query = "Norway", retain_all = True, custom_filter = '["int_ref"~"E 39"]')

road tags and relation

Samuel
  • 211
  • 2
  • 10

1 Answers1

3

No, OSMnx filters on way tags, not on relations. If you want to get only the major roads in a country, see this answer: https://stackoverflow.com/a/52412274/7321942

Something like this may do what you are looking for:

import osmnx as ox
ox.config(use_cache=True, log_console=True)

# get the geometry of the norwegian mainland
gdf = ox.geocode_to_gdf('Norway')
geom = max(gdf['geometry'].iloc[0], key=lambda x: x.area)

# get all motorway/trunk roads
cf = '["highway"~"motorway|motorway_link|trunk|trunk_link"]'
G = ox.graph_from_polygon(geom, network_type='drive', custom_filter=cf)

# plot it
fig, ax = ox.plot_graph(G)

It takes ~370 Overpass API requests to download all the area of the Norwegian mainland, so it takes a while to make all those requests. You can watch its progress in the log in the console.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you! I solved it by following your code and composing it with a set of graphs containing the missing information. – Samuel Nov 17 '20 at 14:20
  • @gboeing, I am trying to use your code snippet to get the trunk highways of Utah: `gdf = ox.geocode_to_gdf(query=["R161993"], by_osmid=True) # Utah, United States geom = max(gdf['geometry'].iloc[0], key=lambda x: x.area) ` The first line seems to work fine and the JSON cache file seems to match. However, the second line fails with this message. Any ideas? `line 12, in geom = max(gdf['geometry'].iloc[0], key=lambda x: x.area) TypeError: 'Polygon' object is not iterable ` – Kevin P. Oct 12 '22 at 19:10
  • Digging into it some more, it looks like Utah is a polygon while Norway is a multipolygon. So the code snippet needs to work with just a polygon. – Kevin P. Oct 12 '22 at 21:48