forked from azavea/pfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_jobs.sh
executable file
·87 lines (69 loc) · 2.58 KB
/
import_jobs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
NB_POSTGRESQL_HOST="${NB_POSTGRESQL_HOST:-127.0.0.1}"
NB_POSTGRESQL_DB="${NB_POSTGRESQL_DB:-pfb}"
NB_POSTGRESQL_USER="${NB_POSTGRESQL_USER:-gis}"
NB_POSTGRESQL_PASSWORD="${NB_POSTGRESQL_PASSWORD:-gis}"
set -e
if [[ -n "${PFB_DEBUG}" ]]; then
set -x
fi
function usage() {
echo -n \
"
Usage: $(basename "$0") <state_abbrev>
Import state jobs data into postgres database.
Requires passing the state FIPS abbrev that the neighborhood boundary is found in. e.g. MA for Massachussetts
See: https://www.census.gov/geo/reference/ansi_statetables.html
Optional ENV vars:
NB_POSTGRESQL_HOST - Default: 127.0.0.1
NB_POSTGRESQL_DB - Default: pfb
NB_POSTGRESQL_USER - Default: gis
NB_POSTGRESQL_PASSWORD - Default: gis
"
}
function import_job_data() {
NB_TEMPDIR=`mktemp -d`
NB_STATE_ABBREV="${1}"
NB_DATA_TYPE="${2-main}" # Either 'main' or 'aux'
NB_JOB_FILENAME="${NB_STATE_ABBREV}_od_${NB_DATA_TYPE}_JT00_2014.csv"
wget -P "${NB_TEMPDIR}" "http://lehd.ces.census.gov/data/lodes/LODES7/${NB_STATE_ABBREV}/od/${NB_JOB_FILENAME}.gz"
gunzip -c "${NB_TEMPDIR}/${NB_JOB_FILENAME}.gz" > "${NB_TEMPDIR}/${NB_JOB_FILENAME}"
# Import to postgresql
psql -h "${NB_POSTGRESQL_HOST}" -U "${NB_POSTGRESQL_USER}" -d "${NB_POSTGRESQL_DB}" \
-c "
CREATE TABLE IF NOT EXISTS \"state_od_${NB_DATA_TYPE}_JT00_2014\" (
w_geocode varchar(15),
h_geocode varchar(15),
\"S000\" integer,
\"SA01\" integer,
\"SA02\" integer,
\"SA03\" integer,
\"SE01\" integer,
\"SE02\" integer,
\"SE03\" integer,
\"SI01\" integer,
\"SI02\" integer,
\"SI03\" integer,
createdate VARCHAR(32)
);"
psql -h "${NB_POSTGRESQL_HOST}" -U "${NB_POSTGRESQL_USER}" -d "${NB_POSTGRESQL_DB}" \
-c "TRUNCATE TABLE \"state_od_${NB_DATA_TYPE}_JT00_2014\";"
# Load data
# Dir and files must be world readable/executable for postgres to use copy command
chmod -R 775 "${NB_TEMPDIR}"
psql -h "${NB_POSTGRESQL_HOST}" -U "${NB_POSTGRESQL_USER}" -d "${NB_POSTGRESQL_DB}" \
-c "COPY \"state_od_${NB_DATA_TYPE}_JT00_2014\"(w_geocode, h_geocode, \"S000\", \"SA01\", \"SA02\", \"SA03\", \"SE01\", \"SE02\", \"SE03\", \"SI01\", \"SI02\", \"SI03\", createdate) FROM '${NB_TEMPDIR}/${NB_JOB_FILENAME}' DELIMITER ',' CSV HEADER;"
# Remove NB_TEMPDIR
rm -rf "${NB_TEMPDIR}"
}
if [ "${BASH_SOURCE[0]}" = "${0}" ]
then
if [ "${1:-}" = "--help" ] || [ -z "${1:-}" ]
then
usage
else
NB_STATE_ABBREV="${1}"
import_job_data "${NB_STATE_ABBREV}" "main"
import_job_data "${NB_STATE_ABBREV}" "aux"
fi
fi