20
votes

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
2
I lightly edited the topojson so the both shape are conserved.Hugolpz

2 Answers

18
votes

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.

0
votes

The topojson package has been deprecated. The following steps are based on the command-line cartography workflow. These interfaces are more flexible, but a little bit more complicated to use.

Install dependencies:

npm install d3-dsv ndjson-cli

Add the node_modules/.bin directory to the path so that you can easily run the commands:

PATH=$(npm bin):$PATH

Convert the tsv file into a newline-delimited json file:

tsv2json data.tsv -n > data.ndjson

{"id":"1","code":"AL","name":"Alabama"}
{"id":"2","code":"AK","name":"Alaska"}

Parse the id column as a number:

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

Extract the geometries of the topojson file:

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

Join both newline-delimited json files:

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

Add the name column to the topojson properties and only keep the topojson geometries:

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

Convert the previous result into an array and concat it with the original topojson file:

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

Overwrite the geometries of original topojson file and save it as a normal json file:

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

All commands in one line:

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

Notes:

  • I swapped "AK" and "AL" in the topojson file to check if the join really works.
  • The last command (before the one-liner) only works on the original output and not on the given pretty-printed version, which has newlines in it.
  • I tested the workflow on the subsystem for Linux since ndjson-map does not seem to work properly on Windows currently.