Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Last active February 5, 2019 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegovalle/7d50aa2edd3934970b6f20e3361231a7 to your computer and use it in GitHub Desktop.
Save diegovalle/7d50aa2edd3934970b6f20e3361231a7 to your computer and use it in GitHub Desktop.
Download electoral cartography from the INE
#!/bin/bash
# Author: Diego Valle-Jones
# Web: http://www.diegovalle.net
# Purpose: Script to download electoral shapefiles
# from http://cartografia.ife.org.mx/sige7/?distritacion=federal
set -euo pipefail
# Be sure to update this variable when the INE updates the shapefiles
DATE="13jul2016"
#set -e
# State abbreviations
declare -a states=("" "ags" "bc" "bcs" "camp" "coah" "col"
"chis" "chih" "cdmx" "dgo" "gto" "gro" "hgo" "jal"
"mex" "mich" "mor" "nay" "nl" "oax" "pue" "qro"
"qroo" "slp" "sin" "son" "tab" "tamps" "tlax" "ver"
"yuc" "zac");
# wget command
CURL="curl -fsS --retry 3 "
mkdir -p $DATE/shps/federal
mkdir -p $DATE/shps/local
# Federal
for i in {1..32}
do
# The INE uses a leading zero for all one digit numbers
if [ "$i" -lt 10 ]
then
FILENUM="0$i"
else
FILENUM="$i"
fi
TMPDIR=$(mktemp -d ine.federal.XXXXXXXXX)
$CURL http://cartografia.ife.org.mx//descargas/distritacion2017/federal/$FILENUM/$FILENUM.zip -o "${states[$i]}".zip
unzip -o -j -LL -d $TMPDIR "${states[$i]}".zip
mkdir -p "$DATE/shps/federal/${states[$i]}"
mv $TMPDIR/* "$DATE/shps/federal/${states[$i]}"
rm "${states[$i]}".zip && rm -rf $TMPDIR
done
# Local
for i in {1..32}
do
# The INE uses a leading zero for all one digit numbers
if [ "$i" -lt 10 ]
then
FILENUM="0$i"
else
FILENUM="$i"
fi
TMPDIR=$(mktemp -d ine.local.XXXXXXXXX)
$CURL http://cartografia.ife.org.mx//descargas/distritacion2017/local/$FILENUM/$FILENUM.zip -o "${states[$i]}".zip
unzip -o -LL -d $TMPDIR "${states[$i]}".zip
mkdir -p "$DATE/shps/local/${states[$i]}"
mv $TMPDIR/* "$DATE/shps/local/${states[$i]}"
rm "${states[$i]}".zip && rm -rf $TMPDIR
done
# You could use the code below to merge all the secciones into one giant
# shapefile.
# file="merge.shp"
# for i in $(find . -name "seccion.shp" )
# do
# if [ -f "$file" ]
# then
# echo "merging……"
# ogr2ogr -f 'ESRI Shapefile' -t_srs "EPSG:4326" -update -append $file $i -nln merge
# else
# echo "creating merge.shp"
# ogr2ogr -f 'ESRI Shapefile' -t_srs "EPSG:4326" $file $i
# fi
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment