-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute-trajectories.js
executable file
·93 lines (83 loc) · 2.34 KB
/
compute-trajectories.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
#!/usr/bin/env node
'use strict'
const {join: pathJoin} = require('path')
const readCsv = require('gtfs-utils/read-csv')
const readServices = require('gtfs-utils/read-services-and-exceptions')
const computeTrajectories = require('gtfs-utils/compute-trajectories')
const resolveTime = require('gtfs-utils/lib/resolve-time')
const {writeFile} = require('fs/promises')
const routesToBeMatched = require('./lib/routes-to-be-matched')
const pkg = require('./package.json')
const TIMEZONE = process.env.TIMEZONE
if (!TIMEZONE) {
console.error('Missing/empty TIMEZONE environment variable.')
process.exit(1)
}
const src = process.env.GTFS_DIR || 'gtfs'
const readFile = async (name) => {
return await readCsv(pathJoin(src, name + '.txt'))
}
const destDir = process.env.TRAJECTORIES_DIR
if (!destDir) {
console.error('Missing/empty TRAJECTORIES_DIR environment variable.')
process.exit(1)
}
const filters = {
trip: t => routesToBeMatched.includes(t.route_id),
}
const withAbsoluteTime = (tr, date) => {
try {
const withAbsTime = ([lon, lat, alt, arr, dep]) => [
lon, lat, alt,
resolveTime(TIMEZONE, date, arr), // arr
resolveTime(TIMEZONE, date, dep), // dep
]
return {
type: 'Feature',
properties: {
...tr.properties,
id: tr.properties.tripId + '-' + date,
date,
},
geometry: {
type: 'LineString',
coordinates: tr.geometry.coordinates.map(withAbsTime)
},
}
} catch (err) {
err.trajectory = tr
err.date = date
throw err
}
}
;(async () => {
const svcDates = new Map()
for await (const [serviceId, dates] of readServices(readFile, TIMEZONE)) {
svcDates.set(serviceId, dates)
}
const trajectories = await computeTrajectories(readFile, filters)
for await (const tr of trajectories) {
const {id, tripId, serviceId} = tr.properties
if (!svcDates.has(serviceId)) {
console.error('invalid service_id', tr.properties)
continue
}
const dates = svcDates.get(serviceId)
if (dates.length === 0) {
console.error('0 service dates', serviceId, dates)
continue
}
console.error('processing', id, 'serviceId', serviceId, 'with', dates.length, 'service dates')
for (const date of dates) {
const absTr = withAbsoluteTime(tr, date)
await writeFile(
pathJoin(destDir, absTr.properties.id + '.json'),
JSON.stringify(absTr),
)
}
}
})()
.catch((err) => {
console.error(err)
process.exit(1)
})