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
    echo "Update Options:" >&2
27
    echo "-G, --gadm-only             update only GADM data" >&2
28
    echo "-N, --geonames-only         update only geonames.org data" >&2
29
    echo "-M, --geonames-to-gadm-only update niether GADM nor geonames.org data," >&2
30
    echo "                            but geonames-to-gadm mappings are always updated." >&2
31
    exit 1;
32
}
33

    
34
while [[ $# -gt 0  ]]; do
35
    case "$1" in
36
        -\? | --help)
37
            usage
38
            ;;
39
        -h)
40
            if [[ -z $2  ]];  then
41
                echo "Option $1 requires an argument." >&2
42
                usage
43
            fi
44
            DB_HOST_OPT="-h $2"
45
            shift 2
46
            ;;
47
        --host=*)
48
            DB_HOST_OPT="-h ${1#*=}"
49
            shift
50
            ;;
51
        -U)
52
            if [[ -z $2  ]];  then
53
                echo "Option $1 requires an argument." >&2
54
                usage
55
            fi
56
            DB_USER="$2"
57
            shift 2
58
            ;;
59
        --username=*)
60
            DB_USER="${1#*=}"
61
            shift
62
            ;;
63
        -d)
64
            if [[ -z $2  ]];  then
65
                echo "Option $1 requires an argument." >&2
66
                usage
67
            fi
68
            DB_NAME="$2"
69
            shift 2
70
            ;;
71
        --dbname=*)
72
            DB_NAME="${1#*=}"
73
            shift
74
            ;;
75
        -G | --gadm-only)
76
            NO_GEONAMES=TRUE
77
            shift
78
            ;;
79
        -N | --geonames-only)
80
            NO_GADM=TRUE
81
            shift
82
            ;;
83
        -M | --geonames-to-gadm-only)
84
            NO_GADM=TRUE
85
            NO_GEONAMES=TRUE
86
            shift
87
            ;;
88
        *)
89
            echo "Invalid option: $1" >&2
90
            usage
91
            ;;
92
    esac
93
done
94

    
95
function run_sql_script {
96
    local SCRIPT=$1
97

    
98
    psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" --set ON_ERROR_STOP=1 < "$SCRIPT"
99
    if [[ $? != 0 ]]; then
100
        echo "Error while executing SQL script ${SCRIPT}"
101
        exit 1
102
    fi
103
}
104

    
105
echo "Updating geoscrub validation data."
106

    
107
if [[ -z $NO_GADM ]]; then
108
    "${SCRIPT_DIR}"/update_gadm_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
109
    if [[ $? != 0 ]]; then
110
        echo "Could not update ${DB_NAME} database with GADM data."
111
        exit 1
112
    fi
113
fi
114

    
115
if [[ -z $NO_GEONAMES ]]; then
116
    "${SCRIPT_DIR}"/update_geonames_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME"
117
    if [[ $? != 0 ]]; then
118
        echo "Could not update ${DB_NAME} database with geonames.org data."
119
        exit 1
120
    fi
121
fi
122

    
123
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.country.sql"
124
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.stateprovince.sql"
125
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.county.sql"
126

    
127
echo "Validation data successfully updated."
128

    
(26-26/26)