给定data.tsv文件:
id code name
1 AL Alabama
2 AK Alaska
4 AZ Arizona
5 AR Arkansas
6 CA California
... ... ...
给定一个topojson.json文件:(结构正确,数值是随机的)
{
"type":"Topology",
"transform":
{
"scale": [0.0015484881821515486,0.0010301030103010299],
"translate":[-5.491666666666662,41.008333333333354]
},
"objects":
{
"states":
{
"type":"GeometryCollection",
"geometries":
[
{"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AL"}},
{"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AK"}}
]
}
},
"arcs":
[
[[2466,9916],[-25,-5],[3,-13]],
[[2357,9852],[1,-2],[1,-2]]
]
}
如何使用公共字段(1)将其他字段(2)的值注入json文件?
1]:data.txt#code
和topojson.txt.objects.states.geometries.properties.code_2
2]:data.txt#name
最终结果应包含:
{"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AL", "name":"Alabama" }},
{"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AK", "name":"Alaska" }},
编辑:接受的回答:
topojson -o final.json -e data.tsv --id-property=code_2,code -p code_2,state=name -- topojson.json
答案 0 :(得分:17)
尝试使用:
topojson -o final.json -e data.tsv --id-property=code_2,code -p code_2,state=name -- topojson.json
应该输出:
{
"type": "Topology",
"transform": {
"scale": [
0.000016880209206372492,
0.000007005401010148724
],
"translate": [
-1.8418800213354616,
51.15278777877789
]
},
"objects": {
"states": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"arcs": [
[
0
]
],
"id": "AK",
"properties": {
"code_2": "AK",
"state": "Alaska"
}
}
]
}
},
"arcs": [
[
[
0,
588
],
[
92,
-294
],
[
91,
-294
],
[
-183,
588
]
]
]
}
来自Command Line Reference wiki:
- id-property 要提升为几何ID
的要素属性的名称
通过将code_2
属性与此选项一起使用,可以将其作为要素ID进行宣传。
在输入属性名称前面添加 + ,以将其值强制转换为数字。
加:
如果 - id-property 引用的属性为null或未定义, 它们是输出几何对象的省略。这样,就产生了 如果输入要素没有,则对象可能没有已定义的ID 具有指定名称的属性。
因此,当您使用+code
和+code_2
时,它们可能是undefined
,因为您无法将AK
字符串值转换为数字。
这里,输入属性“FIPS”被强制转换为数字并用作 特征标识符;同样,名为“FIPS”的列用作 CSV文件中的标识符。 (如果您的CSV文件使用其他列 要素标识符的名称,您可以指定多个ID 属性,例如
--id-property=+FIPS,+id
。)
这就是您必须将code
添加到--id-property=code_2,code
选项的原因。这是映射的方式( topojson.json 的code_2
和 data.tsv 的code
列。
然后,输出属性“失业”从中生成 失败的外部数据文件,失败.tsv,定义输入属性 “速率”
在我们的案例中,-p code_2,state=name
指定我们将保留code_2
属性,并将name
属性重命名为state
。上述文档维基中的属性和外部属性部分可以提供相关信息。
答案 1 :(得分:0)
topojson 软件包已被弃用。以下步骤基于 command-line cartography 工作流程。这些接口更灵活,但使用起来有点复杂。
安装依赖:
npm install d3-dsv ndjson-cli
将 node_modules/.bin 目录添加到路径中,以便您可以轻松运行命令:
PATH=$(npm bin):$PATH
将 tsv 文件转换为换行符分隔的 json 文件:
tsv2json data.tsv -n > data.ndjson
{"id":"1","code":"AL","name":"Alabama"}
{"id":"2","code":"AK","name":"Alaska"}
将 id 列解析为数字:
ndjson-map '{id: +d.id, code: d.code, name: d.name}' < data.ndjson > data_parsed.ndjson
{"id":1,"code":"AL","name":"Alabama"}
{"id":2,"code":"AK","name":"Alaska"}
提取 topojson 文件的几何图形:
ndjson-cat topojson.json | ndjson-split 'd.objects.states.geometries' > topojson_geometries.ndjson
{"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AK"}}
{"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AL"}}
加入两个以换行符分隔的 json 文件:
ndjson-join 'd.properties.code_2' 'd.code' topojson_geometries.ndjson data_parsed.ndjson > geometries_data_join.ndjson
[{"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AK"}},{"id":2,"code":"AK","name":"Alaska"}]
[{"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AL"}},{"id":1,"code":"AL","name":"Alabama"}]
将 name 列添加到 topojson 属性并仅保留 topojson 几何:
ndjson-map 'd[0].properties.name = d[1].name, d[0]' < geometries_data_join.ndjson > geometries_data_merge.ndjson
{"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AK","name":"Alaska"}}
{"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AL","name":"Alabama"}}
将之前的结果转成数组并与原始topojson文件concat:
ndjson-join <(ndjson-cat topojson.json) <(ndjson-reduce < geometries_data_merge.ndjson) > topojson_concat.ndjson
[{
"type": "Topology",
"transform": {
"scale": [0.0015484881821515486, 0.0010301030103010299],
"translate": [-5.491666666666662, 41.008333333333354]
},
"objects": {
"states": {
"type": "GeometryCollection",
"geometries": [{
"type": "Polygon",
"arcs": [[0]],
"properties": {
"code_2": "AK"
}
}, {
"type": "Polygon",
"arcs": [[1]],
"properties": {
"code_2": "AL"
}
}
]
}
},
"arcs": [[[2466, 9916], [-25, -5], [3, -13]], [[2357, 9852], [1, -2], [1, -2]]]
}, [{
"type": "Polygon",
"arcs": [[0]],
"properties": {
"code_2": "AK",
"name": "Alaska"
}
}, {
"type": "Polygon",
"arcs": [[1]],
"properties": {
"code_2": "AL",
"name": "Alabama"
}
}
]
]
覆盖原始topojson文件的几何图形并将其保存为普通的json文件:
ndjson-map 'd[0].objects.states.geometries = d[1], d[0]' < topojson_concat.ndjson > topojson_data.json
{
"type": "Topology",
"transform": {
"scale": [0.0015484881821515486, 0.0010301030103010299],
"translate": [-5.491666666666662, 41.008333333333354]
},
"objects": {
"states": {
"type": "GeometryCollection",
"geometries": [{
"type": "Polygon",
"arcs": [[0]],
"properties": {
"code_2": "AK",
"name": "Alaska"
}
}, {
"type": "Polygon",
"arcs": [[1]],
"properties": {
"code_2": "AL",
"name": "Alabama"
}
}
]
}
},
"arcs": [[[2466, 9916], [-25, -5], [3, -13]], [[2357, 9852], [1, -2], [1, -2]]]
}
一行中的所有命令:
ndjson-join <(ndjson-cat topojson.json) <(ndjson-join 'd.properties.code_2' 'd.code' <(ndjson-cat topojson.json | ndjson-split 'd.objects.states.geometries') <(tsv2json data.tsv -n | ndjson-map '{id: +d.id, code: d.code, name: d.name}') | ndjson-map 'd[0].properties.name = d[1].name, d[0]' | ndjson-reduce) | ndjson-map 'd[0].objects.states.geometries = d[1], d[0]' > topojson_data.json
注意事项: