Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Last active December 15, 2022 18:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save diegovalle/af936552e0998174313154a2ea9e92a9 to your computer and use it in GitHub Desktop.
Save diegovalle/af936552e0998174313154a2ea9e92a9 to your computer and use it in GitHub Desktop.
Script to download shapefiles from 2020 Mexican census (including demographic data)
# The directory from which the script is running
readonly LOCAL_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
TEMP_DIR="$(mktemp -d)"
# index starts at zero
declare -a states=("national" "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");
download_states() {
mkdir -p "$LOCAL_DIR"/scince_2020
for i in {0..32}
do
echo "Downloading ${states[$i]}..."
# The INEGI uses a leading zero for all one digit numbers
if [ "$i" -lt 10 ]
then
FILENUM="0$i"
else
FILENUM="$i"
fi
curl -sLo "$TEMP_DIR"/scince_$FILENUM.exe \
https://gaia.inegi.org.mx/scince2020desktop/$FILENUM/SCINCE2020_DATOS_$FILENUM.exe
(cd "$TEMP_DIR" && \
"$TEMP_DIR"/innoextract-1.9-linux/innoextract \
--lowercase \
--silent "$TEMP_DIR"/scince_$FILENUM.exe)
find "$TEMP_DIR"/app -depth -type f -regextype posix-extended \
-regex '.*\.(dbf|cpg|prj|shp|rtree|shx)' \
-execdir sh -c 'mv $1 "$2"_$(basename $1)' _ {} "${states[$i]}" \;
DIR=$(find "$TEMP_DIR"/app -mindepth 1 -maxdepth 1 -type d -name '[0-9]*' -print)
mv "$DIR" "$LOCAL_DIR/scince_2020/${states[$i]}"
rm -rf "$TEMP_DIR"/app "$TEMP_DIR"/tmp
done
}
main() {
# Download innoextract 1.9
curl -sLo "$TEMP_DIR"/inno.tar.xz https://constexpr.org/innoextract/files/innoextract-1.9/innoextract-1.9-linux.tar.xz
# Check checksum
echo "008efe5011476ccc4aae17c3e22038b5a1bc5c7aad2b9d4d869537bf3874d21f inno.tar.xz" | sha256sum --check --status
tar -xf "$TEMP_DIR"/inno.tar.xz --directory "$TEMP_DIR"
# Download shapefiles
download_states
rm -rf "$TEMP_DIR"
}
main
# -6 Datos reservados por confidencialidad
# -7 No disponible (cuando toda el área tiene viviendas pendientes)
# -8 No disponible (Cuando toda el área tiene sólo viviendas deshabitadas o de uso temporal)
# -9 No aplica (Cuando no es calculable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment