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="localhost"
|
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
|
#
|
37
|
# assemble input data
|
38
|
#
|
39
|
|
40
|
echo "Updating gadm2 tables from GADM data in ${GADM_DATA_DIR}"
|
41
|
echo -n "Note, to force data to download again from ${GADM_DATA_URL},"
|
42
|
echo " delete the directory ${GADM_DATA_DIR} before running this script."
|
43
|
|
44
|
# Check for GADM data (320MB zip file) unziped in data directory.
|
45
|
if [[ ! -d "$GADM_DATA_DIR" ]]; then
|
46
|
echo "making directory ${GADM_DATA_DIR}"
|
47
|
mkdir -p "$GADM_DATA_DIR"
|
48
|
|
49
|
if [[ $? != 0 ]]; then
|
50
|
echo "Could not create directory ${GADM_DATA_DIR}"
|
51
|
exit 1
|
52
|
fi
|
53
|
|
54
|
pushd "$GADM_DATA_DIR"
|
55
|
wget -O gadm_v2_shp.zip "$GADM_DATA_URL"
|
56
|
unzip gadm_v2_shp.zip
|
57
|
rm -f gadm_v2_shp.zip
|
58
|
popd
|
59
|
fi
|
60
|
|
61
|
#
|
62
|
# create and populate gadm2 table
|
63
|
#
|
64
|
|
65
|
psql -e -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME" -c "DROP TABLE IF EXISTS gadm2"
|
66
|
if [[ $? != 0 ]]; then
|
67
|
echo "Could not drop GADM2 table in ${DB_NAME} database."
|
68
|
exit 1
|
69
|
fi
|
70
|
|
71
|
echo "Creating gadm2 table with shp2pgsql from ${GADM_DATA_DIR} data."
|
72
|
|
73
|
pushd "$GADM_DATA_DIR"
|
74
|
|
75
|
# load gadm2 data (took 4.7 minutes on willow, 26-Oct-2012)
|
76
|
shp2pgsql -s 4326 -W latin1 gadm2 gadm2 "$DB_NAME" | \
|
77
|
psql --set ON_ERROR_STOP=1 -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME"
|
78
|
if [[ $? != 0 ]]; then
|
79
|
popd
|
80
|
echo "Could not load GADM2 data into ${DB_NAME} database."
|
81
|
exit 1
|
82
|
fi
|
83
|
|
84
|
popd
|
85
|
|
86
|
# create indexes and additional columns
|
87
|
psql -e -U "$DB_USER" -h "$DB_HOST" -d "$DB_NAME" --set ON_ERROR_STOP=1 < "${SCRIPT_DIR}"/update.gadm2.sql
|
88
|
if [[ $? != 0 ]]; then
|
89
|
echo "Could not update GADM2 data and indexes in ${DB_NAME} database."
|
90
|
exit 1
|
91
|
fi
|
92
|
|
93
|
echo "Update gadm2 tables successfully completed."
|
94
|
|