pythongraphnetworkxopenstreetmaposmnx

osmnx query OpenStreetMap


I am using Python and I am trying to use osmnx to query OpenStreetMap and get data about the Italian city of Brescia.

Here's the code I copied from the docs.

import osmnx as ox

place = "Brescia, Italy"
osmnx_buffer = 10000 # in metres
g = ox.graph.graph_from_place(place, buffer_dist=osmnx_buffer)
g = ox.projection.project_graph(g)

ox.plot_graph(g)

The only thing I changed is the content of the string place.

The problem is that when I run the script, it never replies (and therefore, never plots anything). I tried to read the documentation and change my query, but I do not know what is wrong with it. Can you please suggest me how to make this script work?

My objective is to work on it with networkx.

Thank you in advance.


Solution

  • Everything appears to be working fine with your code (I just ran it successfully). But note that your buffer_dist function parameter you're passing is deprecated. Also, turn on OSMnx logging if you're ever wondering what it's doing. It takes a couple minutes to build the full model of ~2.5 million nodes and ~5 million edges, then do all the proper truncation and topological correction.

    Here's example code plus logging output:

    import osmnx as ox
    ox.settings.log_console = True  # turn on logging
    place = "Brescia, Italy"
    
    # buffer the study area by 10 km
    geom = ox.geocode_to_gdf(place).iloc[0]["geometry"]
    geom_proj, crs = ox.projection.project_geometry(geom)
    geom_buff, _ = ox.projection.project_geometry(geom_proj.buffer(10000), crs=crs, to_latlong=True)
    
    # get the graph and plot it
    G = ox.graph_from_polygon(geom_buff)
    Gp = ox.project_graph(G)
    fig, ax = ox.plot_graph(Gp)
    

    and the log output:

    2023-12-20 12:45:56 Retrieved response from cache file 'cache/39adbce181a495e998c06fd21bd65ad28d80e3d3.json'
    2023-12-20 12:45:56 Created GeoDataFrame with 1 rows from 1 queries
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:32632 / WGS 84 / UTM zone 32N'
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:4326 / WGS 84'
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:32632 / WGS 84 / UTM zone 32N'
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:4326 / WGS 84'
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:32632 / WGS 84 / UTM zone 32N'
    2023-12-20 12:45:56 Projected GeoDataFrame to 'EPSG:4326 / WGS 84'
    2023-12-20 12:45:56 Requesting data from API in 6 request(s)
    2023-12-20 12:45:57 Retrieved response from cache file 'cache/2e7b530676d51fb707ca5d490cea53497b235300.json'
    2023-12-20 12:45:57 Retrieved response from cache file 'cache/ee58a27f7c210838eafe3cc95e2470d884a9582e.json'
    2023-12-20 12:45:58 Retrieved response from cache file 'cache/8d11d09ff31df390c9837d54ee33d55d005e8fa5.json'
    2023-12-20 12:45:59 Retrieved response from cache file 'cache/9a85fc97fe64f2d6acbdf37b8476f36ed11e98cd.json'
    2023-12-20 12:46:00 Retrieved response from cache file 'cache/0636bb65f47203267ae6843d3abd4b2ed812ed9e.json'
    2023-12-20 12:46:00 Retrieved response from cache file 'cache/0f4b714e6f9cd7bcabe129c5c6a294fc104b5381.json'
    2023-12-20 12:46:00 Retrieved all data from API in 6 request(s)
    2023-12-20 12:46:00 Creating graph from 2,465,008 OSM nodes and 271,692 OSM ways...
    2023-12-20 12:46:20 Created graph with 2,465,008 nodes and 4,944,897 edges
    2023-12-20 12:46:30 Added length attributes to graph edges
    2023-12-20 12:46:30 Identifying all nodes that lie outside the polygon...
    2023-12-20 12:46:45 Created nodes GeoDataFrame from graph
    2023-12-20 12:46:45 Created r-tree spatial index for 2,465,008 geometries
    2023-12-20 12:46:46 Identified 2,142,413 geometries inside polygon
    2023-12-20 12:47:05 Removed 322,595 nodes outside polygon
    2023-12-20 12:47:05 Truncated graph by polygon
    2023-12-20 12:47:05 Begin topologically simplifying the graph...
    2023-12-20 12:47:27 Identified 283,430 edge endpoints
    2023-12-20 12:48:19 Simplified graph: 2,142,413 to 283,430 nodes, 4,294,956 to 686,891 edges
    2023-12-20 12:48:19 Identifying all nodes that lie outside the polygon...
    2023-12-20 12:48:21 Created nodes GeoDataFrame from graph
    2023-12-20 12:48:21 Created r-tree spatial index for 283,430 geometries
    2023-12-20 12:48:21 Identified 276,578 geometries inside polygon
    2023-12-20 12:48:25 Removed 6,852 nodes outside polygon
    2023-12-20 12:48:28 Removed 79 isolated nodes
    2023-12-20 12:48:36 Got largest weakly connected component (272,847 of 276,499 total nodes)
    2023-12-20 12:48:36 Truncated graph by polygon
    2023-12-20 12:48:37 Counted undirected street segments incident on each node
    2023-12-20 12:48:38 graph_from_polygon returned graph with 272,847 nodes and 665,586 edges
    2023-12-20 12:48:39 Created nodes GeoDataFrame from graph
    2023-12-20 12:48:42 Projected GeoDataFrame to 'EPSG:32632 / WGS 84 / UTM zone 32N'
    2023-12-20 12:48:46 Created edges GeoDataFrame from graph
    2023-12-20 12:48:47 Projected GeoDataFrame to 'EPSG:32632 / WGS 84 / UTM zone 32N'
    2023-12-20 12:48:56 Created graph from node/edge GeoDataFrames
    2023-12-20 12:48:57 Projected graph with 272847 nodes and 665586 edges
    2023-12-20 12:48:57 Begin plotting the graph...
    2023-12-20 12:49:06 Created edges GeoDataFrame from graph
    2023-12-20 12:49:15 Created nodes GeoDataFrame from graph
    2023-12-20 12:49:19 Finished plotting the graph