You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Mapillary API返回的GeoJSON对象中提取id列表?

问题

我使用Mapillary API通过坐标获取城市图片,调用API获取目标图片ID的代码如下:

import mapillary as mly
mly.interface.set_access_token('here_is_my_private_token')
gj = mly.interface.get_image_close_to(longitude=11.49385, latitude=48.05053, radius=50)

print(gj)

该调用返回一个GeoJSON对象,打印结果如下:

{'type': 'FeatureCollection', 'features': [{'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.49345874786377, 48.050743224759515]}, 'properties': {'captured_at': 1560688528671, 'compass_angle': 219.55442810059, 'creator_id': 105621578345075, 'id': 156304466376583, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.49367868900299, 48.05092969456331]}, 'properties': {'captured_at': 1560688523518, 'compass_angle': 218.94528198242, 'creator_id': 105621578345075, 'id': 4152401114851048, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493721604347229, 48.0509691400122]}, 'properties': {'captured_at': 1560688522466, 'compass_angle': 216.1079864502, 'creator_id': 105621578345075, 'id': 957272818356791, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493549942970276, 48.05081852995386]}, 'properties': {'captured_at': 1560688526608, 'compass_angle': 216.87623596191, 'creator_id': 105621578345075, 'id': 195685349071104, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493507027626038, 48.0507790843896]}, 'properties': {'captured_at': 1560688527607, 'compass_angle': 217.21737670898, 'creator_id': 105621578345075, 'id': 494493828463470, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493635773658752, 48.05089383503807]}, 'properties': {'captured_at': 1560688524562, 'compass_angle': 217.47109985352, 'creator_id': 105621578345075, 'id': 5440816592658176, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493603587150574, 48.05086514739992]}, 'properties': {'captured_at': 1560688525562, 'compass_angle': 216.0885925293, 'creator_id': 105621578345075, 'id': 916290425872451, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}]}

我需要提取所有'id'键对应的数值组成列表,尝试用嵌套字典的方式访问:

id = gj['features'][0]['properties']['id']
features = gj['features']

但出现报错:

TypeError: 'GeoJSON' object is not subscriptable

尝试使用字典的keys()、values()属性也报错:

keys = gj.keys()
values = gj.values()

报错信息:

AttributeError: 'GeoJSON' object has no attribute 'keys', 'values'...

这个GeoJSON对象外观类似字典但访问方式不同,请问如何获取所需的id列表?

解决方法

Mapillary Python SDK返回的GeoJSON对象是SDK封装的自定义对象,并非原生Python字典,需要通过它的data属性访问内部的字典结构。

提取所有id的代码如下:

# 获取原生字典数据
gj_data = gj.data
# 遍历features提取id
id_list = [feature['properties']['id'] for feature in gj_data['features']]
print(id_list)

说明:

  • 调用gj.data可得到和打印结果一致的原生Python字典,之后就能用常规的字典、列表操作提取数据。
  • 用列表推导式遍历features列表,逐个取出每个feature的properties中的id值,最终组成id列表。

内容的提问来源于stack exchange,提问作者Powknow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:04:56