Saving Overpass query results to GeoJSON file with Python - to work on with the data

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
import json

API = overpy.Overpass()

# Fetch schools in Germany
result = API.query("""
out geom;

# Create a GeoJSON dictionary to store the features
geojson = {
    "type": "FeatureCollection",
    "features": []

# Iterate over the result and extract relevant information
for node in result.nodes:
    # Extract coordinates
    lon = float(node.lon)
    lat = float(

    # Create a GeoJSON feature for each node
    feature = {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [lon, lat]
        "properties": {
            "name": node.tags.get("name", "Unnamed School"),
            "amenity": node.tags.get("amenity", "school")
            # Add more properties as needed

    # Append the feature to the feature list

# Write the GeoJSON to a file
with open("schools.geojson", "w") as f:
    json.dump(geojson, f)

print("GeoJSON file created successfully!")

i will add take the data of the query the Overpass API for schools in Germany,

After extraction of the relevant information such as coordinates and school names, i will subsequently then convert this data into GeoJSON format.
Finally, it will write the GeoJSON data to a file named “schools.geojson”.

well with that i will try to adjust the properties included in the GeoJSON as needed.

so see my results i get back on google-colab:

verpassBadRequest                        Traceback (most recent call last)

<ipython-input-3-f0918b7fb5d0> in <cell line: 7>()
      6 # Fetch schools in Germany
----> 7 result = API.query("""
      8 [out:json][timeout:250];
      9 {{geocodeArea:Deutschland}}->.searchArea;

/usr/local/lib/python3.10/dist-packages/overpy/ in query(self, query)
    173                 )
    174                 if not do_retry:
--> 175                     raise current_exception
    176                 retry_exceptions.append(current_exception)
    177                 continue

OverpassBadRequest: Error: line 3: parse error: Unknown type &quot;{&quot; 
Error: line 3: parse error: An empty query is not allowed 
Error: line 3: parse error: ';' expected - '{' found.

Try this query instead (it works in JOSM):

out meta;

Hello dear NKA
first of all - many many thanks for this reply and the awesome idea. This is overwhelming.

i am very happy.

btw: if i am right -the we can export this dataset as GeoJSON - or simmilar and get it shown in a WordPress - widet

guess so !

i will try it out…

You have hepled me alot!
have a great day

Hello @NKA

many thanks for all the hints and that you showed how to handle the data - eg. of a Overpass-Turbo request so that i can use the GeoJson-Formate in a export in order to be able to show in a Leaflet the data - for example in WordPress:

see also: Geojson, properties and Popups – Extensions for Leaflet Map

Extensions for Leaflet Map :: With the WordPress plugin Leaflet Map you can display maps using shortcodes. Extensions for Leaflet Map integrates Leaflet plugins and offers even more functions.

again many many thanks for all the help

TagTheWorld :wink: