Skip to content

Instantly share code, notes, and snippets.

@cmdoptesc cmdoptesc/.block forked from mbostock/.block
Last active Feb 14, 2018

What would you like to do?
Municipalities of Mexico II - How to convert a shapefile to TopoJSON by M Bostock
border: no
height: 600
license: gpl-3.0
<!DOCTYPE html>
<svg width="960" height="600" fill="none" stroke="#333">
<path id="municipalities" stroke-width="0.2"></path>
<path id="states"></path>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
var projection = d3.geoMercator(),
path = d3.geoPath(projection);
d3.json("mx.json", function(error, mx) {
if (error) throw error;
var states = topojson.feature(mx, mx.objects.states),
municipalities = topojson.feature(mx, mx.objects.municipalities);
projection.fitSize([960, 600], states);"#municipalities")
.attr("d", path);"#states")
.attr("d", path);
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
"scripts": {
"prepublish": "bash prepublish"
"devDependencies": {
"d3-geo": "^1.6.4",
"ndjson-cli": "^0.3.1",
"shapefile": "^0.6.5",
"topojson-client": "^3.0.0",
"topojson-server": "^3.0.0",
"topojson-simplify": "^3.0.2"
# original discussion between Mike Bostock and 'lestcape':
mkdir -p build
# Download.
curl -z build/ -o build/
curl -z build/ -o build/
# Decompress.
unzip -od build build/
unzip -od build build/
# Reproject to WGS84.
ogr2ogr build/states.shp build/Entidades_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
ogr2ogr build/municipalities.shp build/Municipios_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
# shp2json - convert shapefiles to GeoJSON.
# ndjson-map - map property names and coerce numeric properties.
# geo2topo - convert GeoJSON to TopoJSON.
# toposimplify - simplify TopoJSON.
# topoquantize - quantize TopoJSON.
geo2topo -n \
states=<(shp2json -n build/states.shp \
| ndjson-map ' = {state_code:, state_name:}, d') \
municipalities=<(shp2json -n build/municipalities.shp \
| ndjson-map ' = {state_code:, mun_code:, mun_name:}, d') \
| toposimplify -s 1e-7 \
| topoquantize 1e5 \
> mx.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.