in

Geospatial Information Evaluation with OSMnx | by Eugenia Anello | Jun, 2023


Introduction to OSMNx

OSMnx is a library for downloading, analyzing and visualizing community knowledge from OpenStreetMap. It relies on two libraries, NetworkX and GeoPandas. Specifically, it exploits the graph module from the NetworkX library to retrieve community knowledge.

Furthermore, it permits us to work together with two OpenStreetMap APIs:

  • Nomatism for geocoding, which consists to find places by identify and handle.
  • Overpass API to extract factors of curiosity, like highways, faculties, and parks.

Obtain and Visualize OSM knowledge

As a substitute of manually downloading the information from the web site or from Geofabrik, we will immediately do it with OSMnx.

First, we have to import 4 libraries, that will likely be used later within the tutorial:

import osmnx as ox
import folium
import contextily as cx
import matplotlib.pyplot as plt

Along with OSMnx and matplotlib, we’re going to exploit folium, which is well-known for its capability on creating an interactive leaflet map, and contextily so as to add the background map. This side could be essential to acquire real looking maps.

Like the opposite time, we learn and visualize the OSM road community knowledge of Bologna, one of many greatest cities in Italy.

PLACE_NAME = 'Bologna, Italy'
G = ox.graph_from_place(PLACE_NAME, network_type='drive')
ox.plot_graph(G)
Black-and-white visualization of Bologna

From the black-and-white visualization, we will observe the factors, that symbolize the nodes, and the traces, that painting the traces. In comparison with OpenStreetMap’s web site, it may well appear very static and fundamental. Folium involves rescue us with its sturdy and interpretable maps:

ox.plot_graph_folium(G)
Map of Bologna obtained with Folium

That is a lot better, don’t you assume? The brilliant colors and the potential of interplay with the map are traits which might be essential after we use Google Maps to go to unknown locations.

In the event you verify higher the OpenStreetMap web site, you possibly can discover there may be the Normal layer as default. Apart from the Normal layer, there are different layers, reminiscent of Cycle Map and Transport Map. It’s unbelievable how we will exploit completely different layers relying on our functions.

If we’re obsessed with bikes, we’d be extra within the Cycle Map. That is potential at all times with one line of code:

G = ox.graph_from_place(PLACE_NAME, network_type='bike')

We’re bearing in mind the usual graph within the subsequent sections.

Convert graph to GeoDataframe

Coping with graphs isn’t intuitive as working with Dataframes and GeoDataframes. Because of this, we’d wish to convert the graph to a GeoDataframe:

space,edges = ox.graph_to_gdfs(G)
space.head()
edges.head()

You’ll be able to discover that now we have obtained two GeoDataframes, one for nodes and one for edges. It’s clear in the event you check out the geometry. The realm geodataframe has solely a pair of coordinates, latitude and longitude, whereas there are two pairs of coordinates within the geodataframe containing edges.

Extract Factors of Curiosity

When engaged on knowledge science tasks, we attempt to add data to our dataset by looking out open knowledge on the Web. From OSM knowledge, it’s potential to extract Factors of Curiosity (POI), that are locations we’d discover fascinating relying on the aim of our evaluation. Examples are eating places, church buildings, museums, and parks.

For instance, we wish to analyze the site visitors in Bologna to optimize and minimize the price of transportation. On this context, it might be helpful to know the highways, gasoline stations, parking garages, and different locations which might be linked to the potential bottleneck.

Let’s take all of the gasoline stations within the metropolis. That is potential by specifying fuel as the worth of the amenity key.

fuel_stations = ox.geometries_from_place(
PLACE_NAME,
{"amenity": "gas"},
)
fuel_stations.head()

Since now we have extracted all of the gasoline stations, it might be extra helpful to grasp the place they’re positioned by visualizing the map. Furthermore, we will add the bottom map to contextualize higher our outcomes.

area_crs = space.to_crs('3857')
edges_crs = edges.to_crs('3857')
fuel_stations_crs = fuel_stations.to_crs('3857')

fig, ax = plt.subplots(figsize=(10, 14))
area_crs.plot(ax=ax, facecolor='white')
edges_crs.plot(ax=ax, linewidth=1, edgecolor='blue')
fuel_stations_crs.plot(ax=ax, colour='pink', alpha=0.9, markersize=12)
plt.tight_layout()
cx.add_basemap(ax,crs=area_crs.crs.to_string())

That’s nice! We will discover that a lot of the gas stations are concentrated within the periphery. Moreover, we will distinguish completely different teams of service stations, that ought to be taken into consideration when measuring the site visitors outdoors the middle.

Discover the shortest route

One other useful performance of the OSMnx library is the chance to calculate the shortest path between two factors.

origin = (
ox.geocode_to_gdf("Parco della Montagnola, Bologna, Italy")
.to_crs(edges.crs)
.at[0, "geometry"]
.centroid
)

vacation spot = (
ox.geocode_to_gdf("Esso, Bologna, Italy")
.to_crs(edges.crs)
.at[0, "geometry"]
.centroid
)

origin_node_id = ox.nearest_nodes(G, origin.x, origin.y)
destination_node_id = ox.nearest_nodes(G, vacation spot.x, vacation spot.y)

That is potential with the shortest_path() methodology that makes use of by default the Dijkstra algorithm to compute the trail between the supply node and the goal node.

route = ox.shortest_path(G, origin_node_id, destination_node_id)
route
#[400881920,
# 250763178,
# 250763179,
# 250763533, ...
# 1694666466]

We will additionally attempt to visualize each the graph and the shortest path in a singular map:

ox.plot_route_folium(G, route, route_linewidth=6, node_size=0)

Et voilà! It’s like now we have used Google Maps to search out the best way, however as a substitute, we exploited the performance of the OSMnx library to search for it.

Ultimate ideas:

This was a information to let you understand how to work with OSM knowledge utilizing Python. I’ve discovered that OSMnx is essentially the most full Python library to cope with OpenStreetMap knowledge. In fact, it’s extra appropriate for exploring smaller locations, like cities. Within the case there are larger datasets, it’s higher to make use of extra specialised software program, like QGIS, to visualise them. Did you strive different libraries to work with OSM knowledge? Please touch upon the story if you recognize it. Take a look at the code here. Thanks for studying! Have a pleasant day!


Unsupervised deep studying identifies semantic disentanglement in single inferotemporal face patch neurons

SambaSafety automates customized R workload, bettering driver security with Amazon SageMaker and AWS Step Features