-
Notifications
You must be signed in to change notification settings - Fork 34
/
13_import_blocks_2020.sh
executable file
·56 lines (43 loc) · 1.62 KB
/
13_import_blocks_2020.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
cd /home/ubuntu/data/blocks2020
# For shp2pgsql
sudo apt-get install -y postgis
if [ -z $PGHOST ]; then
echo "You must set PGHOST environment variable to the hostname of the PostgreSQL server to operate on."
exit 1
fi
echo "Assuming schema 'blocks' exists. Not dropping/recreating because multiple scripts access it."
for i in /home/ubuntu/data/blocks2020/TABBLOCK20 # not a list but easier to keep it like other tiger importers
do
tablename=$(basename $i)
for j in $i/*.zip
do
unzip -q -n $j -d $i
done
# Pick one of the shapefiles to build schema with
one_shapefile=`ls -a $i/*.shp | head -n 1`
# Start by preparing the table
shp2pgsql -W "latin1" -s 4326 -p -I $one_shapefile blocks.$tablename | psql -v ON_ERROR_STOP=1 -q
if [ $? -ne 0 ]
then
echo "Couldn't prep table from ${one_shapefile}"
exit 1
fi
echo "Created table blocks.$tablename"
# Then append all the geometries
for j in $i/*.shp
do
echo "importing $j to blocks.$tablename"
shp2pgsql -W "latin1" -s 4326 -a $j blocks.$tablename | psql -v ON_ERROR_STOP=1 -q
if [ $? -ne 0 ]
then
echo "Couldn't import ${j}"
exit 1
fi
done
# Make sure the geometries in that table we just imported are valid
psql -v ON_ERROR_STOP=1 -q -c "UPDATE blocks.${tablename} SET geom=ST_MakeValid(geom) WHERE NOT ST_IsValid(geom);"
# Delete the unzipped stuff to save room
find $i -type f ! -name '*.zip' -delete
done
psql -v ON_ERROR_STOP=1 -q -c "CREATE UNIQUE INDEX tabblock_geoid20_idx ON blocks.tabblock20 (geoid20);"