python - 在 Python 中将一部分 geojson 对象合并到另一个对象中

标签 python json dictionary geojson

编辑: 我正在尝试在 Python 中操作 JSON 文件。在我的数据中,一些多边形具有多个相关信息:坐标(LineString)和面积百分比面积(TextPoint 中的 Area),我想将它们组合成一个 JSON 对象。例如,文件中的数据如下:

data = {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "2F1"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [61.971069681118479, 36.504485105673659],
                        [46.471068755199667, 36.504485105673659],
                        [46.471068755199667, 35.954489281866685],
                        [44.371068755199758, 35.954489281866685],
                        [44.371068755199758, 36.10448936390457],
                        [43.371069617387093, 36.104489150107824],
                        [43.371069617387093, 23.904496401184584],
                        [48.172716774891342, 23.904496401184584],
                        [48.171892994728751, 17.404489374370311],
                        [61.17106949647404, 17.404489281863786],
                        [61.17106949647404, 19.404489281863786],
                        [61.971069689453991, 19.404489282256687],
                        [61.971069681118479, 36.504485105673659]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "EntityHandle": "2F1",
                    "Text": "6%"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [49.745686139884583, 28.11445704760262, 0.0]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "EntityHandle": "2F1",
                    "Area": "100"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [50.216857362443989, 63.981197759829229, 0.0]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "2F7"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [62.37106968111857, 36.504489398648715],
                        [62.371069689452725, 19.404489281863786],
                        [63.171069496474047, 19.404489281863786],
                        [63.171069496474047, 17.404489281863786],
                        [77.921070051947027, 17.404489281863786],
                        [77.921070051947027, 19.504489281855054],
                        [78.671070051947027, 19.504489281855054],
                        [78.671070051897914, 36.504485105717322],
                        [62.37106968111857, 36.504489398648715]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "EntityHandle": "2F7",
                    "Text": "5.8%"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [67.27548061311245, 28.11445704760262, 0.0]
                }
            }
        ]
    }

我想基于PointTextArea键和值组合到LineString >EntityHandle 的值,并删除 Point 线。预期输出为:

    {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "2F1",
                    "Text": "6%",
                    "Area": "100"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [61.971069681118479, 36.504485105673659],
                        [46.471068755199667, 36.504485105673659],
                        [46.471068755199667, 35.954489281866685],
                        [44.371068755199758, 35.954489281866685],
                        [44.371068755199758, 36.10448936390457],
                        [43.371069617387093, 36.104489150107824],
                        [43.371069617387093, 23.904496401184584],
                        [48.172716774891342, 23.904496401184584],
                        [48.171892994728751, 17.404489374370311],
                        [61.17106949647404, 17.404489281863786],
                        [61.17106949647404, 19.404489281863786],
                        [61.971069689453991, 19.404489282256687],
                        [61.971069681118479, 36.504485105673659]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "2F7",
                    "Text": "5.8%"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [62.37106968111857, 36.504489398648715],
                        [62.371069689452725, 19.404489281863786],
                        [63.171069496474047, 19.404489281863786],
                        [63.171069496474047, 17.404489281863786],
                        [77.921070051947027, 17.404489281863786],
                        [77.921070051947027, 19.504489281855054],
                        [78.671070051947027, 19.504489281855054],
                        [78.671070051897914, 36.504485105717322],
                        [62.37106968111857, 36.504489398648715]
                    ]
                }
            }
        ]
    }

用Python可以得到上面的结果吗?谢谢。

更新的解决方案,感谢@dodopy:

import json
features = data["features"]
point_handle_text = {
    i["properties"]["EntityHandle"]: i["properties"]["Text"]
    for i in features
    if i["geometry"]["type"] == "Point"
}
point_handle_area = {
    i["properties"]["EntityHandle"]: i["properties"]["Area"]
    for i in features
    if i["geometry"]["type"] == "Point"
}
combine_features = []
for i in features:
    if i["geometry"]["type"] == "LineString":
        i["properties"]["Text"] = point_handle_text.get(i["properties"]["EntityHandle"])
        combine_features.append(i)
data["features"] = combine_features

combine_features = []
for i in features:
    if i["geometry"]["type"] == "LineString":
        i["properties"]["Area"] = point_handle_area.get(i["properties"]["EntityHandle"])
        combine_features.append(i)
data["features"] = combine_features

with open('test.geojson', 'w+') as f:
    json.dump(data, f, indent=2)

但是我收到错误:

Traceback (most recent call last):
  File "<ipython-input-131-d132c8854a9c>", line 6, in <module>
    for i in features
  File "<ipython-input-131-d132c8854a9c>", line 7, in <dictcomp>
    if i["geometry"]["type"] == "Point"
KeyError: 'Text'

最佳答案

像这样的例子:

import json
data = json.loads(json_data)
features = data["features"]
point_handle_text = {
    i["properties"]["EntityHandle"]: i["properties"]["Text"]
    for i in features
    if i["geometry"]["type"] == "Point"
}
combine_features = []
for i in features:
    if i["geometry"]["type"] == "LineString":
        i["properties"]["Text"] = point_handle_text.get(i["properties"]["EntityHandle"])
        combine_features.append(i)
data["features"] = combine_features
json_data = json.dumps(data)

关于python - 在 Python 中将一部分 geojson 对象合并到另一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411549/

相关文章:

python - 将 href 添加到 panda .read_html DF

python - Ansible 剧本-Pysnow : Find record using 'IN' clause query

javascript - 使用 JSON 解析 XML,也包含 XML,嵌入到标签的属性中

Python dict.get() 锁

python - matplotlib for 循环显示、保存和重绘所有图

python - 请求 : Module works locally but failed on pythonanywhere

c# - JavaScriptSerializer.Deserialize 不接受由 JavaScriptSerializer.Serialize 序列化的日期

json - 如何将 ElasticSearch 快照从存储库下载到本地计算机?

Java 遍历 HashMap<?, List<?>> 并为每个 Key 计算 Key 有多少个 Values

python - 按列名和多索引向多索引数据框添加值