Project

General

Profile

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. Load gadm2 data into database
8
#  2. Load geonames data into database
9
#  3. Update geonames to gadm2 data tables
10
# 
11
# Paul Sarando
12
# iPlant Collaborative
13
# Oct 2013
14

    
15
DB_NAME="geoscrub"
16
DB_USER="bien"
17
DB_HOST_OPT=""
18
SCRIPT_DIR="$(dirname $0)"
19

    
20
function usage {
21
    echo "Usage: $0 [OPTIONS]" >&2
22
    echo "Valid Options:" >&2
23
    echo "-d, --dbname=DBNAME      database name psql commands will connect to" >&2
24
    echo "-h, --host=HOSTNAME      database server host or socket directory" >&2
25
    echo "-U, --username=USERNAME  database user name" >&2
26
    exit 1;
27
}
28

    
29
while [[ $# -gt 0  ]]; do
30
    case "$1" in
31
        -\? | --help)
32
            usage
33
            ;;
34
        -h)
35
            if [[ -z $2  ]];  then
36
                echo "Option $1 requires an argument." >&2
37
                usage
38
            fi
39
            DB_HOST_OPT="-h $2"
40
            shift 2
41
            ;;
42
        --host=*)
43
            DB_HOST_OPT="-h ${1#*=}"
44
            shift
45
            ;;
46
        -U)
47
            if [[ -z $2  ]];  then
48
                echo "Option $1 requires an argument." >&2
49
                usage
50
            fi
51
            DB_USER="$2"
52
            shift 2
53
            ;;
54
        --username=*)
55
            DB_USER="${1#*=}"
56
            shift
57
            ;;
58
        -d)
59
            if [[ -z $2  ]];  then
60
                echo "Option $1 requires an argument." >&2
61
                usage
62
            fi
63
            DB_NAME="$2"
64
            shift 2
65
            ;;
66
        --dbname=*)
67
            DB_NAME="${1#*=}"
68
            shift
69
            ;;
70
        *)
71
            echo "Invalid option: $1" >&2
72
            usage
73
            ;;
74
    esac
75
done
76

    
77
function run_sql_script {
78
    local SCRIPT=$1
79

    
80
    psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" --set ON_ERROR_STOP=1 < "$SCRIPT"
81
    if [[ $? != 0 ]]; then
82
        echo "Error while executing SQL script ${SCRIPT}"
83
        exit 1
84
    fi
85
}
86

    
87
echo "Updating geoscrub validation data."
88

    
89
"${SCRIPT_DIR}"/update_gadm_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
90
if [[ $? != 0 ]]; then
91
    echo "Could not update ${DB_NAME} database with GADM data."
92
    exit 1
93
fi
94

    
95
"${SCRIPT_DIR}"/update_geonames_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
96
if [[ $? != 0 ]]; then
97
    echo "Could not update ${DB_NAME} database with geonames.org data."
98
    exit 1
99
fi
100

    
101
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.country.sql"
102
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.stateprovince.sql"
103
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.county.sql"
104

    
105
echo "Validation data successfully updated."
106

    
(26-26/26)