-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
144 lines (125 loc) · 3.13 KB
/
convert.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
'use strict'
const readCsv = require('gtfs-utils/read-csv')
const fs = require('fs')
const {writeFile} = require('fs/promises')
const path = require('path')
const {pipeline} = require('stream')
const equal = require('shallow-equals')
const ndjson = require('ndjson')
const {gtfsToFptf} = require('gtfs-utils/route-types')
const readGtfsFile = file => readCsv(path.join(__dirname, file + '.csv'))
const writeNDJSON = (data, file) => new Promise((resolve, reject) => {
const toJSON = ndjson.stringify()
pipeline(
toJSON,
fs.createWriteStream(path.join(__dirname, file)),
(err) => {
if (err) reject(err)
else resolve()
}
)
for (let key in data) toJSON.write(data[key])
toJSON.end()
})
const writeJSON = async (data, file) => {
data = Object.values(data)
await writeFile(path.join(__dirname, file), JSON.stringify(data))
}
// see https://developers.google.com/transit/gtfs/reference/routes-file
const isAmbiguous = {
'0': true, // `0` can be suburban or tram
'2': true // `2` can be regional or express train
}
// see https://developers.google.com/transit/gtfs/reference/routes-file
const products = {
'0': 'suburban',
'1': 'subway',
'2': 'regional',
'3': 'bus',
'4': 'ferry',
'100': 'regional',
'102': 'regional',
'109': 'suburban',
'400': 'subway',
'700': 'bus',
'900': 'tram',
'1000': 'ferry'
}
const fetchLines = async () => {
const lines = Object.create(null)
for await (const line of await readGtfsFile('routes')) {
const id = line.route_id
const type = line.route_type
const name = line.route_short_name || line.route_long_name
if (isAmbiguous[type]) {
console.error(`route_type ${type} of line ${name} (${id}) is ambiguous.`)
}
lines[id + ''] = {
type: 'line',
id,
name,
operator: line.agency_id,
mode: gtfsToFptf(+type),
product: products[line.route_type] || null,
variants: []
}
}
return lines
}
const fetchTrips = async () => {
const trips = Object.create(null)
for await (const trip of await readGtfsFile('trips')) {
const id = trip.trip_id
const lineId = trip.route_id
trips[id + ''] = {id, lineId, stops: []}
}
return trips
}
const addStopoversToTrips = async (trips) => {
for await (const arrival of await readGtfsFile('stop_times')) {
const tripId = arrival.trip_id
if (!trips[tripId]) {
console.error('Unknown trip', tripId)
continue
}
const i = parseInt(arrival.stop_sequence)
trips[tripId].stops[i] = arrival.stop_id
}
return trips
}
const computeLineVariants = (lines, trips) => {
for (let tripId in trips) {
const trip = trips[tripId]
const line = lines[trip.lineId]
if (!line) {
console.error('Unknown line', trip.lineId)
continue
}
const variant = line.variants.find(v => equal(v.stops, trip.stops))
if (variant) variant.trips++
else {
line.variants.push({
stops: trip.stops,
trips: 1
})
}
}
}
;(async () => {
const [
lines, trips,
] = await Promise.all([
fetchLines(),
fetchTrips(),
])
await addStopoversToTrips(trips)
computeLineVariants(lines, trips)
await Promise.all([
writeNDJSON(lines, 'data.ndjson'),
writeJSON(lines, 'data.json'),
])
})()
.catch((err) => {
console.error(err)
process.exit(1)
})