python - Python 更新 geojson 属性
问题描述
我有一个包含很多特征的 geojson 特征集合数据集。我想使用 json 文件的属性添加/更新每个功能的属性。两个数据集的唯一标识符是“uuid”值。
这是geojson格式:
mtx = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "EPSG:4326"
}
},
"features": [
{
"type": "Feature",
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [
5.36516933279853,
51.5510854507331
]
},
"properties": {
"OBJECTID": 1,
"PK_UID": 1,
"uuid": "1efa8916-c854-465b-80f5-1f02fd25fb31",
"road": "A2",
"lane": 1,
"km": 134.96,
"bearing": 148.02261,
"locid": "A2134.96"
}
},
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [
5.05380200345974,
52.3264095459638
]
},
"properties": {
"OBJECTID": 2,
"PK_UID": 2,
"uuid": "73bf3758-6754-433f-9896-d03c0673ae55",
"road": "A1",
"lane": 3,
"km": 11.593,
"bearing": 113.404253,
"locid": "A111.593"
}
}
]
}
这是json格式:
msi= [
{
"uuid": "1efa8916-c854-465b-80f5-1f02fd25fb31",
"road": "A2",
"carriageway": "R",
"lane": "1",
"km": "134.960",
"display": "blank",
"display_attrs": "{'flashing': 'false'}",
"speedlimit": "null"
},
{
"uuid": "73bf3758-6754-433f-9896-d03c0673ae55",
"road": "A1",
"carriageway": "R",
"lane": "3",
"km": "11.593",
"display": "blank",
"display_attrs": "{'flashing': 'false'}",
"speedlimit": "null"
}
]
那么如何制作一个循环遍历geojson功能并根据“uuid”值使用来自json的匹配属性更新每个功能属性的python脚本呢?
我尝试了这样的事情,但这并没有给我预期的结果:
#Loop over GeoJSON features and update the new properties from msi json
for feat in mtx['features']:
for i in range(len(msi)):
if mtx['features'][i]['properties']['uuid'] == msi[i]['uuid']:
feat ['properties'].update(msi[i])
谢谢你的协助。
解决方案
我会做这样的事情(完全未经测试,所以要小心错误......):
# Turn msi into a dictionary for easy access
msi_dict = {m['uuid']: m for m in msi}
for f in mtx['features']:
f['properties'].update(msi_dict.get(f['properties']['uuid'], {}))
推荐阅读
- php - 如何在 Chrome 上修复 ERR_EMPTY_RESPONSE?(PHP卷曲)
- xml - SAXParseException:未能读取架构文档“http://www.springframework.org/schema/context/spring-context.xsd”
- php - 仅当两个 CSV 文件中都存在 CSV 数据时才打印出来
- java - 有没有办法在没有 BitmapFonts 的情况下在 libgdx 中绘制字符串?
- node.js - “路径”参数必须是字符串类型。接收到的类型对象 - Angular 7
- android - ConstraintLayout - 当文本换行发生时,它将“推开”ImageView。如何避免这种行为?
- symfony - Symfony + FOSRestBundle - 如何允许 NULL 值到配置有自定义表单类型的字段?
- reactjs - 在 ReactJs 中,从 redux 存储或从父级到子级将 props 传递给子组件的最佳实践是什么?
- python - 通过queryset限制在sql调用中查询的django字段到数据库
- elasticsearch - 使用 Elastic4s 客户端查找索引中特定字段的所有不同值