-
Notifications
You must be signed in to change notification settings - Fork 6
/
geocodify.js
executable file
·68 lines (56 loc) · 1.63 KB
/
geocodify.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
#!/usr/bin/env node
var geocodify = require('./'),
fs = require('fs'),
binaryCSV = require('binary-csv'),
stream = require('stream'),
csvWriter = require('csv-write-stream'),
through = require('through2'),
JSONStream = require('JSONStream'),
argv = require('subarg')(process.argv.slice(2));
argv.mapbox_mapid = argv.mapbox_mapid || process.env.MAPBOX_MAPID;
var output = argv.f || argv.output || 'csv';
argv.source = argv.s || argv.source || 'mapbox';
if (argv.r || argv.require) {
geocodify.use(require(argv.r || argv.require));
}
if (argv.help || argv.h || (!argv._.length && process.stdin.isTTY)) {
return help();
}
var source = argv._[0] && fs.createReadStream(argv._[0]) || process.stdin;
var addressFields;
if (argv.addressfields) {
addressFields = argv.addressfields._;
}
if (output == 'csv') {
transform = new stream.PassThrough({
objectMode: true
});
encode = csvWriter();
} else if (output == 'geojson') {
transform = through.obj(makePoint);
encode = JSONStream
.stringify(
'{"type":"FeatureCollection","features":[',
",", "]}");
}
source
.pipe(binaryCSV({json:true}))
.pipe(geocodify(argv.source, addressFields, argv))
.pipe(transform)
.pipe(encode)
.pipe(process.stdout);
function help() {
process.stdout.write(fs.readFileSync(__dirname + '/README.md'));
}
function makePoint(obj, enc, callback) {
var feature = {
type: 'Feature',
properties: obj,
geometry: {
type: 'Point',
coordinates: [obj.lon, obj.lat]
}
};
this.push(feature);
callback();
}