gpt4 book ai didi

json - 如何向 topojson 文件添加属性?

转载 作者:行者123 更新时间:2023-12-03 13:33:05 26 4
gpt4 key购买 nike

给定一个 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]]
]
}

如何使用公共(public)字段(1)将另一个字段(2)的值注入(inject)到 json 文件中?

1]: data.txt#codetopojson.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

最佳答案

尝试使用这个:

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 name of feature property to promote to geometry id



通过使用 code_2使用此选项的属性,您将其提升为功能 ID。

Prepend a + in front of the input property name to coerce its value to a number.



加:

If the properties referenced by --id-property are null or undefined, they are omitted from the output geometry object. Thus, the generated objects may not have a defined ID if the input features did not have a property with the specified name.



所以,当你使用 +code+code_2 ,他们可能是 undefined ,因为您无法转换 AK将字符串值转换为数字。

Here, the input property "FIPS" is coerced to a number and used as the feature identifier; likewise, the column named "FIPS" is used as the identifier in the CSV file. (If your CSV file uses a different column name for the feature identifier, you can specify multiple id properties, such as --id-property=+FIPS,+id.)



这就是为什么您必须添加 code--id-property=code_2,code选项。这就是映射的制作方式(topojson.json 中的 code_2 和 data.tsv 中的 code 列)。

Then, the output property "unemployment" is generated from the external data file, unemployment.tsv, which defines the input property "rate"



在我们的例子中, -p code_2,state=name指定我们将保留 code_2属性,我们将重命名 name属性(property)给 state . 房产 外部属性 上述文档 wiki 中的部分对此事提供了相当丰富的信息。

关于json - 如何向 topojson 文件添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18444261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com