1
|
#!/bin/bash
|
2
|
|
3
|
# Bash script to create a new postgis database and prep it with GADM
|
4
|
# data for geovalidation purposes.
|
5
|
#
|
6
|
# Basic workflow:
|
7
|
# 1. create polygon geometry upon import (loader does this)
|
8
|
# 2. create simplified polygon geometry after import
|
9
|
# 3. create polygon geography after import
|
10
|
# 4. create indexes
|
11
|
# 5. cluster data in geom index order
|
12
|
#
|
13
|
# todo:
|
14
|
# * better handle Antarctica problem so it can be included instead of
|
15
|
# just dropped? probably not a huge deal for plants...
|
16
|
#
|
17
|
# Jim Regetz
|
18
|
# NCEAS
|
19
|
# Created November 2012
|
20
|
#
|
21
|
# refactored and reorganized by
|
22
|
# Paul Sarando
|
23
|
# iPlant Collaborative
|
24
|
# Updated Oct 2013
|
25
|
|
26
|
DB_NAME="geoscrub"
|
27
|
DB_USER="bien"
|
28
|
DB_HOST_OPT=""
|
29
|
SCRIPT_DIR="$(dirname $0)"
|
30
|
|
31
|
# GADM data originally available at http://www.gadm.org/data2/gadm_v2_shp.zip
|
32
|
# gadm.org now links this file from biogeo.ucdavis.edu.
|
33
|
GADM_DATA_URL="http://biogeo.ucdavis.edu/data/gadm2/gadm_v2_shp.zip"
|
34
|
GADM_DATA_DIR="${SCRIPT_DIR}/gadm_v2_shp"
|
35
|
|
36
|
function usage {
|
37
|
echo "Usage: $0 [OPTIONS]" >&2
|
38
|
echo "Valid Options:" >&2
|
39
|
echo "-d, --dbname=DBNAME database name psql commands will connect to" >&2
|
40
|
echo "-h, --host=HOSTNAME database server host or socket directory" >&2
|
41
|
echo "-U, --username=USERNAME database user name" >&2
|
42
|
exit 1;
|
43
|
}
|
44
|
|
45
|
while [[ $# -gt 0 ]]; do
|
46
|
case "$1" in
|
47
|
-\? | --help)
|
48
|
usage
|
49
|
;;
|
50
|
-h)
|
51
|
if [[ -z $2 ]]; then
|
52
|
echo "Option $1 requires an argument." >&2
|
53
|
usage
|
54
|
fi
|
55
|
DB_HOST_OPT="-h $2"
|
56
|
shift 2
|
57
|
;;
|
58
|
--host=*)
|
59
|
DB_HOST_OPT="-h ${1#*=}"
|
60
|
shift
|
61
|
;;
|
62
|
-U)
|
63
|
if [[ -z $2 ]]; then
|
64
|
echo "Option $1 requires an argument." >&2
|
65
|
usage
|
66
|
fi
|
67
|
DB_USER="$2"
|
68
|
shift 2
|
69
|
;;
|
70
|
--username=*)
|
71
|
DB_USER="${1#*=}"
|
72
|
shift
|
73
|
;;
|
74
|
-d)
|
75
|
if [[ -z $2 ]]; then
|
76
|
echo "Option $1 requires an argument." >&2
|
77
|
usage
|
78
|
fi
|
79
|
DB_NAME="$2"
|
80
|
shift 2
|
81
|
;;
|
82
|
--dbname=*)
|
83
|
DB_NAME="${1#*=}"
|
84
|
shift
|
85
|
;;
|
86
|
*)
|
87
|
echo "Invalid option: $1" >&2
|
88
|
usage
|
89
|
;;
|
90
|
esac
|
91
|
done
|
92
|
|
93
|
#
|
94
|
# assemble input data
|
95
|
#
|
96
|
|
97
|
echo "Updating gadm2 tables from GADM data in ${GADM_DATA_DIR}"
|
98
|
echo -n "Note, to force data to download again from ${GADM_DATA_URL},"
|
99
|
echo " delete the directory ${GADM_DATA_DIR} before running this script."
|
100
|
|
101
|
# Check for GADM data (320MB zip file) unziped in data directory.
|
102
|
if [[ ! -d "$GADM_DATA_DIR" ]]; then
|
103
|
echo "making directory ${GADM_DATA_DIR}"
|
104
|
mkdir -p "$GADM_DATA_DIR"
|
105
|
|
106
|
if [[ $? != 0 ]]; then
|
107
|
echo "Could not create directory ${GADM_DATA_DIR}"
|
108
|
exit 1
|
109
|
fi
|
110
|
|
111
|
pushd "$GADM_DATA_DIR"
|
112
|
wget -O gadm_v2_shp.zip "$GADM_DATA_URL"
|
113
|
unzip gadm_v2_shp.zip
|
114
|
rm -f gadm_v2_shp.zip
|
115
|
popd
|
116
|
fi
|
117
|
|
118
|
#
|
119
|
# create and populate gadm2 table
|
120
|
#
|
121
|
|
122
|
psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" -c "DROP TABLE IF EXISTS gadm2"
|
123
|
if [[ $? != 0 ]]; then
|
124
|
echo "Could not drop GADM2 table in ${DB_NAME} database."
|
125
|
exit 1
|
126
|
fi
|
127
|
|
128
|
echo "Creating gadm2 table with shp2pgsql from ${GADM_DATA_DIR} data."
|
129
|
|
130
|
pushd "$GADM_DATA_DIR"
|
131
|
|
132
|
# load gadm2 data (took 4.7 minutes on willow, 26-Oct-2012)
|
133
|
shp2pgsql -s 4326 -W latin1 gadm2 gadm2 "$DB_NAME" | \
|
134
|
psql --set ON_ERROR_STOP=1 -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
|
135
|
if [[ $? != 0 ]]; then
|
136
|
popd
|
137
|
echo "Could not load GADM2 data into ${DB_NAME} database."
|
138
|
exit 1
|
139
|
fi
|
140
|
|
141
|
popd
|
142
|
|
143
|
# create indexes and additional columns
|
144
|
psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" --set ON_ERROR_STOP=1 < "${SCRIPT_DIR}"/update.gadm2.sql
|
145
|
if [[ $? != 0 ]]; then
|
146
|
echo "Could not update GADM2 data and indexes in ${DB_NAME} database."
|
147
|
exit 1
|
148
|
fi
|
149
|
|
150
|
echo "Update gadm2 tables successfully completed."
|
151
|
|