avatarJuan Nathaniel

Summary

The provided content outlines a tutorial on using the OSMnx Python library to efficiently download, manipulate, and visualize OpenStreetMap (OSM) data for geospatial data science projects, focusing on examples from Manhattan, New York City.

Abstract

The web content details a comprehensive guide on how to leverage the OSMnx library within a Python environment to interact with OSM data. It begins with instructions on setting up the OSMnx library via conda, then demonstrates how to extract and work with Points of Interest (POIs), specifically cafes, within Manhattan. The tutorial also covers the acquisition and visualization of street networks and building footprints, highlighting the conversion of data into GeoDataFrame format for easier manipulation and the use of the folium library for map visualization. The author emphasizes the benefits of using OSMnx to streamline the process of accessing OSM data, which includes avoiding the time-consuming task of manual data downloads and the ability to directly interact with the dataset within a development environment. The tutorial concludes with an invitation for feedback, showcasing the practical application of OSMnx and folium in geospatial analysis.

Opinions

  • The author suggests that manually downloading OSM datasets can be cumbersome and inefficient, positioning OSMnx as a superior alternative.
  • There is an acknowledgment of the potential for incomplete data within OSM due to its community-driven nature, yet the author maintains that the available data is still valuable.
  • The tutorial promotes the use of OSMnx for its ability to reduce computational and memory costs by allowing users to focus on specific areas, such as Manhattan, rather than processing the entire world's dataset.
  • The author expresses enthusiasm for the OSMnx library's native support for visualizing street networks with folium, indicating a preference for this integrated approach.
  • By encouraging readers to subscribe to an email newsletter for additional programming tips and AI research summaries, the author conveys a commitment to ongoing learning and community engagement in the field of geospatial data science.

Working with OpenStreetMap in Python

How to easily acquire, manipulate, and process OpenStreetMap data for your next geospatial data science project

OpenStreetMap (OSM) is an open-source database that allows virtually anyone to edit the underlying geospatial dataset. It can be a reliable source of data, especially since it is continually updated and validated by thousands of volunteers around the world. You can choose to download any OSM dataset on your own, but it will be time consuming and difficult. Not to mention the hassle of having to move back and forth between the data source and your development environment. With OSMnx Python library, however, you can acquire and process OSM dataset directly from your Notebook!

Photo by Andrew Stutesman on Unsplash

In this tutorial, we will be using OSMnx to perform OSM tasks that are otherwise inaccessible outside of our Python environment.

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.

Table of Content
1. Setup
2. Point-of-Interests (POIs)
3. Street
4. Building

Without further ado, let’s begin!

Setup

Install OSMnx via conda using the following command:

conda install osmnx

We will be importing osmnx using the following convention:

import osmnx as ox

We will then define our place to be in New York City instead of the entire world to reduce computational and memory costs.

place = 'Manhattan, New York, USA'
Manhattan area (image by author)

Do subscribe to my email newsletter where I regularly summarize programming tips and AI research papers in plain English and beautiful visualization.

Point-of-Interests (POIs)

First, let’s acquire some cafe points within Manhattan in New York City. We can easily do this by specifying tags dict object inside the geometries_from_place() function as follows:

tags = {'amenity': 'cafe'}
cafe = ox.geometries_from_place(place, tags=tags)
cafe.head()
Cafe within Manhattan (image by author)

Here we see that some have incomplete addresses. This is the downside of OSM being community-driven where the data can be missing. However, the geometry is still intact, and so we can still derive some location-based information out of the dataset.

Now, we try plotting these cafes on the map using folium library.

import folium

And subsetting the first 100 cafes to speed up processing:

cafe_points = cafe[cafe.geom_type == 'Point'][:100]

And specify the map object by drawing the point individually and saving the completed map as an html file.

m = folium.Map([40.754932, -73.984016], zoom_start=10)
locs = zip(cafe_points.geometry.y, cafe_points.geometry.x)
for location in locs:
    folium.CircleMarker(location=location).add_to(m)
    m.save('cafes.html')

If you open cafes.html you will be able to see the following illustration:

Select cafes in Manhattan (image by author)

Looks good so far! Now we will deal with street network from our OSM dataset.

Street

Let’s acquire the graph data for our street network in Manhattan. We specify the network_type to be of type drive to reduce the number of roads we have to process.

graph = ox.graph_from_place(place, network_type='drive')

The graph above is of NetworkX data format and we need to convert it to GeoDataframe format for easy manipulation. This can easily be done by calling the graph_to_gdfs() function as follows:

nodes, streets = ox.graph_to_gdfs(graph)
streets.head()
Street in Manhattan (image by author)

Now our street data is in the familiar GeoDataframe format.

The last thing to do is to again draw the street network using folium . Fortunately for us, OSMnx provides us with this capability natively (as of now, this native support is only valid for NetworkX type of graph data).

ox.folium.plot_graph_folium(graph)

And you will be able to see the streets in Manhattan of NYC!

Street in Manhattan (image by author)

Now the last geometry that OSM supports is that of building footprints.

Building

Similar to POIs and street objects, let’s acquire the building footprints of Manhattan by setting the building tag to True .

tags = {'building':True}
building = ox.geometries_from_place(place, tags)

There are way too many columns for us to have a meaningful analysis. And so let’s reduce the number of columns by selecting key ones.

cols = ['amenity','building', 'name', 'tourism']
building[cols].head()

We get the following information about our buildings in Manhattan.

Building from OSM (image by author)

Nicely done! Let us try visualizing how our building footprints will look like, again using folium . We will limit our building to 1000 for simplicity.

buildings = building[building.geom_type == 'Polygon'][:1000]

And construct the map object:

m = folium.Map([40.754932, -73.984016], zoom_start=10, tiles='CartoDb dark_matter')
folium.GeoJson(buildings[:1000]).add_to(m)

Finally, display the map containing our building footprints.

m

We will be able to see some building footprints (albeit not completely) in Manhattan beautifully!

Building footprint in Manhattan (image by author)

Conclusion

We have managed to acquire, manipulate, and visualize OSM dataset within a Python environment. We have used OSMnx library as an API interface that effectively connects with the OSM API layer, and folium to display the geospatial information derived from GeoDataframe . As always, if you have further feedback, please feel free to leave a response!

Artificial Intelligence
Machine Learning
Data Science
Open Source
Programming
Recommended from ReadMedium