Skip to content

Instantly share code, notes, and snippets.

@diegovalle diegovalle/download15.sh
Last active Jan 18, 2016

Embed
What would you like to do?
Preliminary shapefiles from the the Encuesta Intercensal 2015
#! /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 from
# the Encuesta Intercensal 2015
# 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
#Exit on error
set -e
# Projection compatible with Google Maps
PROJECTION="+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
# wget command
WGET="wget -w 5 --random-wait --tries=10"
#The way the INEGI download url starts
URL="http://internet.contenidos.inegi.org.mx/contenidos/Productos/prod_serv/contenidos/espanol/bvinegi/productos/geografia/inter_2015/"
#File ends in
STEM="_s.zip"
#State names in the URL
declare -a state_names=("Aguascalientes" "Baja_California" "Baja_California_Sur" "Campeche" "Coahuila_de_Zaragoza" "Colima" "Chiapas" "Chihuahua" "Distrito_Federal" "Durango" "Guanajuato" "Guerrero" "Hidalgo" "Jalisco" "Mexico" "Michoacan_de_Ocampo" "Morelos" "Nayarit" "Nuevo_Leon" "Oaxaca" "Puebla" "Queretaro" "Quintana_Roo" "San_Luis_Potosi" "Sinaloa" "Sonora" "Tabasco" "Tamaulipas" "Tlaxcala" "Veracruz_de_Ignacio_de_la_Llave" "Yucatan" "Zacatecas" )
# The list of shapefiles of manzanas, agebs, etc
# m and M (uppercase) because sometimes the filenames come in uppercase
declare -a files=("ar.shp" "ent.shp" "lpr.shp" "m.shp" "sia.shp" "sip.shp"
"a.shp" "e.shp" "l.shp" "mun.shp" "sil.shp");
#The INEGI filenames are unreadable
declare -a files_nice=("ageb_rural.shp" "entidad.shp" "localidad_rural_no_amanzanada.shp" "manzana.shp" "servicios_area.shp" "servicios_puntual.shp"
"ageb_urbana.shp" "eje_vial.shp" "localidad_urbana_y_rural_amanzanada.shp" "municipio.shp" "servicios_linea.shp");
# State abbreviations
declare -a states=("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");
declare -a state_num=(`seq -s " " -w 1 32`);
# 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 `seq 0 10`
do
if [ -f $1/cartografбa/$4${files[$i]} ];
then
echo "Creating... " "$1"/$2_${files_nice[$i]}
ogr2ogr "$1"/$2_${files_nice[$i]} $1/cartografбa/$4${files[$i]} -t_srs "$PROJECTION"
else
echo "Failed on " "$1"/$2_${files_nice[$i]}
#exit 1
fi
done
rm -rf "$1"/cartografбa
}
#Ags == 702825004482, Zac == 702825004513
for i in `seq 702825004482 702825004513`;
do
FILENAME="$URL${state_names[$i-702825004482]}/$i$STEM"
# download the files from the inegi server.
$WGET $FILENAME -O ${states[$i-702825004482]}.zip
# Extract the shapefiles from the zip file
mkdir -p shps/${states[$i-702825004482]}
unzip -L ${states[$i-702825004482]}.zip -d shps/${states[$i-702825004482]}
reproject shps/${states[$i-702825004482]} ${states[$i-702825004482]} files ${state_num[$i-702825004482]}
# give the server a rest before downloading the next file
sleep 2
done
# Delete the downloaded zip files
rm -rf *.zip
# You could use the code below to merge all the states into one giant
# shapefile of Mexico. Change "*localidad_urbana.shp" to '*ageb_urbana.shp' or '*eje_vial.shp' or whatever
#file="merge.shp"
#for i in $(find . -maxdepth 3 -name "*localidad_urbana.shp" )
#do
# if [ -f "$file" ]
# then
# echo "creating merge.shp"
# ogr2ogr -f 'ESRI Shapefile' -update -append $file $i -nln merge
# else
# echo "merging……"
# ogr2ogr -f 'ESRI Shapefile' $file $i
# fi
#done
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.