-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport.sh
More file actions
executable file
·150 lines (129 loc) · 4.06 KB
/
import.sh
File metadata and controls
executable file
·150 lines (129 loc) · 4.06 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
set -e
set -u
set -E # abort if subshells fail
set -o pipefail
source "$(dirname "$(realpath "$0")")/lib.sh"
gtfs_path=''
postprocessing_d_path="${GTFS_POSTPROCESSING_D_PATH:?missing/empty}"
verbose="${GTFS_IMPORTER_VERBOSE:-true}"
if [ "$verbose" != false ]; then
set -x # enable xtrace
fi
print_bold "Extracting the GTFS feed."
rm -rf "$extracted_path"
unzip_args=()
if [ "$verbose" = false ]; then
unzip_args+=('-q')
fi
unzip "${unzip_args[@]}" \
-d "$extracted_path" \
"$zip_path"
gtfs_path="$extracted_path"
if [[ -f '/etc/gtfs/preprocess.sh' ]]; then
print_bold "Preprocessing GTFS feed using preprocess.sh."
/etc/gtfs/preprocess.sh "$gtfs_path"
fi
# Note: We're using gtfsclean, a fork of gtfstidy, but keep the flags backwards-compatible.
if [ "${GTFSTIDY_BEFORE_IMPORT:-true}" != false ]; then
print_bold "Tidying GTFS feed using gtfsclean."
# Remove any leftovers from previous runs (e.g. pathways.txt/levels.txt)
rm -rf "$tidied_path"
set +x
gtfstidy_args=()
if [ "$verbose" != false ]; then
gtfstidy_args+=('--show-warnings')
fi
# fixing
if [ "${GTFSTIDY_FIX_ZIP:-true}" != false ]; then
gtfstidy_args+=('--fix-zip') # -z
fi
if [ "${GTFSTIDY_DEFAULT_ON_ERRS:-true}" != false ]; then
gtfstidy_args+=('--default-on-errs') # -e
fi
if [ "${GTFSTIDY_DROP_ERRS:-true}" != false ]; then
gtfstidy_args+=('--drop-errs') # -D
fi
if [ "${GTFSTIDY_CHECK_NULL_COORDS:-true}" != false ]; then
gtfstidy_args+=('--check-null-coords') # -n
fi
# minimization
# todo: default to true once https://github.com/public-transport/gtfsclean/issues/12 is fixed
if [ "${GTFSTIDY_KEEP_ADDITIONAL_FIELDS:-false}" == true ]; then
gtfstidy_args+=('--keep-additional-fields') # -F
fi
if [ "${GTFSTIDY_KEEP_IDS:-true}" != false ]; then
gtfstidy_args+=('--keep-ids')
fi
if [ "${GTFSTIDY_MIN_SHAPES:-true}" != false ]; then
gtfstidy_args+=('--min-shapes') # -s
fi
if [ "${GTFSTIDY_MINIMIZE_SERVICES:-true}" != false ]; then
gtfstidy_args+=('--minimize-services') # -c
fi
if [ "${GTFSTIDY_MINIMIZE_STOPTIMES:-true}" != false ]; then
gtfstidy_args+=('--minimize-stoptimes') # -T
fi
if [ "${GTFSTIDY_DELETE_ORPHANS:-true}" != false ]; then
gtfstidy_args+=('--delete-orphans') # -O
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_AGENCIES:-true}" != false ]; then
gtfstidy_args+=('--remove-red-agencies') # -A
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_ROUTES:-true}" != false ]; then
gtfstidy_args+=('--remove-red-routes') # -R
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_SERVICES:-true}" != false ]; then
gtfstidy_args+=('--remove-red-services') # -C
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_SHAPES:-true}" != false ]; then
gtfstidy_args+=('--remove-red-shapes') # -S
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_STOPS:-true}" != false ]; then
gtfstidy_args+=('--remove-red-stops') # -P
fi
if [ "${GTFSTIDY_REMOVE_REDUNDANT_TRIPS:-true}" != false ]; then
gtfstidy_args+=('--remove-red-trips') # -I
fi
# todo: allow configuring additional flags
set -x
gtfsclean \
"${gtfstidy_args[@]}" \
-o "$tidied_path" \
"$gtfs_path" \
2>&1 | tee "$gtfs_tmp_dir/tidied.gtfs.gtfstidy-log.txt"
gtfs_path="$tidied_path"
fi
print_bold "Importing GTFS feed into the $PGDATABASE database."
gtfs-to-sql --version
psql_args=()
gtfs_to_sql_args=()
if [ "$verbose" = false ]; then
psql_args+=('--quiet')
gtfs_to_sql_args+=('--silent')
fi
gtfs-to-sql -d "${gtfs_to_sql_args[@]}" \
--trips-without-shape-id --lower-case-lang-codes \
--stops-location-index \
--import-metadata \
--schema "${GTFS_IMPORTER_SCHEMA:-public}" \
--postgrest \
"$gtfs_path/"*.txt \
| zstd | sponge | zstd -d \
| psql -b -v 'ON_ERROR_STOP=1' "${psql_args[@]}"
if [ -d "$postprocessing_d_path" ]; then
print_bold "Running custom post-processing scripts in $postprocessing_d_path."
shopt -s nullglob
# todo: DRY this with the hash calculation in import.js
for file in "$postprocessing_d_path/"*; do
ext="${file##*.}"
if [ "$ext" = "sql" ]; then
psql -b -1 -v 'ON_ERROR_STOP=1' --set=SHELL="$SHELL" "${psql_args[@]}" \
-f "$file"
else
"$file" "$gtfs_path"
fi
done
shopt -u nullglob
fi
print_bold 'Done!'