嗨,我也是 osmapi
和 python 的新手。我正在编写一个脚本来使用 osmapi 执行一些查询,直到出现此错误并且数据似乎可以在此链接 https://www.openstreetmap.org/way/77517260 上运行。 ,与 xml 响应相同 https://api.openstreetmap.org/api/0.6/way/77517260 .
当我测试另一种方式 ID 时它可以工作,但这个 id 77517260
却不能,这是以下错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~/.pyenv/versions/3.6.4/lib/python3.6/site-packages/osmapi/OsmApi.py in _OsmResponseToDom(self, response, tag, single)
2060 all_data = osm_dom.getElementsByTagName(tag)
-> 2061 first_element = all_data[0]
2062 except (xml.parsers.expat.ExpatError, IndexError) as e:
IndexError: list index out of range
During handling of the above exception, another exception occurred:
XmlResponseInvalidError Traceback (most recent call last)
<ipython-input-20-79d93245d84a> in <module>
----> 1 way = api.NodeWays(77517260)
~/.pyenv/versions/3.6.4/lib/python3.6/site-packages/osmapi/OsmApi.py in NodeWays(self, NodeId)
513 uri = "/api/0.6/node/%d/ways" % NodeId
514 data = self._get(uri)
--> 515 ways = self._OsmResponseToDom(data, tag="way")
516 result = []
517 for way in ways:
~/.pyenv/versions/3.6.4/lib/python3.6/site-packages/osmapi/OsmApi.py in _OsmResponseToDom(self, response, tag, single)
2062 except (xml.parsers.expat.ExpatError, IndexError) as e:
2063 raise XmlResponseInvalidError(
-> 2064 "The XML response from the OSM API is invalid: %r" % e
2065 )
2066
XmlResponseInvalidError: The XML response from the OSM API is invalid: IndexError('list index out of range',)
我的Python代码:
import osmapi as osm
api = osm.OsmApi()
way = api.NodeWays(77517260)
最佳答案
首先 - 您应该在构造函数中传递 url 和凭据:
api = osm.OsmApi(api="https://api.openstreetmap.org", 用户名="用户名", 密码=" secret ")
下一步 -api/0.6/way/{id}
- 也许您正在寻找 WayGet
方法。
代码:
导入 osmapi 为 osm
api = osm.OsmApi(api="https://api.openstreetmap.org", 用户名="用户名", 密码=" secret ")
方式 = api.WayGet(77517260)
关于Python:使用 osmapi 执行查询时列出索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232516/