Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将geojson对象的一部分组合到另一个对象中,并在Python中拆分为键值对_Python_Json_Dictionary_Geojson - Fatal编程技术网

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

将geojson对象的一部分组合到另一个对象中,并在Python中拆分为键值对,python,json,dictionary,geojson,Python,Json,Dictionary,Geojson,我有以下JSON格式的数据: data = { "type": "FeatureCollection", "name": "entities", "features": [{ "type": "Feature", "properties": { "Layer": "0", "SubClasses": "Ac

我有以下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]
                }
            }
        ]
    }
我想根据EntityHandle的值将点的文本键和值组合为LineString,然后转换以下部分文本:HouseID:B6G31;面积:143至房号:B6G31;面积:143,最后删除点线。预期产出为:

{
    "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]
                ]
            }
        }
    ]
}
到目前为止的解决方案,多亏了这里的@dodopy,我已经意识到将点和线串结合起来可以得到以下结果:

import json
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
print(data)
{
    'type': 'FeatureCollection',
    'name': 'entities',
    'features': [{
            'type': 'Feature',
            'properties': {
                'Layer': '0',
                'SubClasses': 'AcDbEntity:AcDbBlockReference',
                'EntityHandle': '33C',
                'Text': 'HouseID: B6G31; Area: 143'
            },
            'geometry': {
                'type': 'LineString',
                'coordinates': [
                    [128.30069876369856, 19.589569788550868],
                    [122.21045961023306, 19.589569886701838],
                    [91.72173671060479, 50.1930739634195],
                    [92.27371017252334, 50.74297719953902],
                    [84.75859656427339, 58.28638302549696],
                    [77.21519073831544, 50.77126941724701],
                    [77.76711130961861, 50.22141886736856],
                    [47.2501493852178, 19.589569788550868],
                    [44.19358734894194, 22.63466787268287],
                    [79.04684685278335, 57.61913923559142],
                    [79.04684685278335, 59.23260037240937],
                    [84.75166060397453, 64.91601778815193],
                    [90.41376187014976, 59.23260037240937],
                    [90.41376187014976, 57.61913923559142],
                    [128.30069876369856, 19.589569788550868]
                ]
            }
        },
        {
            'type': 'Feature',
            'properties': {
                'Layer': '0',
                'SubClasses': 'AcDbEntity:AcDbBlockReference',
                'EntityHandle': '33D',
                'Text': 'HouseID: B622; Area: 31; Type: B'
            },
            'geometry': {
                'type': 'LineString',
                'coordinates': [
                    [74.6823103587667, 53.238171737765796],
                    [79.04684685278335, 57.61913923559142],
                    [79.04684685278335, 59.23260037240937],
                    [81.6956604878942, 61.8913860732074],
                    [75.42085500169195, 68.1426574998851],
                    [67.60077999639907, 60.29314230022384],
                    [74.6823103587667, 53.238171737765796]
                ]
            }
        }
    ]
}

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

我刚刚浏览了字典和列表

本质上,如果缺少文本键,则使用该EntitiyHandle并找到找到的文本值,然后将其拆分。然后,只要将其吸收到没有文本键的元素中(如果有意义的话):

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

只需将json对象转换为字典,并以这种方式进行操作。在python字典中重新排列键和值是相当容易的。请问,completed=[]在代码中有什么用途?我没发现有人用过。谢谢。啊,是的。很抱歉绝对没有用的代码,你可以删除,我忘了把它拿出来。当我在测试/调试时,我就把它放在那里了。我有一个运行列表,列出了它迭代过的实体和没有迭代过的实体。我有时会这样做,而不是简单地打印,看看我的for循环是否像我预期的那样工作