简体   繁体   English

Python包装器在Overpass-API的端点上运行请求

[英]Python wrapper to run requests on the endpoint of overpass-API

we have with Overpass API python wrapper a thin Python wrapper around the OpenStreetMap Overpass API https://github.com/mvexel/overpass-api-python-wrapper 我们使用Overpass API python包装器将围绕OpenStreetMap Overpass API的薄Python包装器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. 请注意,您不必包括任何输出meta语句。 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. 我们将以字典形式获得结果,该字典代表您将从Overpass API直接获得的JSON输出。

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. 默认情况下,我们将使用responseformat参数获取GeoJSON。 Alternatives are plain JSON (json) and OSM XML (xml), as ouput directly by the Overpass API. 替代方法是普通JSON(json)和OSM XML(xml),由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? 更新的问题:我们还可以获取cvs-我们可以使用python包装器对立交桥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? 你怎么看?

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', '', '', '', '', '', '', '']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM