python - 如何将geojson中的属性置于所有属性之上?
问题描述
我正在尝试feature_id
从属性数组中删除属性并向上移动。
with open('test.geojson', 'r+') as gjson:
data = json.load(gjson)
for l in range(0, len(data['features'])):
data['features'][l]['id'] = data['features'][l]['properties']['feature_id']
del data['features'][l]['properties']['feature_id']
gjson.seek(0)
json.dump(data, gjson, indent=4)
gjson.truncate()
这是输入。
{
"type": "FeatureCollection",
"name": "name",
"features": [
{
"type": "Feature",
"properties": {
"feature_id": "1e181120-2047-4f97-a359-942ef5940da1",
"type": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[...]
]
}
}
]
}
它完成了工作,但在底部添加了属性
{
"type": "FeatureCollection",
"name": "name",
"features": [
{
"type": "Feature",
"properties": {
"type": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[..]
]
},
"id": "1e181120-2047-4f97-a359-942ef5940da1"
}
]
}
如您所见id
,最后添加了,但它应该在之前properties
。
解决方案
您可以为此使用OrderedDict。
with open('test.geojson', 'r+') as gjson:
data = json.load(gjson, object_pairs_hook=OrderedDict)
for l in range(0, len(data['features'])):
d = data['features'][l]
d['id'] = data['features'][l]['properties']['feature_id']
d.move_to_end('id', last=False)
del d['properties']['feature_id']
gjson.seek(0)
json.dump(data, gjson, indent=4)
gjson.truncate()
推荐阅读
- angular - 如何使用angulars controlvalueaccessor触发表单验证
- python - 使用 mrjob 的文本文件中的前十个值
- mysql - Node.js,Mysql TypeError:无法读取未定义的属性“apikey”
- php - JSON 文档必须有效
- python - 在字符串中寻找“mop”、“map”、“mXp”等模式,以小写“m”开头,以小写“p”结尾
- python - 从 np 随机选择中获取数组,避免循环和错误“a must be 1-dimensional”
- css - 平坦的菜单响应断点
- linux - 打开文件/文件夹时,vscode 在 Arch linux 上崩溃
- rust - 预期的 fn 指针,发现闭包
- ios - iOS 14.1 随机显示 RSSI 为 19