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 ""
27
    echo "Update Options:" >&2
28
    echo "-G, --gadm-only             update only GADM data" >&2
29
    echo "-N, --geonames-only         update only geonames.org data" >&2
30
    echo "-M, --geonames-to-gadm-only update niether GADM nor geonames.org data," >&2
31
    echo "                            but geonames-to-gadm mappings are always updated." >&2
32
    echo ""
33
    echo "Input Data Options:" >&2
34
    echo "-g, --gadm-data          GADM data directory (default: ${HOME}/gadm_v2_shp)" >&2
35
    echo "                         Delete this directory, or the GADM data in it," >&2
36
    echo "                         to re-download the data." >&2
37
    echo "-m, --geonames-data      Geonames.org data directory (default: ${HOME}/geonames)" >&2
38
    echo "                         Delete this directory, or the geonames.org data in it," >&2
39
    echo "                         to re-download the data." >&2
40
    exit 1;
41
}
42

    
43
while [[ $# -gt 0  ]]; do
44
    case "$1" in
45
        -\? | --help)
46
            usage
47
            ;;
48
        -h)
49
            if [[ -z $2  ]];  then
50
                echo "Option $1 requires an argument." >&2
51
                usage
52
            fi
53
            DB_HOST_OPT="-h $2"
54
            shift 2
55
            ;;
56
        --host=*)
57
            DB_HOST_OPT="-h ${1#*=}"
58
            shift
59
            ;;
60
        -U)
61
            if [[ -z $2  ]];  then
62
                echo "Option $1 requires an argument." >&2
63
                usage
64
            fi
65
            DB_USER="$2"
66
            shift 2
67
            ;;
68
        --username=*)
69
            DB_USER="${1#*=}"
70
            shift
71
            ;;
72
        -d)
73
            if [[ -z $2  ]];  then
74
                echo "Option $1 requires an argument." >&2
75
                usage
76
            fi
77
            DB_NAME="$2"
78
            shift 2
79
            ;;
80
        --dbname=*)
81
            DB_NAME="${1#*=}"
82
            shift
83
            ;;
84
        -G | --gadm-only)
85
            NO_GEONAMES=TRUE
86
            shift
87
            ;;
88
        -N | --geonames-only)
89
            NO_GADM=TRUE
90
            shift
91
            ;;
92
        -M | --geonames-to-gadm-only)
93
            NO_GADM=TRUE
94
            NO_GEONAMES=TRUE
95
            shift
96
            ;;
97
        -g)
98
            if [[ -z $2  ]];  then
99
                echo "Option $1 requires an argument." >&2
100
                usage
101
            fi
102
            GADM_DATA_DIR_OPT="-g $2"
103
            shift 2
104
            ;;
105
        --gadm-data=*)
106
            GADM_DATA_DIR_OPT="$1"
107
            shift
108
            ;;
109
        -m)
110
            if [[ -z $2  ]];  then
111
                echo "Option $1 requires an argument." >&2
112
                usage
113
            fi
114
            GEONAMES_DATA_DIR_OPT="-m $2"
115
            shift 2
116
            ;;
117
        --geonames-data=*)
118
            GEONAMES_DATA_DIR_OPT="$1"
119
            shift
120
            ;;
121
        *)
122
            echo "Invalid option: $1" >&2
123
            usage
124
            ;;
125
    esac
126
done
127

    
128
function run_sql_script {
129
    local SCRIPT=$1
130

    
131
    psql -e -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" --set ON_ERROR_STOP=1 < "$SCRIPT"
132
    if [[ $? != 0 ]]; then
133
        echo "Error while executing SQL script ${SCRIPT}"
134
        exit 1
135
    fi
136
}
137

    
138
echo "Updating geoscrub validation data."
139

    
140
if [[ -z $NO_GADM ]]; then
141
    "${SCRIPT_DIR}"/update_gadm_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" $GADM_DATA_DIR_OPT
142
    if [[ $? != 0 ]]; then
143
        echo "Could not update ${DB_NAME} database with GADM data."
144
        exit 1
145
    fi
146
fi
147

    
148
if [[ -z $NO_GEONAMES ]]; then
149
    "${SCRIPT_DIR}"/update_geonames_data.sh -U "$DB_USER" $DB_HOST_OPT -d "$DB_NAME" $GEONAMES_DATA_DIR_OPT
150
    if [[ $? != 0 ]]; then
151
        echo "Could not update ${DB_NAME} database with geonames.org data."
152
        exit 1
153
    fi
154
fi
155

    
156
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.country.sql"
157
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.stateprovince.sql"
158
run_sql_script "${SCRIPT_DIR}/geonames-to-gadm.county.sql"
159

    
160
echo "Validation data successfully updated."
161

    
(27-27/27)