Posted on Mon 24 June 2013

This article was translated into: English

Descargar shapefiles de manzanas, AGEB, municipios, estados… (SCINCE 2010)



Update: También pueden descargar shapefiles del censo de población y vivienda 2020.

Mi post de como descargar shapefiles electorales resultó ser muy popular, así que cree otro script para descargar las geografías censales del INEGI. Todos estos datos están disponibles en el sitio web del INEGI, pero son dificiles de encontrar, están disponibles sólo como archivos ejecutables para windows y los links tienden a cambiar cada par de meses.

Los archivos shape usan codepage 1252 y fueron reproyectados a WGS84. Todos los atributos en los shapefiles son del censo 2010.



Ahora la parte difícil, para correr el script se necesita de una versión reciente de innoextract, una que funcione con los archivos creados por la versión 5.5 de Inno Setup. Las versiones disponibles en universe no son lo suficientemente recientes. La versión disponible en este repositorio funciona.

1
2
3
4
sudo add-apt-repository ppa:arx/release
sudo apt-get update
sudo apt-get install innoextract
chmod +x download-census-shp.sh

El script al final de este post crea un directorio shps donde descarga todos los shapefiles

Geografías Censales:

Estados -> Municipios -> Localidades -> AGEB -> Manzana

Municipios

Localidades

AGEB

Manzanas

Ejes viales

Servicios A

Servicios L

Tren Metropolitano, Lindero, etc

Servicios P

Iglesias, Escuelas, Hospitales, etc

Aquí el script para descargar los shapefiles:

#!/bin/bash
# Author: Diego Valle-Jones
# Web: http://www.diegovalle.net
# Purpose: Download shapefiles of manzanas (blocks), agebs (census areas), ejes
# viales (streets), interesting areas and a whole bunch of other stuff
# Note that you'll need a recent version of innoextract
# http://constexpr.org/innoextract/, one that can work with files
# created by version 5.5 of Inno Setup, the one in Ubuntu universe is
# not recent enough. The version available at
# https://launchpad.net/~arx/+archive/release is good enough.
# sudo add-apt-repository ppa:arx/release
# sudo apt-get update
# sudo apt-get install innoextract
# chmod +x download-census-shp.sh
# As of now, this script has not been tested on Windows and Mac
# systems, only on Ubuntu. The script will create a directory
# called 'shps' where all the shapefiles are located, if something
# goes wrong when dowloading be sure to delete it and try again
set -e
# Projection compatible with Google Maps
PROJECTION="+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
# wget command
CURL="curl -fsS --retry 3 "
# The list of shapefiles of manzanas, agebs, etc
declare -a files=("ageb_urb" "eje_vial" "estatal" "loc_rur" "loc_urb"
"manzanas" "municipal" "servicios_a" "servicios_l" "servicios_p")
# List of files for the national (not state level) data
declare -a national_files=("estatal" "loc_urb" "nacional" "loc_rur" "municipal" "zonas_metro")
# State abbreviations
declare -a states=("national" "ags" "bc" "bcs" "camp" "coah" "col" "chis" "chih"
"df" "dgo" "gto" "gro" "hgo" "jal" "mex" "mich" "mor" "nay" "nl" "oax"
"pue" "qro" "qroo" "slp" "sin" "son" "tab" "tamps" "tlax" "ver" "yuc"
"zac");
# Use gdal to reproject, and then rename the shapefiles to include
# a user friendly abbreviation instead of a number
# First argument: directory of shapefiles shps/state_abbreviation
# Second argument: the state abbreviation
# TODO: convert the encoding from windows-1252 to utf-8
function reproject {
name="$3[@]"
arr=("${!name}")
for i in "${arr[@]}"
do
ogr2ogr "$1/$2_$i.shp" "$1"/$i.shp -t_srs "$PROJECTION"
rm "$1"/$i*
done
# rename the extra census data that comes with the shapefiles
cd "$1/tablas"
rename "s/^cpv2010/$2_cpv2010/" cpv2010*
rm -rf cpv2010*
cd ../../..
}
# For each of the 32 states (and national data == 00) download and reproject
for i in $(seq 0 32);
do
# The INEGI uses a leading zero for all one digit numbers
if [ "$i" -lt 10 ]
then
FILENUM="0$i"
else
FILENUM="$i"
fi
# download the files from the inegi server. 'idusr' is the id you get
# when you register at the INEGI (yes, I'm 12 years old)
$CURL "http://www.inegi.org.mx/est/scince/scince2010.aspx?_file=/est/scince/scince2010/Scince2010_$FILENUM.exe&idusr=80085" -o ${states[$i]}_scince.exe
# Extract the shapefiles from the inno setup installer windows
# executable (note that it doesn't allow you to specify the
# directory to extract the files)
innoextract --lowercase --silent ${states[$i]}_scince.exe
# Create a directory called "shps" to store the shapefiles
mkdir -p shps/${states[$i]}
# Copy the shapefiles to the new directory
cp -r app/"$FILENUM"/* shps/${states[$i]}
# Delete the temp files from innoextract
rm -rf app
rm -rf tmp
rm -rf ${states[$i]}_scince.exe
# call the reproject function above
if [ "$i" -eq 0 ]
then
reproject shps/${states[$i]} ${states[$i]} national_files
else
reproject shps/${states[$i]} ${states[$i]} files
fi
# give the server a rest before downloading the next file
sleep 20
done
# You could use the code below to merge all the states into one giant
# shapefile of Mexico. Change '_manzanas' to '_agebs' or '_eje_vial' or whatever
#for file in $(find shps -maxdepth 2 -name "*_manzanas.shp" )
#do
# ogr2ogr -update -append mexico_manzanas.shp $file -f "esri shapefile" -nln merge
#done
# Filter attributes (e.g. only include total population in the dbf)
#ogr2ogr -select POB1 mexico_manazanas.shp merge.shp


P.S. Pueden enviarle un mail al INEGI y decirles lo que opinan de como distribuyen los datos
P.P.S. Si necesitan shapefiles de sitios arqueológicos, ríos, curvas de nivel, etc. prueben con este otro script

Update: También pueden usar docker para descargar los shapefiles si no quieren instalar los requerimientos para correr el script de este post

1
2
3
4
5
6
docker pull diegovalle/scince2010-docker
# shared directory to store the output
mkdir /tmp/scince2010
# run ./download.sh within the container and the files should be ready
# in /tmp/since2010
docker run -v /tmp/scince2010:/shapefiles/shps -i -t diegovalle/scince2010-docker



Unless otherwise stated, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. Privacy policy

Disclaimer: This website is not affiliated with any of the organizations or institutions to which Diego Valle-Jones belongs. All opinions are my own.

Special Projects:

Blogs I like: