Skip to content

Instantly share code, notes, and snippets.

@ackuser
Forked from arthur-e/GeoJSON_TopoJSON.md
Created July 4, 2023 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ackuser/bfaf27348eacddc51b30bb6453d4df7c to your computer and use it in GitHub Desktop.
Save ackuser/bfaf27348eacddc51b30bb6453d4df7c to your computer and use it in GitHub Desktop.
Installation and use of topojson and geojson command line interfaces for simplifying OGC geometries

Installation

# Requires Node.js and NPM
sudo npm install -g topojson

# Install the geojson-cli
sudo npm install -g geojson

Tasks

Removing Excess Precision

The geojson binary allows for truncating precision but it will only act on TopoJSON documents. So, we first convert our original GeoJSON document (geometry.json) into TopoJSON and then back into GeoJSON while truncating precision.

# Back-up the GeoJSON document, then convert to TopoJSON
cp geometry.json geometry.json.0
topojson -o geometry.topo.json geometry.json

# We have to remove geometry.json because `geojson` won't overwrite it
rm geometry.json; geojson --precision 5 geometry.topo.json

Simplifying Geometry

The topojson binary allows for simplification of arcs where the tolerance is specified in steradians. Check out the table on this Wikipedia page to get an idea of what tolerance you want. Again, to get a GeoJSON document, we use geojson to convert the TopoJSON back into GeoJSON.

# Simplify arcs shorter than 10x-9 steradians, a reasonable threshold to start with
topojson --simplify 0.000000001 -o geometry.topo.json geometry.json

# Convert back to a GeoJSON document (optional)
rm geometry.json; geojson geometry.topo.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment