Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Given a data.tsv file such :

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

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]]
    ]
}

How to use the common fields(1) to inject the values of an other field(2) into the json file ?

1]: data.txt#code and topojson.txt.objects.states.geometries.properties.code_2

2]: data.txt#name

The end result should contains :

            {"type":"Polygon","arcs":[[0]],"properties":{"code_2":"AL", "name":"Alabama" }},
            {"type":"Polygon","arcs":[[1]],"properties":{"code_2":"AK", "name":"Alaska" }},

EDIT: Accepted answer:

topojson -o final.json -e data.tsv --id-property=code_2,code -p code_2,state=name -- topojson.json
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
806 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try using this:

topojson -o final.json -e data.tsv --id-property=code_2,code -p code_2,state=name -- topojson.json

Which should output:

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

From the Command Line Reference wiki:

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

Plus:

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.

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.

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

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...