I have just started to use Python and I would like to make a query to Overpass and store the results in a geospatial format (e.g. GeoJSON).

As far as I know, there is a library called overpy that should be what I am looking for. After reading its documentation I came up with the following code:

import overpy

API = overpy.Overpass()

# Fetch highways within Granollers' city.

result = API.query("""
    // query part for: “highway=*”
    // print results
    out body;

The thing is that I am not familiar at all with python and I don't know how to store result as a GeoJSON file (Honestly, I don't even know what kind of thing result is -I tried with type(result) and I get overpy.Result as an output, which doesn't mean much to my poor knowledge). I have been reading overpy documentation but I haven't been able to figure it out.

  • 2
    Same question as also posted here: github.com/DinoTools/python-overpy/issues/75 - so better follow up there.
    – mmd
    Mar 7 '19 at 15:38
  • Yes, it's me who posted it there too. Unfortunately, the question was originally asked months ago without receiving any attention
    – ccamara
    Mar 8 '19 at 6:35

You could do this using overpass that already returns a geojson object and then, if you want, you can use the geojson package to convert this object into string or save it in a file.

import overpass
import geojson

api = overpass.API()

# api.get already returns a FeatureCollection, a GeoJSON type
res = api.get("""
    // query part for: “highway=*”
    // recurse down to get the nodes, required for the geometry

# if you wnat a a str, then dump as string
#geojson_str = geojson.dumps(res)

# dump as file, if you want to save it in file
with open("./test.geo.json",mode="w") as f:

OBS: Take a look to the line with (._;>;); it is VERY important for the example you provide. Your query is about ways and relations, and if you don't add the last line to the query, the server will respond with ways and relations but no nodes and therefore won't be coordinates and without coordinates the resulting geojson won't have geometries at all.

Finally, be aware that this query retrieves almost 2 MB of data, so consider this when you try to handle the results.

  • Thank you very much for reviving this issue, @carpinchosaurio!
    – ccamara
    Oct 6 '19 at 17:51
  • I tried your solution after installing osmtogeojson via pip, but I am afraid nor osm2gojson nor json2geojson are defined. Possibly I am doing something wrong?
    – ccamara
    Oct 6 '19 at 17:52
  • Hello again, I am afraid that there is an error on the import statement, which should be import osm2geojson (which seems to be a different package than osmtogejson). However, I did not succeed in running the code, as I get some errors ` File "<ipython-input-1-28700f60e0f8>", line 13, in <module> geojson_obj = osm2geojson.json2geojson(res)` amongs others
    – ccamara
    Oct 7 '19 at 8:58
  • I am really sorry @ccamara, you are right my code was wrong, I get confused with some other libraries that I was using for a problem similar to yours, but I fixed this code and tested thoroughly, seems that overpass already returns a geojson so there is no need of osm2geojson anymore, please don't hesitate in asking for any further details, good luck Oct 7 '19 at 11:58
  • Thanks for updating the code. Now it works, except by the fact that it only outputs a regular json, with attributes (OK) but no geometries (KO)
    – ccamara
    Oct 7 '19 at 21:21

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.