-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson-merge.js
43 lines (37 loc) · 1.08 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
var StreamConcat = require('stream-concat')
var geojsonStream = require('geojson-stream')
var glob = require('glob')
var fs = require('fs')
var log = require('single-line-log').stderr
var through2 = require('through2')
var config = require('./config.json')
var geoParse = geojsonStream.parse()
var geoStringify = geojsonStream.stringify()
var outputDirectory = config.outputDirectory
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory)
}
var w = fs.createWriteStream(`${outputDirectory}/output.geojson`)
var results = 0
var c = through2({
objectMode: true
}, function(result, enc, callback) {
results++
log('Processing result: ' + results)
this.push(result)
callback()
})
glob('./geojson/*.geojson', function(er, files) {
var streams = files.map(file => {
return fs.createReadStream(file)
})
var streamIndex = 0
var nextStream = function() {
if (streamIndex === streams.length) return null
return streams[streamIndex++]
}
var combinedStream = new StreamConcat(nextStream, {
objectMode: true
})
combinedStream.pipe(geoParse).pipe(c).pipe(geoStringify).pipe(w)
})