将geojson对象的一部分合并为另一个对象,并在Python中拆分为键值对

时间:2019-04-03 05:17:15

标签: python json dictionary geojson

我有以下JSON格式数据:

data = {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "33C"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [128.300698763698563, 19.589569788550868],
                        [122.210459610233059, 19.589569886701838],
                        [91.721736710604787, 50.193073963419501],
                        [92.273710172523337, 50.74297719953902],
                        [84.75859656427339, 58.28638302549696],
                        [77.215190738315442, 50.771269417247012],
                        [77.767111309618613, 50.221418867368563],
                        [47.250149385217803, 19.589569788550868],
                        [44.193587348941939, 22.634667872682869],
                        [79.046846852783347, 57.619139235591419],
                        [79.046846852783347, 59.232600372409372],
                        [84.751660603974528, 64.916017788151933],
                        [90.413761870149756, 59.232600372409372],
                        [90.413761870149756, 57.619139235591419],
                        [128.300698763698563, 19.589569788550868]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 8.650739891855252     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END",
                    "EntityHandle": "33C",
                    "Text": "HouseID: B6G31; Area: 143"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [82.573226323750248, 61.543228505735186, 0.0]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "33D"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [74.682310358766699, 53.238171737765796],
                        [79.046846852783347, 57.619139235591419],
                        [79.046846852783347, 59.232600372409372],
                        [81.695660487894202, 61.8913860732074],
                        [75.420855001691947, 68.142657499885104],
                        [67.600779996399069, 60.293142300223842],
                        [74.682310358766699, 53.238171737765796]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 8.650739891855252     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END",
                    "EntityHandle": "33D",
                    "Text": "HouseID: B622; Area: 31; Type: B"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [72.482530938336538, 62.10442248906768, 0.0]
                }
            }
        ]
    }

我想基于Point的值将Text的{​​{1}}键和值组合到LineString,然后将下面的部分EntityHandle转换为"Text": "HouseID: B6G31; Area: 143",最后删除"HouseID": "B6G31"; "Area": "143"行。预期的输出是:

Point

到目前为止,该解决方案要归功于@dodopy,来自Combine part of geojson object into another in Python,我已经意识到将{ "type": "FeatureCollection", "name": "entities", "features": [{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "33C", "HouseID": "B6G31", "Area": "143" }, "geometry": { "type": "LineString", "coordinates": [ [128.300698763698563, 19.589569788550868], [122.210459610233059, 19.589569886701838], [91.721736710604787, 50.193073963419501], [92.273710172523337, 50.74297719953902], [84.75859656427339, 58.28638302549696], [77.215190738315442, 50.771269417247012], [77.767111309618613, 50.221418867368563], [47.250149385217803, 19.589569788550868], [44.193587348941939, 22.634667872682869], [79.046846852783347, 57.619139235591419], [79.046846852783347, 59.232600372409372], [84.751660603974528, 64.916017788151933], [90.413761870149756, 59.232600372409372], [90.413761870149756, 57.619139235591419], [128.300698763698563, 19.589569788550868] ] } }, { "type": "Feature", "properties": { "Layer": "0", "SubClasses": "AcDbEntity:AcDbBlockReference", "EntityHandle": "33D", "HouseID": "B622", "Area": "31", "Type": "B" }, "geometry": { "type": "LineString", "coordinates": [ [74.682310358766699, 53.238171737765796], [79.046846852783347, 57.619139235591419], [79.046846852783347, 59.232600372409372], [81.695660487894202, 61.8913860732074], [75.420855001691947, 68.142657499885104], [67.600779996399069, 60.293142300223842], [74.682310358766699, 53.238171737765796] ] } } ] } Point结合使用可获得以下结果:

LineString

是否可以在Python中获得预期的输出?谢谢。

1 个答案:

答案 0 :(得分:1)

我只是遍历字典和列表。

本质上,如果缺少"Text"键,请使用该EntitiyHandle并找到找到的"Text"值,然后将其拆分。然后将其摄取到没有"Text"键的元素中(如果可以的话):

data = {
        "type": "FeatureCollection",
        "name": "entities",
        "features": [{
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "33C"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [128.300698763698563, 19.589569788550868],
                        [122.210459610233059, 19.589569886701838],
                        [91.721736710604787, 50.193073963419501],
                        [92.273710172523337, 50.74297719953902],
                        [84.75859656427339, 58.28638302549696],
                        [77.215190738315442, 50.771269417247012],
                        [77.767111309618613, 50.221418867368563],
                        [47.250149385217803, 19.589569788550868],
                        [44.193587348941939, 22.634667872682869],
                        [79.046846852783347, 57.619139235591419],
                        [79.046846852783347, 59.232600372409372],
                        [84.751660603974528, 64.916017788151933],
                        [90.413761870149756, 59.232600372409372],
                        [90.413761870149756, 57.619139235591419],
                        [128.300698763698563, 19.589569788550868]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 8.650739891855252     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END",
                    "EntityHandle": "33C",
                    "Text": "HouseID: B6G31; Area: 143"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [82.573226323750248, 61.543228505735186, 0.0]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbBlockReference",
                    "EntityHandle": "33D"
                },
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [74.682310358766699, 53.238171737765796],
                        [79.046846852783347, 57.619139235591419],
                        [79.046846852783347, 59.232600372409372],
                        [81.695660487894202, 61.8913860732074],
                        [75.420855001691947, 68.142657499885104],
                        [67.600779996399069, 60.293142300223842],
                        [74.682310358766699, 53.238171737765796]
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "Layer": "0",
                    "SubClasses": "AcDbEntity:AcDbMText",
                    "ExtendedEntity": "ACAD_MTEXT_COLUMN_INFO_BEGIN     75      2     79      0     76      1     78      0     48 8.650739891855252     49 12.5     50      1 0.0 ACAD_MTEXT_COLUMN_INFO_END",
                    "EntityHandle": "33D",
                    "Text": "HouseID: B622; Area: 31; Type: B"
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [72.482530938336538, 62.10442248906768, 0.0]
                }
            }
        ]
    }







temp_dict = {}
for each in data['features']:
    entity_handle = each['properties']['EntityHandle']
    if 'Text' not in each['properties']:
        continue
    else:
        #temp_dict[entity_handle] = each['properties']['Text']
        text = each['properties']['Text']
        text_split = text.split(';')
        temp_dict2 = {}
        for item in text_split:
            k,v = item.split(':')[0].strip(), item.split(':')[1].strip()
            temp_dict2[k] = v
        temp_dict[entity_handle] = temp_dict2



updated_data = {}
for k,v in data.items():
    if type(v) == list:
        updated_data[k] = []
    else:
        updated_data[k] = v



completed = []
for each in data['features']:
    temp_dict3 = {}
    entity_handle = each['properties']['EntityHandle']
    if 'Text' not in each['properties']:
        temp_dict3.update(each)
        temp_dict3['properties'].update(temp_dict[entity_handle])
        updated_data['features'].append(temp_dict3)
    else:
        continue