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:
geojson_school_map
import overpy
import json
API = overpy.Overpass()
# Fetch schools in Germany
result = API.query("""
[out:json][timeout:250];
{{geocodeArea:Deutschland}}->.searchArea;
nwr[amenity=school][!"isced:level"](area.searchArea);
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(node.lat)
# 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
geojson["features"].append(feature)
# 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>()
5
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/__init__.py 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 "{"
Error: line 3: parse error: An empty query is not allowed
Error: line 3: parse error: ';' expected - '{' found.