Created
November 5, 2020 16:05
-
-
Save nautilytics/a386a3219d8e00e2914109bd9c2bbdee to your computer and use it in GitHub Desktop.
Retrieve all US Census Tract files from Census.gov and upload to a PostGIS database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
####### | |
# Note: | |
# This OS X tested script assumes you have a PostGIS-enabled database named gis_db | |
# and the command line tool shp2pgsql installed. | |
####### | |
# Easiest to begin inside the /Downloads directory | |
cd ~/Downloads | |
# Go through each state FIPs code and get all of their census tract SHP files | |
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 we are on the first pass through then let's ensure we create a new table tl_2019_tracts with a geometry index | |
# otherwise, let's append the new data to the existing table | |
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 | |
# Perform some clean-up on the downloaded files - not necessary | |
cd ~/Downloads | |
rm tl_2019_${f}_tract.zip | |
rm -rf tl_2019_${f}_tract | |
# Increment the counter so we don't recreate the tl_2019_tracts table again | |
counter=$((counter+1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment