-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
executable file
·118 lines (104 loc) · 3.88 KB
/
index.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
#!/usr/bin/env node
/* eslint-disable max-len */
const fs = require('fs');
const path = require('path');
const { docopt } = require('docopt');
const commands = require('./src/commands');
const defaults = require('./src/defaults.js');
const packagejson = require('./package.json');
const doc = `
Galton. Lightweight Node.js isochrone server.
Usage:
galton extract [--useBestMatch] <name>
galton build [--profile=<profileName>] <filename>
galton run [options] (--sharedMemory | <filename>)
galton extract build run [--useBestMatch --profile=<profileName>] [options] <name>
galton -h | --help
galton --version
Options:
--cellSize=<cellSize> Distance across each cell [default: ${defaults.cellSize}].
--concavity=<concavity> Concaveman relative measure of concavity [default: ${
defaults.concavity
}].
--deintersect Whether or not to deintersect the final isochrones [default: ${
defaults.deintersect
}].
--intervals=<intervals> Isochrones intervals in minutes [default: ${
defaults.intervals
}].
--lengthThreshold=<lengthThreshold> Concaveman length threshold [default: ${
defaults.lengthThreshold
}].
--port=<port> Port to run on [default: 3000].
--algorithm=<algorithm> Algorithm used by OSRM (CH or MLD) [default: ${
defaults.algorithm
}].
--radius=<radius> Isochrone buffer radius [default: ${defaults.radius}].
--sharedMemory Use shared memory [default: ${defaults.sharedMemory}].
--units=<units> Either 'kilometers' or 'miles' [default: ${defaults.units}].
--useBestMatch Geocoder will use the best match for the query
-p --profile=<profileName> OSRM profile name that will be used for graph building
-h --help Show this screen.
--version Show version.
`;
const args = docopt(doc, { version: packagejson.version });
const main = async () => {
let extractPath;
if (args.extract) {
try {
const name = args['<name>'];
const useBestMatch = args['--useBestMatch'];
extractPath = await commands.extract(name, useBestMatch);
} catch (error) {
process.stderr.write(`${error.message}\n`);
process.exit(-1);
}
}
let graphPath;
if (args.build) {
try {
extractPath = args['<filename>'] || extractPath;
extractPath = path.isAbsolute(extractPath)
? extractPath
: path.join(process.cwd(), extractPath);
fs.accessSync(extractPath, fs.R_OK);
const profileName = args['--profile'];
graphPath = await commands.build(extractPath, profileName);
} catch (error) {
process.stderr.write(`${error.message}\n`);
process.exit(-1);
}
}
if (args.run) {
const sharedMemory = args['--sharedMemory'] || defaults.sharedMemory;
if (!sharedMemory) {
graphPath = args['<filename>'] || graphPath;
graphPath = path.isAbsolute(graphPath) ? graphPath : path.join(process.cwd(), graphPath);
fs.accessSync(graphPath, fs.R_OK);
}
const options = {
osrmPath: graphPath,
algorithm: args['--algorithm'] || defaults.algorithm,
sharedMemory,
port: parseInt(args['--port'], 10),
radius: parseFloat(args['--radius']),
cellSize: parseFloat(args['--cellSize']),
concavity: parseFloat(args['--concavity']),
lengthThreshold: parseFloat(args['--lengthThreshold']) || defaults.lengthThreshold,
deintersect: args['--deintersect'],
intervals: args['--intervals']
.split(',')
.map(parseFloat)
.sort((a, b) => a - b)
};
try {
commands.run(options);
} catch (error) {
process.stderr.write(`\n${error.message}\n`);
process.exit(-1);
}
}
};
process.on('SIGINT', () => process.exit());
process.on('SIGTERM', () => process.exit());
main();