Reputation: 1217
we have with Overpass API python wrapper a thin Python wrapper around the OpenStreetMap Overpass API https://github.com/mvexel/overpass-api-python-wrapper
we have some Simple example:
import overpass
api = overpass.API()
response = api.Get('node["name"="Salt Lake City"]')
Note that you don't have to include any of the output meta statements. The wrapper will, well, wrap those. we will get our result as a dictionary, which represents the JSON output you would get from the Overpass API directly.
print [(feature['tags']['name'], feature['id']) for feature in response['elements']]
[(u'Salt Lake City', 150935219), (u'Salt Lake City', 585370637), (u'Salt Lake City', 1615721573)]
we can specify the format of the response. By default, we will get GeoJSON using the responseformat parameter. Alternatives are plain JSON (json) and OSM XML (xml), as ouput directly by the Overpass API.
response = api.Get('node["name"="Salt Lake City"]', responseformat="xml")
updated question: can we also get cvs - can we perform a request like below with the python wrapper to the endpoint of overpass turbo?
[out:csv(::id,::type,"name","addr:postcode","addr:city",
"addr:street","addr:housenumber","website"," contact:email=*")][timeout:30];
area[name="Madrid"]->.a;
( node(area.a)[amenity=hospital];
way(area.a)[amenity=hospital];
rel(area.a)[amenity=hospital];);
out;
by the way: i have encountered that the example code in the readme-text is unfortunatly broken: When I try the following:
print( [(feature['tags']['name'], feature['id']) for feature in response['elements']] )
I get the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'elements'
This works, though:
print( [(feature['properties']['name'], feature['id']) for feature in response['features']] )
What do you think?
Upvotes: 1
Views: 1309
Reputation: 2621
You can try this.
import overpass
api = overpass.API()
query = """
[out:csv(::id,::type,"name","addr:postcode","addr:city",
"addr:street","addr:housenumber","website"," contact:email=*")][timeout:30];
area[name="Madrid"]->.a;
( node(area.a)[amenity=hospital];
way(area.a)[amenity=hospital];
rel(area.a)[amenity=hospital];);
out;
"""
resp = api._get_from_overpass(query)
data = [row.split('\t') for row in resp.text.split('\n')]
Output:
for x in data[:5]:
print(x)
# ['@id', '@type', 'name', 'addr:postcode', 'addr:city', 'addr:street', 'addr:housenumber', 'website', ' contact:email=*']
# ['597375537', 'node', 'Centro de especialidades Emigrantes', '', '', '', '', '', '']
# ['1437313175', 'node', '', '', '', '', '', '', '']
# ['1595068136', 'node', '', '', '', '', '', '', '']
# ['2320596216', 'node', '', '', '', '', '', '', '']
Or
api = overpass.API()
query = """
area[name="Madrid"]->.a;
( node(area.a)[amenity=hospital];
way(area.a)[amenity=hospital];
rel(area.a)[amenity=hospital];);
"""
fmt = 'csv(::id,::type,"name","addr:postcode","addr:city","addr:street","addr:housenumber","website"," contact:email=*")'
data = api.get(query, responseformat=fmt)
Output:
for x in data[:5]:
print(x)
# ['@id', '@type', 'name', 'addr:postcode', 'addr:city', 'addr:street', 'addr:housenumber', 'website', ' contact:email=*']
# ['597375537', 'node', 'Centro de especialidades Emigrantes', '', '', '', '', '', '']
# ['1437313175', 'node', '', '', '', '', '', '', '']
# ['1595068136', 'node', '', '', '', '', '', '', '']
# ['2320596216', 'node', '', '', '', '', '', '', '']
Upvotes: 2