liveBook · Manning

Topojson-client

As explained in D3.js in Action, Third Edition

topojson-client is a JavaScript library that converts TopoJSON topology into GeoJSON features and geometries.

topojson-client is a JavaScript library that decodes compact TopoJSON topology into GeoJSON data that D3 can read and manipulate. TopoJSON stores shared borders as single arcs, reducing file size and preventing duplicated strokes. section 13.1.2 section 13.5

Use the library’s conversion methods to prepare geographic layers:

  • Call feature(topology, object) to extract GeoJSON features from a named topology object. section 13.5
  • Call mesh(topology, object) to extract shared borders as a GeoJSON MultiLineString. section 13.5
  • Pass the resulting features and borders to D3’s geographic path generator for SVG rendering. section 13.5

Draw departments from TopoJSON

Install the package with npm i topojson-client, then import it and extract named objects: const departments = topojson.feature(france, france.objects.FRA_adm2).features; const borders = topojson.mesh(france, france.objects.FRA_adm2); Render departments as paths and borders as one additional path. A Mercator projection and geoPath generator convert the geographic data into SVG path commands, while the single border path avoids overlap. section 13.5

Figure 13.3 While shared borders appear twice in GeoJSON, TopoJSON reduces redundancy by merging them into a single arc.
figure

Compare duplicated GeoJSON borders with TopoJSON’s single shared arcs to understand the library’s compact representation. section 13.1.2

How D3.js in Action, Third Edition covers it

How deep it goes
Explains feature() and mesh() through a France map, stopping short of deeper topology decoding internals.
Examples
Uses France’s 96 departments, extracted borders, a Mercator projection, and a map showing Nobel laureate birth cities.
What it assumes
Assumes familiarity with GeoJSON and D3 map drawing while teaching TopoJSON conversion from named topology objects.
What you can do after
Build it from scratch by installing topojson-client, extracting features and borders, and rendering them with D3.
How it connects
Connects TopoJSON with GeoJSON, D3, SVG paths, geographic projections, and shared geographic borders.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.