问题描述
我们具有Overpass API python包装器,它是围绕OpenStreetMap Overpass API的薄Python包装器 https ://github.com/mvexel/overpass-api-python-wrapper
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"]')
请注意,您不必包括任何输出meta语句.包装程序将包装这些文件.我们将以字典的形式获得结果,该字典代表您将从Overpass API直接获得的JSON输出.
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)]
我们可以指定响应的格式.默认情况下,我们将使用responseformat参数获取GeoJSON.替代方法是普通JSON(json)和OSM XML(xml),由Overpass API直接输出.
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")
更新的问题:我们还可以获取cvs-我们可以使用python包装器向天桥涡轮增压端点执行以下请求吗?
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']] )
我得到了错误
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']] )
您怎么看?
推荐答案
您可以尝试一下.
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')]
输出:
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)
输出:
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', '', '', '', '', '', '', '']
这篇关于Python包装器在Overpass-API的端点上运行请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!