forked from stadtnavi/generate-gtfs-flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-locations-geojson.js
executable file
·103 lines (90 loc) · 2.42 KB
/
gen-locations-geojson.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
#!/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:
generate-locations-geojson <path-to-flex-rules> <gtfs-routes> <gtfs-trips> <gtfs-stops> <gtfs-stop-times>
Examples:
generate-locations-geojson flex-rules.js \\
gtfs/{routes,trips,stops,stop_times}.txt >gtfs/locations.geojson
\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 circle = require('@turf/circle').default
const truncate = require('@turf/truncate').default
const createReadGtfsFile = require('./lib/read-gtfs-files')
const {computeFlexSpecsWithStopsByTripId} = require('./lib/flex-specs-by-trip-id')
const {generateFlexLocationId: flexLocId} = 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',
'stops',
'stop_times',
]
const readGtfsFile = createReadGtfsFile(requiredGtfsFiles, argv._.slice(1))
;(async () => {
const byTripId = await computeFlexSpecsWithStopsByTripId(flexRules, readGtfsFile)
process.stdout.write(`\
{
"type": "FeatureCollection",
"features": [
`)
let first = true
const printLoc = (loc) => {
if (first) first = false
else process.stdout.write(',')
process.stdout.write(JSON.stringify(loc) + '\n')
}
const printedLocs = new Set()
for (const [trip_id, spec] of byTripId) {
const {
id: specId,
radius,
stops,
} = spec
for (const s of stops) {
const locId = flexLocId(specId, s.stop_id)
if (printedLocs.has(locId)) continue
const properties = {
// We duplicate the name here because some downstream tools need it.
name: s.stop_name || null,
stop_name: s.stop_name || null,
stop_desc: s.stop_desc || null,
// todo: stop_url?
}
printLoc({
id: locId, // todo: why not insde `properties`? double-check spec again
...truncate(
circle([s.stop_lon, s.stop_lat], radius, {steps: 32, properties}),
{precision: 5, mutate: true},
),
})
printedLocs.add(locId)
}
}
process.stdout.write(`\
]
}
`)
})()
.catch(showError)