Skip to content

Instantly share code, notes, and snippets.

Last active January 8, 2024 19:24
Show Gist options
  • Save diegovalle/5129746 to your computer and use it in GitHub Desktop.
Save diegovalle/5129746 to your computer and use it in GitHub Desktop.
Topojson of Mexican municipalities and states

How to create the topojson map:

curl -o
curl -o
ogr2ogr states.shp Entidades_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
ogr2ogr municipalities.shp Municipios_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
topojson -o mx_tj.json -s 1e-7 -q 1e5 states.shp municipalities.shp -p state_code=+CVE_ENT,state_name=NOM_ENT,mun_code=+CVE_MUN,mun_name=NOM_MUN

Related: Projected Topojson of Mexican Municipalities

<!DOCTYPE html>
<meta charset="utf-8">
<title>Mexico Topojson</title>
<script src=""></script>
<script src=""></script>
var x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([height, 0]);
var width = 960,
height = 500;
var projection = d3.geo.mercator()
.center([-102.34034978813841, 24.012062015793]);
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g");
d3.json("mx_tj.json", function(error, mx) {
.data(topojson.object(mx, mx.objects.municipalities).geometries)
.attr("d", d3.geo.path().projection(projection))
.attr("fill", "transparent")
.style("stroke", "#333")
.style("stroke-width", ".2px")
.attr("class", "muns");
.data(topojson.object(mx, mx.objects.states).geometries)
.attr("d", d3.geo.path().projection(projection))
.attr("fill", "transparent")
.style("stroke", "#333");
Display the source blob
Display the rendered blob
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copy link

Hola, muchas gracias por tu contribución, ¿que tan posible es que puedas compartir el código de implementación en d3.js para poner el nombre de los estados y que permita la interactividad al darle click para obtener una información?

Gracias por tu ayuda.

Copy link

Creo este mapa te puede servir de ejemplo de lo que quieres hacer,

sobretodo el código después del comentario //Adding mouseevents

Copy link

Oye no has hecho lo mismo en cesium?

Copy link

Where did you learn how to use ingei mapserver. I've seen it in a bunch of examples but can't find documentation on how to access the data. Any help would be appreciated.

Copy link

ghost commented Aug 3, 2017

@diegovalle Thanks for your code, was help fully. I try to imitate your procedure, but i currently cat not simulate it. Some changes to create the topojoson map... I used ubuntu 16.04 and i need to create a symlink for nodejs to node because topojson don't work if not and i change the name of the topojson executable to geo2topo. Also i can not convert a .shp to topojson directly using geo2topo, so i need to generate a GeoJSON and then convert it. The problem is the big size of the resulting file, was 11.3mb.

Have you an idea of actually how we can create better resulting files as your own mx_tj.json?

This was my procedure, from the beginning:

install packages and add a symlink

sudo apt-get install nodejs npm
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo npm install topojson

download and unzip

curl -o
curl -o

My procedure to generate the topojson

ogr2ogr -f GeoJSON states.json Entidades_2010_5.shp -lco RFC7946=YES -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
ogr2ogr -f GeoJSON municipalities.json Municipios_2010_5.shp -lco RFC7946=YES -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
geo2topo -o mx_tj.json -s 1e-7 -q 1e5 states.json municipalities.json -p state_code=+CVE_ENT,state_name=NOM_ENT,mun_code=+CVE_MUN,mun_name=NOM_MUN

Copy link

ghost commented Aug 3, 2017


I get the size of your files using this command instead:

ogr2ogr -f GeoJSON -lco COORDINATE_PRECISION=1 states.json Entidades_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
ogr2ogr -f GeoJSON -lco COORDINATE_PRECISION=1 municipalities.json Municipios_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"

Thanks anyway...

Copy link

Vicroni commented Jan 24, 2018

Hi, someone here knows how to do the same but without municipalities only states, please, save my life

Copy link

Hola @diegovalle , ¿hay manera de extraer únicamente uno de los estados con todos sus municipios?

Copy link

Here is a version that works similarly with later versions of the topojson ecosystem. Due to Mike Bostock, the origianl author of the libraries.

Copy link

Avzolem commented Aug 23, 2022 Aqui hay un proyecto que utiliza interactividad y el nombre de los estados encima del mapa :D

Copy link

MJChimal commented Jan 8, 2024

👍 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment