Skip to content

Instantly share code, notes, and snippets.

@adg29
Last active July 24, 2017 04:28
Show Gist options
  • Save adg29/a28218da58f9ad42120cf3804bd79098 to your computer and use it in GitHub Desktop.
Save adg29/a28218da58f9ad42120cf3804bd79098 to your computer and use it in GitHub Desktop.
How to merge external properties from a CSV into a topojson

##External Properties

Please see this visual editor that is available for editing properties of a topojson file.

Please refer to the external properties section of the Command Line Reference for the topojson tool to learn about how to merge properites from an external CSV file into an existing topojson file.

##Merging a region property for each country in Asia and Africa The output topojson should contain a modified properties object for each country in Asia or Africa. For example:

{
                "type": "Polygon",
                "properties": {
                    "name": "Algeria",
                    "region": "Africa"
                },
                "id": "DZA",
                "arcs": [
                    [237, 238, 239, 240, 241, 242, 243, 244]
                ]
            }

The input is likely to be a CSV with two columns. One column will contain the region, the second collumn will contain the name of the country (to allow the tool to map between the country in the CSV to the country entry in the topojson ). For example:

countryName,region
Algeria,Africa
Kenya,Africa
India,Asia

https://github.com/mbostock/topojson/wiki/Command-Line-Reference#external-properties

##Example: Merging regions into a US topojson Try to run the following command with the example files in this repo. It will help you get a sense for how this merge works. From there you can extrapolate to add your own external properties to any topojson.

topojson -o us-regions.json -e regions.csv --id-property=abbreviation,abbr -p +id,state,abbreviation,region -- us-labeled.json

In this example:

  • -o us-regions.json specifies the output file
  • -e regions.csv refers to external CSV with the properties you want to import.
  • --id-property=abbreviation,abbr creates the mapping between the abbreviation property in the original topojson input file and the abbr column in the CSV
  • -p +id,state,abbreviation,region specifies which properites should be kep in the output file.
  • -- us-labeled.json this last part of the command specifies the input file.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
abbr region
CT Northeast
ME Northeast
MA Northeast
NH Northeast
RI Northeast
VT Northeast
NJ Northeast
NY Northeast
PA Northeast
IL Midwest
IN Midwest
MI Midwest
OH Midwest
WI Midwest
IA Midwest
KS Midwest
MN Midwest
MO Midwest
NE Midwest
ND Midwest
SD Midwest
ON Midwest
DE South
FL South
GA South
MD South
NC South
SC South
VA South
DC South
WV South
AL South
KY South
MS South
TN South
AR South
LA South
OK South
TX South
AZ West
CO West
ID West
MT West
NV West
NM West
UT West
WY West
AK West
CA West
HI West
OR West
WA West
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment