block by mbostock c4c27dc3724e1ad3d680a581079b2e9c

Municipalities of Mexico II

Full Screen

index.html

<!DOCTYPE html>
<svg width="960" height="600" fill="none" stroke="#333">
  <path id="municipalities" stroke-width="0.2"></path>
  <path id="states"></path>
</svg>
<script src="https://unpkg.com/d3-array@1"></script>
<script src="https://unpkg.com/d3-collection@1"></script>
<script src="https://unpkg.com/d3-dispatch@1"></script>
<script src="https://unpkg.com/d3-request@1"></script>
<script src="https://unpkg.com/d3-selection@1"></script>
<script src="https://unpkg.com/d3-geo@1"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<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);

  d3.select("#municipalities")
      .datum(municipalities)
      .attr("d", path);

  d3.select("#states")
      .datum(states)
      .attr("d", path);
});

</script>

package.json

{
  "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"
  }
}

prepublish

#!/bin/bash

mkdir -p build

# Download.
curl -z build/estados.zip -o build/estados.zip http://mapserver.inegi.org.mx/MGN/mge2010v5_0.zip
curl -z build/municipios.zip -o build/municipios.zip http://mapserver.inegi.org.mx/MGN/mgm2010v5_0.zip

# Decompress.
unzip -od build build/estados.zip
unzip -od build build/municipios.zip

# 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 'd.properties = {state_code: +d.properties.CVE_ENT, state_name: d.properties.NOM_ENT}, d') \
  municipalities=<(shp2json -n build/municipalities.shp \
    | ndjson-map 'd.properties = {state_code: +d.properties.CVE_ENT, mun_code: +d.properties.CVE_MUN, mun_name: d.properties.NOM_MUN}, d') \
  | toposimplify -s 1e-7 \
  | topoquantize 1e5 \
  > mx.json