首页 > 解决方案 > Python3 - 如何连接 GeoJSON 的字典?

问题描述

所以这可能是一个非常明显的答案,但我似乎无法掌握它。

我正在尝试建立我所在城市的等值线地图。我在 XY 坐标中有不同区域的边界。

我必须将它与 GeoJSON 规范匹配才能输入 folium。“特征”应该是 JSON 特征的字典。

geojson = {
    "type": "FeatureCollection",
    "features": feature
}

为了构建功能字典,我使用了:

for district in range(len(khobar_districts_xy)):

    feature = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": khobar_districts_xy[district]["boundaries"]},
            "properties": {
                "district_id": khobar_districts_xy[district]["district_id"],
                "city_id": khobar_districts_xy[district]["city_id"],
                "name_en": khobar_districts_xy[district]["name_en"]}
        }

对于单个功能,这种安排是有效的,但是一旦我将它循环到许多地区,它就需要采用以下形式:

{{feature1},{feature2},{feature3}}

我尝试了很多东西,包括写入文件和读取文件,但我似乎一直遇到问题。有没有人有好的解决方案?

完整代码在这里

标签: pythonjsondictionarygeojson

解决方案


“特征”应该是 JSON 特征的字典。

geojson = {
    "type": "FeatureCollection",
    "features": feature
}

我很确定features(注意复数)应该是一个字典列表

[{feature1},{feature2},{feature3}]

这就是它的全部内容。


推荐阅读