-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
65 lines (62 loc) · 2.16 KB
/
index.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
var normalize = require('@mapbox/geojson-normalize');
var geojsonStream = require('geojson-stream');
var fs = require('fs');
/**
* Merge a series of GeoJSON objects into one FeatureCollection containing all
* features in all files. The objects can be any valid GeoJSON root object,
* including FeatureCollection, Feature, and Geometry types.
*
* @param {Array<Object>} inputs a list of GeoJSON objects of any type
* @return {Object} a geojson FeatureCollection.
* @example
* var geojsonMerge = require('@mapbox/geojson-merge');
*
* var mergedGeoJSON = geojsonMerge.merge([
* { type: 'Point', coordinates: [0, 1] },
* { type: 'Feature', geometry: { type: 'Point', coordinates: [0, 1] }, properties: {} }
* ]);
*
* console.log(JSON.stringify(mergedGeoJSON));
*/
function merge (inputs) {
var output = {
type: 'FeatureCollection',
features: []
};
for (var i = 0; i < inputs.length; i++) {
var normalized = normalize(inputs[i]);
for (var j = 0; j < normalized.features.length; j++) {
output.features.push(normalized.features[j]);
}
}
return output;
}
/**
* Merge GeoJSON files containing GeoJSON FeatureCollections
* into a single stream of a FeatureCollection as a JSON string.
*
* This is more limited than merge - it only supports FeatureCollections
* as input - but more performant, since it can operate on GeoJSON files
* larger than what you can keep in memory at one time.
* @param {Array<string>} inputs a list of filenames of GeoJSON files
* @returns {Stream} output: a stringified JSON of a FeatureCollection.
* @example
* var geojsonMerge = require('@mapbox/geojson-merge');
*
* var mergedStream = geojsonMerge.mergeFeatureCollectionStream([
* 'features.geojson',
* 'otherFeatures.geojson'])
*
* mergedStream.pipe(process.stdout);
*/
function mergeFeatureCollectionStream (inputs) {
var out = geojsonStream.stringify();
inputs.forEach(function(file) {
fs.createReadStream(file)
.pipe(geojsonStream.parse())
.pipe(out);
});
return out;
}
module.exports.merge = merge;
module.exports.mergeFeatureCollectionStream = mergeFeatureCollectionStream;