Skip to content

Instantly share code, notes, and snippets.

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

Embed
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
.DS_Store
build
node_modules
<!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>
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': https://github.com/topojson/topojson/issues/310
#!/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
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.