Obtaining Geospatial Polygons for Administrative Areas of Munich via the Overpass API

Amanda Iglesias Moreno
Python in Plain English
6 min readOct 3, 2023

--

Marlene Haiberger in Unsplash

OpenStreetMap (OSM) is a fantastic source of geospatial data, offering insights into roads, buildings, and more. OSM provides a handy tool called the Overpass API, allowing us to create custom queries for accessing data just like we do in a web browser.

In this article, we’ll focus on obtaining a GeoJSON file that maps out Munich’s neighborhoods. This file could be a valuable resource, particularly for creating heatmaps in Python or in other tools like Power BI. Join us as we dive into OpenStreetMap and discover how it can enhance our understanding of geographic data, starting with Munich’s neighborhoods.

Overpass API: Your Gateway to OpenStreetMap Data Retrieval

OpenStreetMap is a collaborative project that allows users to create, edit, and share geospatial data, providing a free and open alternative to commercial mapping services. The Overpass API is a powerful tool that enables programmers to easily access, query, and retrieve data from the OpenStreetMap (OSM) database. It allows users to obtain detailed information about various map features available in OpenStreetMaps, such as roads, buildings, amenities, and more, based on user-defined queries. It fundamentally facilitates the acquisition of geospatial data corresponding to any observable feature within the OpenStreetMap ecosystem.

Obtaining Geospatial Polygons for Administrative Areas in Munich

The Overpass API can be employed to acquire a wide array of geographical data. In this article, we will describe a specific use case, among the many this API provides. Specifically, we will retrieve data to create a GeoJSON file representing a specific administrative geometry: the neighborhoods of the city of Munich.

Constructing and Understanding Overpass API Query

The following Overpass API query facilitates the retrieval of administrative polygons pertaining to the Bezirksteil in Munich.

Next, we will undertake a detailed examination of each element within the Overpass API query, in a sequential manner, with the aim of achieving a deeper comprehension of the query structure.

The components of the query are the following:

  • [out:json][timeout:25];: specifies JSON output format and a query timeout of 25 seconds.
  • area["name"="München"]->.searchArea;: defines an area filter for Munich and assigns it to the searchArea set.
  • relation["admin_level"="10"]["boundary"="administrative"](area.searchArea);: retrieves administrative boundaries (admin_level 10) within the Munich area.
  • out geom;: outputs the geometries (shapes) of the matched administrative boundaries.

This query fetches and outputs the geometries of the administrative boundaries for Munich (admin_level 10) from OpenStreetMap data within a specified timeout. The administrative boundary denoted as ‘Level 10’ corresponds to the regions known as ‘Stadtviertel’ in the German context.

The JSON output resulting from the query includes four primary keys: version, generator, osm3s, and elements. Among these, elements holds paramount significance, as it encapsulates the requested data from the query. This key, an array, contains information pertaining to OpenStreetMap (OSM) elements, such as nodes, ways, and relations, that meet the specified query criteria. Each element within this array is characterized by a distinct set of key-value pairs. In this particular instance, all elements within the elements array include the following keys: type, id, bounds, members, and tags, as illustrated below.

Each element in the JSON output corresponds to a polygon representing a neighborhood. The members key within each element contains the latitude and longitude coordinates of all the nodes that collectively define the polygon. Once this output is obtained, we will convert this data into Shapely polygons, which will be utilized to generate the GeoJSON file.

Locate the Appropriate Administrative Region Within the Documentation

We want to retrieve the neighborhoods within the city of Munich. To determine the appropriate value to assign to the admin_level parameter in the query, it is necessary to refer to the table provided in the OpenStreetMap Wiki, accessible through the following link: https://wiki.openstreetmap.org/wiki/Tag:boundary%3Dadministrative

Administrative Levels — OpenStreetMap Wiki

As shown in the table linked above, in order to retrieve the neighborhoods of the city of Munich, it is necessary to set a value of 10 for the admin_level parameter.

Conversion of JSON Output into a GeoJSON File

After obtaining the API response, we need to process the acquired data to derive a collection of polygons, each of which represents an individual neighborhood within the city of Munich. Below, you will find the code responsible for executing this transformation.

Convert the API response into a list of polygons

This code processes the output of the Overpass API and transforms it into a list of polygons, with each polygon representing a neighborhood in Munich. Its primary focus is on handling relation elements and converting them into polygons. Here’s a brief explanation of the steps taken by the code:

  1. It iterates through elements in the elements array, specifically searching for elements with the type set to relation, which typically represent groups of geographical features.
  2. For each relation element, it extracts way members, which consist of sequences of geographical coordinates.
  3. It converts these sequences of coordinates into Shapely LineString objects, representing connected line segments.
  4. The code then merges these LineString objects to create a single geometry that represents the combined boundaries of the relation.
  5. Finally, it converts the merged boundaries into polygons and stores these polygons in the polygons list, along with their associated names for reference.

After obtaining a list of polygons, our next step is to convert them into a GeoDataFrame, which can be subsequently saved as a GeoJSON file. The following code demonstrates this conversion process, and it also includes a visualization of the obtained polygons.

Multipolygons representing Munich’s neighborhoods (Image created by the author)

Instances of Administrative Polygons Retrieved from OpenStreetMap

By following a similar process, we can obtain the polygons of other administrative regions. By varying the name and admin_level parameters in the previously shown query, you can retrieve a wide range of JSON files containing different administrative regions from various locations. For instance, you can access data on the comarcas (regions) of the Valencian Community in Spain or the Stadtbezirke (city districts) of Munich, as illustrated in the following images.

Stadtbezirke in Munich and Comarcas in Valencia (Image created by the author)

Utilizing Obtained Polygons for Enhanced Geovisualizations

As shown before, acquiring a set of multi-polygons for a particular administrative region in OpenStreetMap is easily achievable through the processing of the information provided by the Overpass API. These multi-polygons can be converted into a GeoJSON file, which we can subsequently utilize to create heatmaps in Python or in other visualization tools, such as Power BI.

Article Overview

GeoJSON files of an administrative region serve as a valuable tool for visualizing various quantitative or qualitative variables across a specific geographic area. While numerous GeoJSON files are readily available on different GitHub repositories or data repositories, there are instances where this valuable geographic information can be found on geographic data portals like OpenStreetMap, rather than being directly downloadable in GeoJSON format from a data repository. However, the fact that these files cannot be downloaded with a simple click of a button does not mean that this information is inaccessible.

The Overpass API is a versatile tool that allows for tailored access to OpenStreetMap data. In our case, we used it to extract coordinates outlining Munich’s neighborhoods. With Python, we converted these coordinates into multi-polygons and then into a GeoJSON file. This GeoJSON file can be used for Python-based visualizations or exported to tools like Power BI for further analysis.

Amanda Iglesias

In Plain English

Thank you for being a part of our community! Before you go:

--

--