-
Notifications
You must be signed in to change notification settings - Fork 101
/
polyline.bin.js
executable file
·94 lines (81 loc) · 1.78 KB
/
polyline.bin.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
#!/usr/bin/env node
const meow = require('meow');
const polyline = require('../');
const cli = meow(
`
Provide data from stdin and use with --decode (default), --encode, --toGeoJSON, or --fromGeoJSON. Optionally provide precision.
Usage
$ cat file.json | polyline --fromGeoJSON > file.geojson
Options
--decode -d return an array of lat, lon pairs
--toGeoJSON return GeoJSON from string-encoded polyline
--encode -e return a string-encoded polyline
--fromGeoJSON return a string-encoded polyline from GeoJSON
--precision, -p set a precision.
`,
{
flags: {
decode: {
type: 'boolean',
alias: 'd'
},
toGeoJSON: {
type: 'boolean'
},
encode: {
type: 'boolean',
alias: 'e'
},
fromGeoJSON: {
type: 'boolean'
},
precision: {
type: 'string',
alias: 'p'
}
}
}
);
const {
precision,
decode,
toGeoJSON,
toGeoJson,
encode,
fromGeoJSON,
fromGeoJson
} = cli.flags;
let p;
if (precision) {
p = parseInt(precision, 10);
}
let rawInput = '';
process.stdin.on('readable', function() {
const chunk = process.stdin.read();
if (chunk !== null) {
rawInput += chunk;
}
});
process.stdin.on('end', function() {
const converted = convert(rawInput);
if (!converted) {
exit();
}
process.stdout.write(`${JSON.stringify(converted)}\n`);
});
function convert(rawString) {
if (encode) {
return polyline.encode(rawString, p);
}
if (toGeoJSON || toGeoJson) {
return polyline.toGeoJSON(rawString, p);
}
if (fromGeoJSON || fromGeoJson) {
return polyline.fromGeoJSON(JSON.parse(rawString), p);
}
return polyline.decode(rawString, p);
}
function exit() {
process.stdout.write(cli.showHelp());
process.exit();
}