在GeoJSON中访问嵌套的Python Dict坐标

时间:2019-08-29 14:28:01

标签: python geojson

我正在从REST服务发出get请求,并返回GeoJSON,但Python将该命令识别为字典。我试图从coordinates键访问嵌套列表值,从Zone_键访问字符串值。这是数据示例:

data =  {
        "type": "FeatureCollection",
        "crs": {
            "type": "name",
            "properties": {
                "name": "EPSG:4326"
            }
        },
        "features": [{
                "type": "Feature",
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -80.1135430232235,
                                33.49892053365546
                            ],
                            [
                                -80.1165139377003,
                                33.499835530094444
                            ],
                            [
                                -80.1170369402652,
                                33.49992051898103
                            ],
                            [
                                -80.11707393820328,
                                33.49992653060032
                            ]
                        ]
                    ]
                },
                "properties": {
                    "Zone_": "H"
                }
            },
            {
                "type": "Feature",
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                -79.62281482439842,
                                33.289964520159124
                            ],
                            [
                                -79.62376378105404,
                                33.29028749972797
                            ],
                            [
                                -79.6247927817771,
                                33.29068750016911
                            ],
                            [
                                -79.62604278223982,
                                33.29121650014533
                            ]
                        ]
                    ]
                },
                "properties": {
                    "Zone_": "I"
                }
            }
        ]
    }

我遇到的问题是,我陷入了尝试从geometry键访问坐标的问题。我正在返回字符串,而且我不知道如何到达coordinates中的嵌套列表。这是我到目前为止的内容:

for x in data['features']:
    for y in x['geometry']:
        print(y)

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

coordinates嵌套在geometry的{​​{1}}内部。因此,您需要相应地访问它。

尝试:

features

这将为您提供嵌套的坐标列表:

for feature in data['features']:
  print("Feature coods:")
  for cood in feature['geometry']['coordinates']:
    print(cood)

答案 1 :(得分:0)

功能是一个列表,因此您必须通过整数索引[0]引用它

for x in data['features'][0]['geometry']['coordinates']:
    print(x)

for x in data['features'][0]['properties']:
    print(x)