I'm able to retrieve geojson using the following call to the Overpass API for boroughs in London:

import overpass
api = overpass.API()
response = api.get('relation(1811626);>;out;')

However the result for each geo is individual coordinates, not a polygon:

{"features": [{"geometry": {"coordinates": [25.292698, 56.7083821],"type": "Point"}, "id": 1347523073, "properties": {}, "type": "Feature"}, {"geometry": {"coordinates": [25.292627, 56.7080704], "type": "Point"} ... }}

Is it possible to have Overpass return polygons instead of coordinates when querying relations? Or will we need to convert the coordinates to polygons.


It's not possible to pull Geojson directly from OSM / Overpass from what I can tell.

Here's a workaround. First make a request to overpass and return XML. Then convert the XML to Geojson using osm2geojson (originally a JS package but recenlty adapted for PY)

import requests
import osm2geojson

response = requests.get('https://overpass-api.de/api/interpreter?data=[out:xml][timeout:25];(rel(51800););out geom;')
string = response.text.replace("\n","")
geojson = osm2geojson.xml2geojson(string)

In my experience making a get request is easier than learning syntax for various python modules (overpy and overpass)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.