How to add properties to topojson file?

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": [
            [[2466,9916],[-25,-5],[3,-13]],
            [[2357,9852],[1,-2],[1,-2]]
        ]
    }

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.

Leave a Comment