block by mbostock 9265674

Mexico

Full Screen

Like the other Mexican municipalities map, but projecting in the client. In response to a Stack Overflow question.

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.municipalities {
  fill: #222;
}

.municipalities :hover {
  fill: orange;
}

.state-boundary {
  fill: none;
  stroke: #fff;
  pointer-events: none;
}

.municipality-boundary {
  fill: none;
  stroke: #fff;
  stroke-opacity: .25;
  stroke-width: .5px;
  pointer-events: none;
}

</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>

var width = 960,
    height = 628;

var projection = d3.geo.conicConformal()
    .rotate([102, 0])
    .center([0, 24])
    .parallels([17.5, 29.5])
    .scale(1850)
    .translate([width / 2, height / 2]);

var path = d3.geo.path()
    .projection(projection);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json("mx.json", function(error, mx) {
  if (error) throw error;

  svg.append("g")
      .attr("class", "municipalities")
    .selectAll("path")
      .data(topojson.feature(mx, mx.objects.municipalities).features)
    .enter().append("path")
      .attr("d", path)
    .append("title")
      .text(function(d) { return d.properties.name; });

  svg.append("path")
      .datum(topojson.mesh(mx, mx.objects.municipalities, function(a, b) { return a.properties.state !== b.properties.state; }))
      .attr("class", "state-boundary")
      .attr("d", path);

  svg.append("path")
      .datum(topojson.mesh(mx, mx.objects.municipalities, function(a, b) { return a.properties.state === b.properties.state && a !== b; }))
      .attr("class", "municipality-boundary")
      .attr("d", path);
});

d3.select(self.frameElement).style("height", height + "px");

</script>

Makefile

GENERATED_FILES = \
	mx.json

.PHONY: all clean

all: $(GENERATED_FILES)

clean:
	rm -rf -- $(GENERATED_FILES) build

build/mgm2010v5_0a.zip:
	mkdir -p $(dir $@)
	curl -o $@ 'http://mapserver.inegi.org.mx/MGN/mgm2010v5_0a.zip'

build/mgm2010v5_0a.shp: build/mgm2010v5_0a.zip
	rm -rf -- $(basename $@)
	mkdir -p $(basename $@)
	unzip -d $(basename $@) $<
	for file in `find $(basename $@) -type f`; do chmod 644 $$file; mv $$file $(basename $@).$${file##*.}; done
	rm -rf -- $(basename $@)
	touch $@

build/municipalities.shp: build/mgm2010v5_0a.shp
	ogr2ogr -f 'ESRI Shapefile' -t_srs EPSG:4326 $@ $<

mx.json: build/municipalities.shp
	node_modules/.bin/topojson --simplify=2e-7 -q 1e5 -o $@ -p name=NOM_MUN -p state=+CVE_ENT -- $<

package.json

{
  "name": "anonymous",
  "version": "0.0.1",
  "private": true,
  "devDependencies": {
    "topojson": "1"
  }
}