【发布时间】:2019-04-03 05:17:15
【问题描述】:
我有以下 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的值将Point的Text的键和值组合成LineString,然后将下面的"Text": "HouseID: B6G31; Area: 143"转换成"HouseID": "B6G31"; "Area": "143",最后删除Point线。预期的输出是:
{
"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 参考这里Combine part of geojson object into another in Python,我已经意识到结合Point 和LineString 得到以下结果:
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 中获得预期的输出?谢谢。
【问题讨论】:
-
只要把json对象变成字典,就可以这样操作了。在 python 字典中重新排列键和值是相当容易的。
标签: python json dictionary geojson