使用JQ将Geojson附加到json字段
我有一个正在进行的项目,它创建了一个choropleth地图,其中包含从使用JQ将Geojson附加到json字段,json,jq,Json,Jq,我有一个正在进行的项目,它创建了一个choropleth地图,其中包含从file1.json加载的所有美国县边界,并根据file2.json中的值填充颜色渐变。在以前的迭代中,我只是在file1.json中手动输入值,但现在我想扩展我的映射,使其更加用户友好 file1.json的结构如下: { “类型”:“FeatureCollection”, “特点”:[ { “类型”:“功能”, “财产”:{ “GEO_ID”:“0500000US06001”, “州”:“06”, “县”:“001”,
file1.json
加载的所有美国县边界,并根据file2.json
中的值填充颜色渐变。在以前的迭代中,我只是在file1.json
中手动输入值,但现在我想扩展我的映射,使其更加用户友好
file1.json
的结构如下:
{
“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“财产”:{
“GEO_ID”:“0500000US06001”,
“州”:“06”,
“县”:“001”,
“名称”:“阿拉米达”,
“LSAD”:“县”,
“普查地区”:739.017
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[
-122.30936,
37.77615
],
[
-122.317215,
37.778527
]
]
]
}
},
...
]
}
file2.json
的结构如下:
[
{
“县”:“阿拉米达”,
“计数”:25
},
{
“县”:“阿马多”,
“计数”:1
},
{
“县”:“巴特”,
“计数”:2
},
...
]
我想创建一个新文件,其中包含file1.json
中的所有内容,但将其附加到包含基于County字段的relevent Count字段中
结果如下所示:
[
{
“类型”:“功能”,
“财产”:{
“GEO_ID”:“0500000US06001”,
“州”:“06”,
“县”:“001”,
“名称”:“阿拉米达”,
“计数”:“25”,
“LSAD”:“县”,
“普查地区”:739.017
},
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[
[
-122.30936,
37.77615
],
[
-122.317215,
37.778527
]
]
]
}
},
...
]
我不熟悉使用
jq
,但我已经在PowerShell中充分使用了它。这里有一个test.jq
文件可能会有所帮助
# utility to create lookup table from array of objects
# k is the name to use as the key
# f is a function to compute the value
#
def obj(k;f): reduce .[] as $o ({}; .[$o[k]] = ($o | f));
# create map from county to count
( $file2 | obj("County";.Count) ) as $count
# add .properties.Count to each feature
| .features |= map( .properties.Count = $count[.properties.NAME] )
示例使用假设合适的file1.json
和file2.json
:
$ jq -M --argfile file2 file2.json -f test.jq file1.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"GEO_ID": "0500000US06001",
"STATE": "06",
"COUNTY": "001",
"NAME": "Alameda",
"LSAD": "County",
"CENSUSAREA": 739.017,
"Count": 25
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.30936,
37.77615
],
[
-122.317215,
37.778527
]
]
]
}
}
]
}
我注意到“Count”在示例输出中是一个字符串,但在示例文件2中是一个数字。如果需要将其转换为字符串,可以包括对tostring
的调用。e、 g
.features |= map( .properties.Count = ( $count[.properties.NAME] | tostring ) )
或者,您可以在创建查找表时执行转换,例如
( $file2 | obj("County"; .Count | tostring ) ) as $count