-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·120 lines (105 loc) · 2.99 KB
/
cli.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
#!/usr/bin/env node
'use strict'
const {parseArgs} = require('util')
const pkg = require('./package.json')
const {
values: flags,
positionals: args,
} = parseArgs({
options: {
'help': {
type: 'boolean',
short: 'h',
},
'version': {
type: 'boolean',
short: 'v',
},
'quiet': {
type: 'boolean',
short: 'q',
},
'pathway-props': {
type: 'string',
short: 'f',
},
'node-props': {
type: 'string',
short: 'F',
},
},
allowPositionals: true,
})
if (flags.help) {
process.stdout.write(`
Usage:
extract-gtfs-pathways <path-to-pathways-file> <path-to-stops-file> <output-directory>
Options:
--quiet -q Don't log the written files.
--pathway-props -f A JS function to determine additional pathway properties.
Example: pw => ({isWalking: pw.pathway_mode === '1'})
Note: The argument will be eval-ed!
--node-props -F A JS function to determine additional node properties.
Example: n => ({isFoo: n.stop_id === 'foo'})
Note: The argument will be eval-ed!
Examples:
mkdir extracted-pathways
extract-gtfs-pathways data/gtfs/pathways.txt data/gtfs/stops.txt extracted-pathways
Notes:
This tool will read a reduced form of stops.txt into memory.
stops.txt needs to be sorted by
1. parent_station: lexically ascending, empty first
2. location_type: numerically descending, empty first
You can use Miller (https://miller.readthedocs.io/) and the
Unix tool sponge to do this:
mlr --csv sort -f parent_station -nr location_type \\
stops.txt | sponge stops.txt
\n`)
process.exit(0)
}
if (flags.version) {
process.stdout.write(`extract-gtfs-pathways v${pkg.version}\n`)
process.exit(0)
}
const {join: pathJoin} = require('path')
const readCsv = require('gtfs-utils/read-csv')
const {writeFile: fsWriteFile} = require('fs/promises')
const extractGtfsPathways = require('.')
const showError = (err) => {
console.error(err)
process.exit(1)
}
const pathToPathwaysFile = args[0]
if (!pathToPathwaysFile) {
showError('Missing path-to-pathways-file parameter.')
}
const pathToStopsFile = args[1]
if (!pathToStopsFile) {
showError('Missing path-to-pathways-file parameter.')
}
const outputDir = args[2]
if (!outputDir) {
showError('Missing output-directory parameter.')
}
const quiet = !!flags.quiet
const pathwayProps = flags['pathway-props']
? eval(flags['pathway-props'])
: () => ({})
const nodeProps = flags['node-props']
? eval(flags['node-props'])
: () => ({})
let file = 0
const writeFile = async (stationId, data) => {
const filename = stationId + '.geo.json'
if (!quiet) process.stderr.write(`${++file} ${filename}: ${data.length}b\n`)
await fsWriteFile(pathJoin(outputDir, filename), data)
}
;(async () => {
const pathwaysSrc = await readCsv(pathToPathwaysFile)
const stopsSrc = await readCsv(pathToStopsFile)
extractGtfsPathways(stopsSrc, pathwaysSrc, writeFile, {
pathwayProps,
nodeProps,
})
})()
.catch(showError)