4

osmnx is a Python tool that allows you to download maps from OpenStreetMap and work with them as Networkx graphs. I could, for example get the the street map of a LA using the following command:

osmnx.graph_from_place('Los Angeles, CA, USA', network_type='drive')

Now I am trying to download a district within Tehran, Iran. Using the name 'Tehran, Iran' didn't help much and the result is wrong. However, the city boundary has an OSM ID of 8775292 as found in the link below:

https://www.openstreetmap.org/relation/8775292

So, is there a way to give OSMNx the boundary ID instead of giving it the name for query?

Thanks!

dani
  • 147
  • 2
  • 10

1 Answers1

6

Per the OSMnx documentation you can provide your place query as either a string or a dict. See the usage examples for demonstrations of each. Using a dict seems to work fine in returning the city boundaries you're looking for:

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

# define the place query
query = {'city': 'Tehran'}

# get the boundaries of the place
gdf = ox.geocode_to_gdf(query)
gdf.plot()

# or just get the street network within the place
G = ox.graph_from_place(query, network_type='drive')
fig, ax = ox.plot_graph(G, edge_linewidth=0)

It also works fine if you query a more specific string in Persian or English:

# query in persian
gdf = ox.geocode_to_gdf('شهر تهران')
gdf.plot()

# or in english
gdf = ox.geocode_to_gdf('Tehran City')
gdf.plot()

is there a way to give OSMNx the boundary ID instead of giving it the name for query

No, OSMnx retrieves boundary polygons by querying the Nominatim geocoder.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you dear Prof. Boeing for answering my question, and also creating the OSMnx package. I greatly appreciate it, and your presence in the forums is also a great help to the community. I tried your solution and it worked. I found that setting the query as 'tehran city district 1' works as well. I'm trying to use the street layout as the water pipe network of the city for some hydraulic calculations. I'll have to extract the point elevations from an SRTM tiff file later and assign it to nodes. – dani Dec 01 '20 at 20:38
  • 1
    See also https://github.com/gboeing/osmnx/issues/620 – gboeing Dec 16 '20 at 00:06
  • Amazing! Thank you for incorporating the OSM ID search via by_osmid=True. Much appreciate it Prof. Boeing. – dani Dec 29 '20 at 07:19