Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Last active January 24, 2019 20:46
Show Gist options
  • Save diegovalle/10487038 to your computer and use it in GitHub Desktop.
Save diegovalle/10487038 to your computer and use it in GitHub Desktop.
Projected Topojson of Mexican Municipalities

Projected topojson example of a choropleth of Mexican municipalities and states with tooltips and each municipality colored by the number of people who live there

First download the shapefiles of Mexico (AGEBs, Manzanas, Municipios, Estados, etc) 1.4GB from

http://www.diegovalle.net/projects.html#url=%23datasets

Then convert the files 'shps/national/national_municipal.shp' and 'shps/national/national_estatal.shp' to topojson with

topojson \
    --width 960 \
    --height 800 \
    --margin 0 \
    -s .25 \
    --projection 'd3.geo.mercator()' \
    -o national.json \
    --id-property=+CVE_ENT,+CVE_MUN \
    -p +CVE_ENT,+CVE_MUN,NOM_MUN,+POB1 \
    counties=national_municipal.shp \
    states=national_estatal.shp

Based on Mike Bostock's Projected TopoJSON example https://gist.github.com/mbostock/5557726

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.counties {
fill: none;
stroke: #eee;
}
.state-borders {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
}
.boundary {
fill: none;
stroke: #999;
}
.selected {
fill:red
}
.q0-9 { fill:rgb(247,251,255); }
.q1-9 { fill:rgb(222,235,247); }
.q2-9 { fill:rgb(198,219,239); }
.q3-9 { fill:rgb(158,202,225); }
.q4-9 { fill:rgb(107,174,214); }
.q5-9 { fill:rgb(66,146,198); }
.q6-9 { fill:rgb(33,113,181); }
.q7-9 { fill:rgb(8,81,156); }
.q8-9 { fill:rgb(8,48,107); }
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
//Extra width for the tooltips
var width = 1200,
height = 800;
comma = d3.format("0,000");
var quantize = d3.scale.quantize()
//Iztapalapa is the mostpopulated county with ~1816000 people
.domain([0, 1816000])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
var path = d3.geo.path()
.projection(null);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("national.json", function(error, ca) {
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(ca, ca.objects.counties).features)
.enter().append("path")
.attr("class", function(d) { return quantize(+d.properties.POB1); })
.attr("d", path)
.attr("title", function(d) { return +d.properties.POB1; })
.on("mouseover", function(d) {
var xPosition = d3.mouse(this)[0];
var yPosition = d3.mouse(this)[1] - 30;
svg.append("text")
.attr("id", "tooltip")
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "left")
.text(d.properties.NOM_MUN + " - " + comma(d.properties.POB1));
d3.select(this)
.attr("class", "selected");
})
.on("mouseout", function(d) {
d3.select("#tooltip").remove();
d3.select(this)
.transition()
.attr("class", function(d) { return quantize(+d.properties.POB1); })
.duration(250)
});
svg.append("path")
.datum(topojson.mesh(ca, ca.objects.states))
.attr("class", "boundary")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
</script>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment