forked from mapbox/csv2geojson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv2geojson
executable file
·49 lines (42 loc) · 1.49 KB
/
csv2geojson
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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const fs = require('fs');
const concat = require('concat-stream');
const csv2geojson = require('./');
const cli = meow(
`
Usage: ./csv2geojson --lat [string] --lon [string] --delimiter [string] FILE
Options:
--lat the name of the latitude column
--lon the name of the longitude column
--line whether or not to output points as a LineString [default: false]
--delimiter the type of delimiter [default: ","]
--numeric-fields comma separated list of fields to convert to numbers
`,
{
flags: {
lat: {type: 'string'},
lon: {type: 'string'},
line: {type: 'boolean', default: false},
delimiter: {type: 'string', default: ','},
numericFields: {type: 'string'},
},
}
);
const file = cli.input[0];
const {lat, lon, delimiter, numericFields, line} = cli.flags;
if (process.stdin.isTTY && !file) return cli.showHelp();
const stream = file ? fs.createReadStream(file) : process.stdin;
stream.pipe(concat(convert));
function convert(data) {
csv2geojson.csv2geojson(
data.toString(),
{latfield: lat, lonfield: lon, delimiter, numericFields},
function (err, data) {
if (err) console.error(JSON.stringify(err, null, 2));
if (line) data = csv2geojson.toLine(data);
console.log(JSON.stringify(data, null, 2));
}
);
}