-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (209 loc) · 6.17 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict'
const {
STOP,
STATION,
ENTRANCE_EXIT,
GENERIC_NODE,
BOARDING_AREA,
} = require('gtfs-utils/lib/location-types')
const nodeColors = {
// stops/stations/boarding areas orange/red, entrances/exits green, others gray
[STOP]: '#d35400', // pumpkin
[STATION]: '#c0392b', // pomegranate
[ENTRANCE_EXIT]: '#16a085', // green sea
[GENERIC_NODE]: '#7f8c8d', // asbestos
[BOARDING_AREA]: '#f39c12', // orange
}
const nodeRadiuses = {
[STOP]: 5,
[STATION]: 7,
[ENTRANCE_EXIT]: 5,
[GENERIC_NODE]: 4,
[BOARDING_AREA]: 5,
}
// todo: pull these from gtfs-utils
// possible pathway_mode values:
// https://gtfs.org/reference/static/#pathwaystxt
// - `1`: Walkway.
const WALKWAY = '1'
// - `2`: Stairs.
const STAIRS = '2'
// - `3`: Moving sidewalk/travelator.
const MOVING_SIDEWALK_TRAVELATOR = '3'
// - `4`: Escalator.
const ESCALATOR = '4'
// - `5`: Elevator.
const ELEVATOR = '5'
// - `6`: Fare gate (or payment gate): A pathway that crosses into an area of the station where proof of payment is required to cross. Fare gates may separate paid areas of the station from unpaid ones, or separate different payment areas within the same station from each other. This information can be used to avoid routing passengers through stations using shortcuts that would require passengers to make unnecessary payments, like directing a passenger to walk through a subway platform to reach a busway.
const FARE_GATE = '6'
// - `7`: Exit gate: A pathway exiting a paid area into an unpaid area where proof of payment is not required to cross.
const EXIT_GATE = '7'
const pwColors = {
[WALKWAY]: '#2c3e50', // midnight blue
[STAIRS]: '#34495e', // wet asphalt
[MOVING_SIDEWALK_TRAVELATOR]: '#8e44ad', // wisteria
[ESCALATOR]: '#9b59b6', // amethyst
[ELEVATOR]: '#2980b9', // belize hole
[FARE_GATE]: '#27ae60', // nephritis
[EXIT_GATE]: '#2ecc71', // emerald
}
const pwWidths = {
[WALKWAY]: .7,
[STAIRS]: 1,
[MOVING_SIDEWALK_TRAVELATOR]: 2,
[ESCALATOR]: 2,
[ELEVATOR]: 2,
[FARE_GATE]: 2,
[EXIT_GATE]: 2,
}
const extractGtfsPathways = async (stopsSrc, pathwaysSrc, writeFile, opt = {}) => {
const {
pathwayProps,
nodeProps,
logErrorMsg,
} = {
logErrorMsg: console.error,
...opt,
}
const nodes = Object.create(null) // nodes, by stop_id
const stations = Object.create(null) // "top-most" parent_station, by stop_id
for await (const s of stopsSrc) {
const props = {}
if (s.location_type in nodeColors) {
props['circle-color'] = nodeColors[s.location_type]
}
if (s.location_type in nodeRadiuses) {
props['circle-radius'] = nodeRadiuses[s.location_type]
}
nodes[s.stop_id] = [
parseFloat(s.stop_lon),
parseFloat(s.stop_lat),
s.stop_name || null,
s.location_type,
s.level_id,
{
...props,
...nodeProps(s),
},
]
// todo: DRY with gtfs-utils/lib/read-stop-stations
// stops.txt is sorted so that we get stations first.
stations[s.stop_id] = s.parent_station
? stations[s.parent_station] || s.parent_station
: s.stop_id
}
// todo: add a --sorted mode that doesn't have to group in-memory
const pws = Object.create(null) // pathways, grouped by station ID
for await (const pw of pathwaysSrc) {
const props = {}
if (pw.pathway_mode in pwColors) {
props['line-color'] = pwColors[pw.pathway_mode]
}
if (pw.pathway_mode in pwWidths) {
props['line-width'] = pwWidths[pw.pathway_mode]
}
const encodedPw = [
pw.pathway_id || undefined,
pw.from_stop_id,
pw.to_stop_id,
pw.pathway_mode ? parseInt(pw.pathway_mode) : undefined,
pw.is_bidirectional === '1',
pw.length ? parseFloat(pw.length) : undefined,
pw.traversal_time ? parseInt(pw.traversal_time) : undefined,
pw.stair_count ? parseInt(pw.stair_count) : undefined,
pw.max_slope ? parseFloat(pw.max_slope) : undefined,
pw.min_width ? parseFloat(pw.min_width) : undefined,
pw.signposted_as || undefined,
pw.reversed_signposted_as || undefined,
{
...props,
...pathwayProps(pw),
},
]
const fromStationId = stations[pw.from_stop_id]
if (pws[fromStationId]) pws[fromStationId].push(encodedPw)
else pws[fromStationId] = [encodedPw]
const toStationId = stations[pw.to_stop_id]
if (pws[toStationId]) pws[toStationId].push(encodedPw)
else pws[toStationId] = [encodedPw]
}
for (const stationId in pws) {
let data = '{"type": "FeatureCollection", "features": ['
let first = true
const addFeature = (feature) => {
data += (first ? '' : ',') + JSON.stringify(feature) + '\n'
first = false
}
const nodesAdded = new Set()
const addNode = (nodeId, pathwayId) => {
if (nodesAdded.has(nodeId)) return;
if (!(nodeId in nodes)) {
logErrorMsg(`node ${nodeId} does not exist, used in pathway ${pathwayId}`)
return;
}
nodesAdded.add(nodeId)
const n = nodes[nodeId]
// todo: n might be undefined!
addFeature({
type: 'Feature',
properties: {
stop_id: nodeId,
stop_name: n[2],
location_type: n[3],
level_id: n[4],
...n[5], // additional props
},
geometry: {
type: 'Point',
coordinates: [n[0], n[1]],
},
})
}
for (const pw of pws[stationId]) {
const [
pathway_id,
from_stop_id, to_stop_id,
] = pw
addNode(from_stop_id, pathway_id)
addNode(to_stop_id, pathway_id)
const fromNode = nodes[from_stop_id]
if (!fromNode) {
logErrorMsg(`invalid from_stop_id "${from_stop_id}" in pathway ${pw[0]}`)
continue
}
const toNode = nodes[to_stop_id]
if (!toNode) {
logErrorMsg(`invalid to_stop_id "${to_stop_id}" in pathway ${pw[0]}`)
continue
}
// todo: some pathways have a length of 0, e.g. elevators -> show them as a point?
addFeature({
type: 'Feature',
properties: {
pathway_id,
pathway_mode: pw[3],
is_bidirectional: pw[4],
length: pw[5],
traversal_time: pw[6],
stair_count: pw[7],
max_slope: pw[8],
min_width: pw[9],
signposted_as: pw[10],
reversed_signposted_as: pw[11],
...pw[12], // additional props
},
geometry: {
type: 'LineString',
coordinates: [
[fromNode[0], fromNode[1]],
[toNode[0], toNode[1]],
],
},
})
}
data += ']}'
// todo: write concurrently
await writeFile(stationId, data)
}
}
module.exports = extractGtfsPathways