-
-
Save cmdoptesc/16fd87bedcdb161d9c315699547409c7 to your computer and use it in GitHub Desktop.
Municipalities of Mexico II - How to convert a shapefile to TopoJSON by M Bostock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
border: no | |
height: 600 | |
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DS_Store | |
build | |
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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