Skip to content

Instantly share code, notes, and snippets.

@nautilytics
Created September 24, 2020 13:55
Show Gist options
  • Save nautilytics/4625ffd215f85d6237ae16e530eef5f3 to your computer and use it in GitHub Desktop.
Save nautilytics/4625ffd215f85d6237ae16e530eef5f3 to your computer and use it in GitHub Desktop.
An automated approach to retrieving Census Tract shape-files for every state and inserting into a Postgres database
#!/bin/bash
cd ~/Downloads
# Go through each state FIPs code and get their places SHP file
StateFIPS=(
"01" "02" "04" "05" "06" "08" "09" "10"
"11" "12" "13" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "27"
"28" "29" "30" "31" "32" "33" "34" "35"
"36" "37" "38" "39" "40" "41" "42" "44"
"45" "46" "47" "48" "49" "50" "51" "53"
"54" "55" "56"
)
counter=1
for f in ${StateFIPS[@]}
do
curl https://www2.census.gov/geo/tiger/TIGER2019/TRACT/tl_2019_${f}_tract.zip --output tl_2019_${f}_tract.zip
mkdir tl_2019_${f}_tract
unzip tl_2019_${f}_tract.zip -d tl_2019_${f}_tract
if [ $counter == 1 ]; then
shp2pgsql -d -I tl_2019_${f}_tract/tl_2019_${f}_tract.shp tl_2019_tracts | psql -d gis_db
else
shp2pgsql -a tl_2019_${f}_tract/tl_2019_${f}_tract.shp tl_2019_tracts | psql -d gis_db
fi
cd ~/Downloads
rm tl_2019_${f}_tract.zip
rm -rf tl_2019_${f}_tract
counter=$((counter+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment