微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在Python中将geojson对象的一部分合并到另一个对象中

像这样的例子:

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中操作JSON文件。在我的数据中,一些多边形具有多个相关信息:坐标(LineString)以及 面积百分比
面积TextAreain Point),我想将它们组合到单个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,35.954489281866685],[44.371068755199758,36.10448936390457],[43.371069617387093,36.104489150107824],23.904496401184584],[48.172716774891342,[48.171892994728751,17.404489374370311],[61.17106949647404,17.404489281863786],19.404489281863786],[61.971069689453991,19.404489282256687],[61.971069681118479,36.504485105673659]
                    ]
                }
            },{
                "type": "Feature","SubClasses": "AcDbEntity:AcDbMText","EntityHandle": "2F1","Text": "6%"
                },"geometry": {
                    "type": "Point","coordinates": [49.745686139884583,28.11445704760262,0.0]
                }
            },"Area": "100"
                },"coordinates": [50.216857362443989,63.981197759829229,"EntityHandle": "2F7"
                },"coordinates": [
                        [62.37106968111857,36.504489398648715],[62.371069689452725,[63.171069496474047,[77.921070051947027,19.504489281855054],[78.671070051947027,[78.671070051897914,36.504485105717322],[62.37106968111857,36.504489398648715]
                    ]
                }
            },"EntityHandle": "2F7","Text": "5.8%"
                },"coordinates": [67.27548061311245,0.0]
                }
            }
        ]
    }

我想结合PointTextArea关键和价值LineString基础上EntityHandle的价值,同时也删除
Point线。预期输出为:

    {
        "type": "FeatureCollection","Text": "6%",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'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。