-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson-merge.js
54 lines (45 loc) · 1.46 KB
/
geojson-merge.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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const merge = require("geojson-merge");
function match(value) {
return new Promise((resolve, reject) => {
glob(value, (error, files) => {
if(error) {
reject(error);
} else {
resolve(files);
}
});
});
}
/**
* Merges one or more files with geojson object on each line into one file / feature collection
* @param {string[]} inputs
* @param {string} output
*/
function mergeFiles(inputs, output) {
const geojsons = inputs
.map(input => fs.readFileSync(path.join(__dirname, input), "utf8"))
.map(content => content.split("\n"))
.reduce((prev, cur) => [...prev, ...cur])
.filter(line => !!(line.trim()))
.map(line => JSON.parse(line));
const featureCollection = merge(geojsons);
fs.writeFileSync(path.join(__dirname, output), JSON.stringify(featureCollection));
}
(() => {
const args = process.argv.slice(2);
if (args.length < 2) {
console.log("Usage: geojson-merge <inputs> <output>");
return;
}
const inputArgs = args.slice(0, args.length - 1);
const output = args[args.length - 1];
Promise.all(inputArgs.map(arg => match(arg))).then(inputArrays => {
const inputs = inputArrays.reduce((prev, cur) => [...prev, ...cur]);
mergeFiles(inputs, output);
});
})();