Retrieving OpenStreetMap Data Using OSMNX, a Python Package.

If you want to retrieve Geospatial data from OpenStreetMap (OSM), you can download it, but that takes time and storage. Without leaving your development environment (Jupyter Notebooks), you can access and retrieve OpenStreetMap data. Imagine you are doing analysis and want to find out how many recreational facility or restaurants are in your interested area.

In this tutorial, we learn to retrieve OpenStreetMap data using OSMNX, a python package.

OSMnx is a Python package that lets you download spatial geometries and model, project, visualize, and analyze street networks and other spatial data from OpenStreetMap’s API

In the next three sections, we retrieve three different kinds of data from OpenStreetMap: Cafes as points of interest, buildings, and street networks. We also explore Geospatial data visualisation with Folium.

Streets

We first create a variable to hold our area of interest. Feel free to explore any other location you would like, but I use Liverpool city in this tutorial ( Note the larger your area, the longer the computation takes.)

place = “Liverpool, United Kingdom”
graph = ox.graph_from_place(place, network_type=’drive’)

With the above two lines of code, OSMnx allows us to retrieve the street networks of the city quickly. The result is a Networkx class, and we convert it to Geopandas to further process the data. Let us see how we can turn it to Geopandas Geodataframe. OSMnx comes with “graph_to_gdf” function which can easily do that:

nodes, streets = ox.graph_to_gdfs(graph)
streets.head()

Now, if we look at the data, it is converted to the familiar form of a pandas data frame with extra capability for Geographic data handling.

Retrieved streets
Retrieved streets

You can process the retrieved data with any tool of your choice (Pandas in our case) or visualize your data with any of the Python libraries. Let us say we want to get the bar chart of street types. The following process is just pure pandas functionality with seaborn data visualisation.

street_types = pd.DataFrame(edges["highway"].apply(pd.Series)[0].value_counts().reset_index())
street_types.columns = ["type", "count"]
fig, ax = plt.subplots(figsize=(12,10))
sns.barplot(y="type", x="count", data=street_types, ax=ax)
plt.tight_layout()
plt.savefig("barchart.png")

Here is the output bar chart plot for the data. Residential streets have the highest frequency in this dataset.

Bar chart
Bar chart — Street types

We can also use maps to visualise the street data by using any of your favourite Geospatial visualisation tool in Python. I use here Folium.

style = {‘color’: ‘#F7DC6F’, ‘weight’:’1'}
m = folium.Map([-2.914018, 53.366925],
zoom_start=15,
tiles=”CartoDb dark_matter”)
folium.GeoJson(edges.sample(), style_function=lambda x: style).add_to(m)
m.save(“edges.html”)
m

The output is this beautiful map of all streets in Liverpool. No need to download data, upload it and read it with Pandas.

Streets Visualized in Folium
Streets Visualized in Folium

In the next section, we retrieve all buildings available in OpenStreetMap data in Liverpool.

Building Footprints

To retrieve building footprints, we use “footprints_from_place” functionality from OSMnx. We need to pass the name of the place.

buildings = ox.footprints_from_place(place)
buildings.shape

The building dataset has 27329 rows and 185 columns ( Note this might change as OSM users update any feature in this area). Let us see a subset of the buildings dataset we retrieved.

cols = [‘amenity’,’building’, ‘name’, ‘tourism’]
buildings[cols].head()

Building table
Building table

We can also map the buildings. Due to the larger dataset, Folium might not correctly display it in the notebook, but you can save it and open it in a browser.

style_buildings = {‘color’:’#6C3483 ‘, ‘fillColor’: ‘#6C3483 ‘, ‘weight’:’1', ‘fillOpacity’ : 1}
m = folium.Map([ 57.70958, 11.96687],
zoom_start=15,
tiles=”Stamen Toner”)
folium.GeoJson(buildings[:1000], style_function=lambda x: style_buildings).add_to(m)
m

A subset of Building footprints in Liverpool
A subset of Building footprints in Liverpool.

Points of Interest (Cafes)

We can also access and retrieve some other point-based datasets from OpenStreetMap dataset. OSMnx has also ox.pois_from_place() Functionality where you can pass what variable you are interested in the amenity parameter. The list of available amenities is available from OpenStreetMap Wiki.

In this example, we retrieve the cafes in Liverpool.

cafe = ox.pois_from_place(place, amenities=[“cafe”])
cafe.head()

Here is a subset of the cafe dataset, where we have a name, wheelchair availability and opening hours columns. Some of the columns have no data, though.

columns

Finally, let us plot these cafes on a map.

cafe_points = cafe[cafe.geom_type == “Point”]
m = folium.Map([53.366925, -2.914018], zoom_start=10, tiles=”CartoDb dark_matter”)
locs = zip(cafe_points.geometry.y, cafe_points.geometry.x)
#folium.GeoJson(buildings, style_function=lambda x: style_buildings).add_to(m)
for location in locs:
folium.CircleMarker(location=location,
color = “#F4F6F7”, radius=2).add_to(m)
m.save(“cafes.html”)
m

Below is the map of all cafes available in OpenStreetMap database in Liverpool.

Cafes in Liverpool
Cafes in Liverpool

Retrieving the OSM data can be incorporated with any other data analysis or visualisation project of yours. Experiment with any other place of your interest. Please also know that you can contribute OpenStreetMap data if you find your area of interest does not have enough data.

Conclusion

In this tutorial, we retrieved OpenStreetMap data using OSMnx and also processed it with Pandas and Geopandas. We also have seen how to visualise Geospatial data with Folium.

#python #programming #map

Retrieving OpenStreetMap Data Using OSMNX, a Python Package.
2 Likes100.50 GEEK