forked from spencerrecneps/pfb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport_connectivity.sh
executable file
·91 lines (70 loc) · 2.56 KB
/
export_connectivity.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
88
89
90
91
#!/usr/bin/env bash
# ec prefix stands for 'export connectivity'
set -e
cd $(dirname "$0")
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}"
NB_POSTGRESQL_PORT="${NB_POSTGRESQL_PORT:-5432}"
NB_OUTPUT_FILE_PREFIX="${NB_OUTPUT_FILE_PREFIX:-analysis_}"
NB_OUTPUT_DIR="${1}"
function ec_usage() {
echo -n \
"
Usage: $(basename "$0") <output_directory>
Export data from a successful run of the PeopleForBike Network Analysis to a directory on disk.
<output_directory> must be an absolute path (pgsql COPY doesn't support relative paths)
This script exports the following tables:
- neighborhood_ways as SHP
- neighborhood_ways as GeoJSON (TODO)
- neighborhood_connected_census_blocks as SHP
- neighborhood_overall_scores as CSV
Optional ENV vars:
NB_OUTPUT_FILE_PREFIX - Default: 'analysis_'
NB_POSTGRESQL_HOST - Default: 127.0.0.1
NB_POSTGRESQL_DB - Default: pfb
NB_POSTGRESQL_USER - Default: gis
NB_POSTGRESQL_PASSWORD - Default: gis
NB_POSTGRESQL_PORT - Default: 4326
"
}
function ec_export_table_shp() {
OUTPUT_DIR="$1"
EXPORT_TABLENAME="$2"
FILENAME="${OUTPUT_DIR}/${NB_OUTPUT_FILE_PREFIX}${EXPORT_TABLENAME}.shp"
pgsql2shp -h "${NB_POSTGRESQL_HOST}" \
-u "${NB_POSTGRESQL_USER}" \
-p "${NB_POSTGRESQL_PORT}" \
-P "${NB_POSTGRESQL_PASSWORD}" \
-f "${FILENAME}" \
"${NB_POSTGRESQL_DB}" \
"${EXPORT_TABLENAME}"
}
function ec_export_table_csv() {
OUTPUT_DIR="$1"
EXPORT_TABLENAME="$2"
FILENAME="${OUTPUT_DIR}/${NB_OUTPUT_FILE_PREFIX}${EXPORT_TABLENAME}.csv"
psql -h "${NB_POSTGRESQL_HOST}" \
-U "${NB_POSTGRESQL_USER}" \
-p "${NB_POSTGRESQL_PORT}" \
-d "${NB_POSTGRESQL_DB}" \
-c "COPY ${EXPORT_TABLENAME} TO '${FILENAME}' WITH (FORMAT CSV, HEADER)"
}
if [ "${BASH_SOURCE[0]}" = "${0}" ]
then
if [ "${1:-}" = "--help" ] || [ -z "${1:-}" ]
then
ec_usage
else
NB_OUTPUT_DIR="${1}"
# Export neighborhood_ways as SHP
ec_export_table_shp "${NB_OUTPUT_DIR}" "neighborhood_ways"
# Export neighborhood ways as GeoJSON
# TODO: Add ogr2ogr and try again
# Export neighborhood_connected_census_blocks as SHP
ec_export_table_shp "${NB_OUTPUT_DIR}" "neighborhood_connected_census_blocks"
# Export neighborhood_overall_scores as CSV
ec_export_table_csv "${NB_OUTPUT_DIR}" "neighborhood_overall_scores"
fi
fi