forked from derhuerst/generate-gtfs-flex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch-trips-txt.js
executable file
·82 lines (69 loc) · 1.87 KB
/
patch-trips-txt.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
patch-trips-txt <path-to-flex-rules> <gtfs-routes> <gtfs-trips>
Examples:
patch-trips-txt flex-rules.js \\
gtfs/{routes,trips,stops,stop_times}.txt >gtfs/trips.patched.txt
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`${pkg.name} v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const {resolve} = require('path')
const assert = require('assert')
const pickupTypes = require('gtfs-utils/pickup-types')
const dropOffTypes = require('gtfs-utils/drop-off-types')
const {Stringifier} = require('csv-stringify')
const createReadGtfsFile = require('./lib/read-gtfs-files')
const addSeconds = require('./lib/add-seconds')
const {computeFlexSpecsByRouteId} = require('./lib/flex-specs-by-trip-id')
const {
generateFlexTripId: flexTripId,
} = require('./lib/ids')
const pathToFlexRules = argv._[0]
if (!pathToFlexRules) showError('Missing path-to-flex-rules.')
const flexRules = require(resolve(process.cwd(), pathToFlexRules))
const requiredGtfsFiles = [
'routes',
'trips',
]
const readGtfsFile = createReadGtfsFile(requiredGtfsFiles, argv._.slice(1))
;(async () => {
const byRouteId = await computeFlexSpecsByRouteId(flexRules, readGtfsFile)
const csv = new Stringifier({
quoted: true,
header: true,
})
csv.pipe(process.stdout)
// pass through all trips, create Flex duplicates
for await (const t of readGtfsFile('trips')) {
csv.write(t)
if (byRouteId.has(t.route_id)) {
// Flex/on-demand case, duplicate trip
const t2 = {
...t,
trip_id: flexTripId(t.trip_id),
}
csv.write(t2)
}
}
csv.end()
})()
.catch(showError)