如何添加属性到topojson文件? [英] How to add properties to topojson file?

查看:187
本文介绍了如何添加属性到topojson文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个data.tsv文件,例如:

id  code    name
1   AL  Alabama
2   AK  Alaska
4   AZ  Arizona
5   AR  Arkansas
6   CA  California
... ... ...

给出一个topojson.json文件,例如:(结构正确,数字值是随机的)

Given a topojson.json file such : (the structure is correct, the numeral values are random)

{ 
"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#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
            ]
        ]
    ]
}

命令行参考Wiki :

-id属性要升级为几何ID的要素属性的名称

--id-property name of feature property to promote to geometry id

通过将code_2属性与此选项结合使用,可以将其提升为功能ID.

By using the code_2 property with this option, you promote it as the feature ID.

在输入属性名称的前面加上 + ,以将其值强制为数字.

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

加:

如果-id-property 引用的属性为空或未定义, 它们从输出几何对象中被省略.因此, 如果输入要素不具有定义的ID,则对象可能没有定义的ID. 具有指定名称的属性.

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字符串值转换为数字.

So, when you are using +code and +code_2, they are probably undefined, as you can't convert the AK string value to a number.

此处,输入属性"FIPS"被强制为数字,并用作 特征标识符;同样,名为"FIPS"的列用作 CSV文件中的标识符. (如果您的CSV文件使用其他列 功能标识符的名称,您可以指定多个ID 属性,例如--id-property=+FIPS,+id.)

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列).

That's why you have to add the code to the --id-property=code_2,code option. This is how the mapping is made (the code_2 from topojson.json and the code column from data.tsv).

然后,从 外部数据文件失业.tsv,它定义了输入属性 评分"

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属性重命名为state.前述文档Wiki中的属性外部属性部分在此问题上提供了很多信息.

In our case, -p code_2,state=name specifies that we will preserve the code_2 property and we will rename the name property to state. The Properties and External Properties sections in the aforementioned documentation wiki are pretty informative on the matter.

这篇关于如何添加属性到topojson文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆