Search code examples
node.jsv8topojson

How to create optimal world.topo.json file with IDs?


Given a massive shape file of world countries.

Given I'am using Topojson@1.0 installed locally via npm install topojson@1.0. talled locally via npm install topojson@1.0.

Given I have csv data for many countries such as

FR,144145
EN,5643
DE,25667
ES,3567
US,83466
CN,34576
JA,69353

Given I want to bind that data to the Topojson+D3js generated SVG.

enter image description here

Thus I want a light yet precise world-id.topojson file with the rights properties... so to ease up the CSV-SVG data biding via matchings ids.

So, I go for :

# download GADM
curl \
    -L -C - 'http://biogeo.ucdavis.edu/data/gadm2.8/gadm28_levels.shp.zip' \
    -o ./gadm28_levels.shp.zip
unzip -n ./gadm28_levels.shp.zip -d ./
# Process data
node ./node_modules/topojson/bin/topojson -q 1e4 \
-p name=NAME_ENGL,iso=ISO,iso2=ISO2
-o world-all.json \
-- ./gadm28_adm0.shp

But it fails with Aborted (core dumped). How to proceed ?


EDIT: clean elegant world-id.json, 579.9kb, with iso-639-2, countrynames, and iso-639-3.


Solution

  • Currently via Topojoson@1.0. (version using Topojson@3.0 welcome!)

    Output (Natural Earth) : clean elegant world-id.json, 579.9kb, with iso-639-2, countrynames, and iso-639-3.

    Properties filtering

    Add -p to keep all properties and their values, use nothing to drop them all, and use -p ISO to transmit to your topojson the duo "ISO": "FRA". See Topojson v.1 API

    What we want

    • Data sample / visual :
    {
      "type": "MultiPolygon",
      "arcs": [
        [ [4347,4348,4349] ],
        [ [4350,4350,4351,4352,4353,4354] ],
        [ [4355,4356,4357,4358,4358,4358,4359,4360,4361,-4350,4362,4363,-3047,-1961,-1960,-598], [4364], [4365] ]
      ],
      "properties": {
        "name": "Italy",
        "iso2": "IT",
        "iso3": "ITA"
      }
    },
    

    enter image description here

    GADM data

    # Install topojson v.1 locally
    npm install topojson@1.0
    # Run topojson
    node --max_old_space_size=8000 ./node_modules/topojson/bin/topojson \
         -q 1e4 \
         -p name=NAME_ENGL,iso=ISO,iso2=ISO2 \
         -o world-id.json \
         -- countries=./gadm28_adm0.shp 
    

    Shapefile's data is such :

    {
      "properties": {
        "OBJECTID": 79,
        "ID_0": 79,
        "ISO": "FRA",
        "NAME_ENGLI": "France",
        "NAME_ISO": "FRANCE",
        "NAME_FAO": "France",
        "NAME_LOCAL": "France",
        "NAME_OBSOL": null,
        "NAME_VARIA": null,
        "NAME_NONLA": null,
        "NAME_FRENC": "France",
        "NAME_SPANI": "Francia",
        "NAME_RUSSI": "ФÑанÑиÑ",
        "NAME_ARABI": "ÙرÙسا",
        "NAME_CHINE": "æ³å½",
        "WASPARTOF": null,
        "CONTAINS": null,
        "SOVEREIGN": "France",
        "ISO2": "FR",
        "WWW": null,
        "FIPS": "FR",
        "ISON": 250,
        "VALIDFR": "1944",
        "VALIDTO": "Present",
        "POP2000": 59237668,
        "SQKM": 546728.875,
        "POPSQKM": 108.349258122,
        "UNREGION1": "Western Europe",
        "UNREGION2": "Europe",
        "DEVELOPING": 2,
        "CIS": 0,
        "Transition": 0,
        "OECD": 1,
        "WBREGION": null,
        "WBINCOME": "High income: OECD",
        "WBDEBT": "Debt not classified",
        "WBOTHER": "EMU",
        "CEEAC": 0,
        "CEMAC": 0,
        "CEPLG": 0,
        "COMESA": 0,
        "EAC": 0,
        "ECOWAS": 0,
        "IGAD": 0,
        "IOC": 0,
        "MRU": 0,
        "SACU": 0,
        "UEMOA": 0,
        "UMA": 0,
        "PALOP": 0,
        "PARTA": 0,
        "CACM": 0,
        "EurAsEC": 0,
        "Agadir": 0,
        "SAARC": 0,
        "ASEAN": 0,
        "NAFTA": 0,
        "GCC": 0,
        "CSN": 0,
        "CARICOM": 0,
        "EU": 1,
        "CAN": 0,
        "ACP": 0,
        "Landlocked": 0,
        "AOSIS": 0,
        "SIDS": 0,
        "Islands": 0,
        "LDC": 0,
        "Shape_Leng": 130.51585694,
        "Shape_Area": 64.5133204963
      }
    }
    

    Natural Earth Data

    • Download : 1.3G
    • Input : actual source is just 5M and doesn't crash due to size.
    • Output : elegant world-id.json, 579.9kb.
    Command
    # download NaturalEarthData
    curl \
        -L -C - 'https://github.com/nvkelso/natural-earth-vector/archive/v4.0.0.zip' \
        -o ./ne.shp.zip
    unzip -n ./ne.shp.zip -d ./
    # Install topojson v.1 locally
    npm install topojson@1.0
    # Run topojson
    node ./node_modules/topojson/bin/topojson -q 1e3 --bbox \
         -p name=ADMIN,iso2=WB_A2,iso3=WB_A3 \
         -o world-id.json \
         -- countries=./natural-earth-vector-4.0.0/10m_cultural/ne_10m_admin_0_countries.shp
    

    Note: NE v4.0 data is :

    {
      "properties": {
        "scalerank": 0,
        "featurecla": "Admin-0 country",
        "LABELRANK": 2,
        "SOVEREIGNT": "France",
        "SOV_A3": "FR1",
        "ADM0_DIF": 1,
        "LEVEL": 2,
        "TYPE": "Country",
        "ADMIN": "France",
        "ADM0_A3": "FRA",
        "GEOU_DIF": 0,
        "GEOUNIT": "France",
        "GU_A3": "FRA",
        "SU_DIF": 0,
        "SUBUNIT": "France",
        "SU_A3": "FRA",
        "BRK_DIFF": 0,
        "NAME": "France",
        "NAME_LONG": "France",
        "BRK_A3": "FRA",
        "BRK_NAME": "France",
        "BRK_GROUP": null,
        "ABBREV": "Fr.",
        "POSTAL": "F",
        "FORMAL_EN": "French Republic",
        "FORMAL_FR": null,
        "NAME_CIAWF": "France",
        "NOTE_ADM0": null,
        "NOTE_BRK": null,
        "NAME_SORT": "France",
        "NAME_ALT": null,
        "MAPCOLOR7": 7,
        "MAPCOLOR8": 5,
        "MAPCOLOR9": 9,
        "MAPCOLOR13": 11,
        "POP_EST": 67106161,
        "POP_RANK": 16,
        "GDP_MD_EST": 2699000,
        "POP_YEAR": 2017,
        "LASTCENSUS": -99,
        "GDP_YEAR": 2016,
        "ECONOMY": "1. Developed region: G7",
        "INCOME_GRP": "1. High income: OECD",
        "WIKIPEDIA": -99,
        "FIPS_10_": "FR",
        "ISO_A2": "-99",
        "ISO_A3": "-99",
        "ISO_A3_EH": "-99",
        "ISO_N3": "250",
        "UN_A3": "250",
        "WB_A2": "FR",
        "WB_A3": "FRA",
        "WOE_ID": -90,
        "WOE_ID_EH": 23424819,
        "WOE_NOTE": "Includes only Metropolitan France (including Corsica)",
        "ADM0_A3_IS": "FRA",
        "ADM0_A3_US": "FRA",
        "ADM0_A3_UN": -99,
        "ADM0_A3_WB": -99,
        "CONTINENT": "Europe",
        "REGION_UN": "Europe",
        "SUBREGION": "Western Europe",
        "REGION_WB": "Europe & Central Asia",
        "NAME_LEN": 6,
        "LONG_LEN": 6,
        "ABBREV_LEN": 3,
        "TINY": -99,
        "HOMEPART": 1,
        "MIN_ZOOM": 0,
        "MIN_LABEL": 1.7,
        "MAX_LABEL": 6.7
      }
    }