如何访问GeoJSON对象中的数据?

问题描述 投票:0回答:1

我使用 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”对象不可订阅

我尝试了标准字典属性:

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

这会抛出:

AttributeError:'GeoJSON'对象没有属性'键','值'...

显然,GeoJSON 对象看起来像字典,但工作方式不同。我如何获得我需要的清单?

python dictionary geojson
1个回答
0
投票

您可以使用

[feature['properties']['id'] for feature in gj['features']]


_filter = lambda x: [feature['properties']['id'] for feature in x['features']]

gj = {
    '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'
            }
        }
    ]
}


print(_filter(gj))


打印

[156304466376583、4152401114851048、957272818356791、195685349071104、494493828463470、5440816592658176、91629042587245 1]

© www.soinside.com 2019 - 2024. All rights reserved.