使用jq为GeoJSON的properties添加新键值对的方法求助
解决方法
你的问题出在原命令直接替换了整个properties对象,而非在原有对象中添加新键值对。正确做法是定位到每个feature的properties字段,将新键值对合并进去。
正确的jq命令
jq --arg dd "$d" '.features[].properties |= . + {date_added: $dd}' /tmp/file-$d.geojson
命令说明
.features[].properties: 精准定位到每个feature下的properties对象|= . + {date_added: $dd}: 将新的date_added键值对合并到原properties对象中,完整保留所有已有字段
执行后结果
执行该命令后,每个feature的properties会保留原有字段并新增date_added键,结果如下:
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 4.991669, 52.324436, 0 ] ] ] ] }, "properties": { "neighbourhood": "Bijlmer-Oost", "neighbourhood_group": null, "date_added": "2022-01-01" } }, { "type": "Feature", "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 5.079162, 52.388654, 0 ] ] ] ] }, "properties": { "neighbourhood": null, "neighbourhood_group": null, "date_added": "2022-01-01" } } ] }
内容的提问来源于stack exchange,提问作者csl
相关产品推荐
相关产品推荐

