1
|
#!/bin/bash
|
2
|
|
3
|
# Bash script to download geoscrub_input table dump (created by AMK) from the
|
4
|
# vegbien database, and load it into the geoscrub database (i.e., the
|
5
|
# postgis database prepped with geonames.org data, GADM2 data, and
|
6
|
# associated mapping tables).
|
7
|
#
|
8
|
# Won't be necessary if we end up injecting all of the geoscrubbing and
|
9
|
# geovalidation functionality directly into vegbien itself. And if we
|
10
|
# end up implementing this stuff as a standalone service instead, we'd
|
11
|
# need to rethink (and generalize) how the input data is handled. But
|
12
|
# for now, this should at least serve as a placeholder that could be
|
13
|
# tweaked manually to load any arbitrary geoscrub input data table.
|
14
|
#
|
15
|
# Jim Regetz
|
16
|
# NCEAS
|
17
|
# Created Nov 2012
|
18
|
|
19
|
# Note, to force data to download from DATA_URL, ensure the DATAFILE is deleted
|
20
|
# before running this script.
|
21
|
|
22
|
DB_USER="bien"
|
23
|
DB_HOST="vegbiendev"
|
24
|
SCRIPT_DIR=$(dirname $0)
|
25
|
DATA_URL="http://fs.vegpath.org/exports/geoscrub_input.no_header.cols=country,stateProvince,county,decimalLatitude,decimalLongitude.csv"
|
26
|
DATADIR="${SCRIPT_DIR}/input"
|
27
|
DATAFILE="${DATADIR}/geoscrub-corpus.csv"
|
28
|
|
29
|
if [[ ! -d "$DATADIR" ]]; then
|
30
|
echo "making directory ${DATADIR}"
|
31
|
mkdir -p "$DATADIR"
|
32
|
|
33
|
if [[ $? != 0 ]]; then
|
34
|
echo "Could not create directory ${DATADIR}"
|
35
|
exit 1
|
36
|
fi
|
37
|
fi
|
38
|
|
39
|
if [[ ! -r "$DATAFILE" ]]; then
|
40
|
# download distinct records from vegbien
|
41
|
wget -O "$DATAFILE" "$DATA_URL"
|
42
|
|
43
|
if [[ $? != 0 ]]; then
|
44
|
echo "Could not download input to ${DATAFILE}"
|
45
|
exit 1
|
46
|
fi
|
47
|
fi
|
48
|
|
49
|
echo "Loading vegbien data from ${DATAFILE}"
|
50
|
|
51
|
# clear previous data
|
52
|
psql -e -U "$DB_USER" -h "$DB_HOST" -d geoscrub --set ON_ERROR_STOP=1 < "${SCRIPT_DIR}/truncate.vegbien_geoscrub.sql"
|
53
|
if [[ $? != 0 ]]; then
|
54
|
echo "Could not clear data from vegbien_geoscrub tables."
|
55
|
exit 1
|
56
|
fi
|
57
|
|
58
|
# load
|
59
|
psql -U "$DB_USER" -h "$DB_HOST" -c "\COPY vegbien_geoscrub FROM '${DATAFILE}' WITH CSV" geoscrub
|
60
|
|