Repository: mapbox/geojson-merge Branch: master Commit: 5c217a3af0a7 Files: 10 Total size: 9.2 KB Directory structure: gitextract_7cz5bma_/ ├── .gitignore ├── .travis.yml ├── CODEOWNERS ├── LICENSE.txt ├── README.md ├── fixtures/ │ └── featureCollection.geojson ├── geojson-merge ├── index.js ├── package.json └── test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ node_modules ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - "0.8" - "0.10" before_install: - npm install -g npm@~1.4.6 ================================================ FILE: CODEOWNERS ================================================ * @mapbox/maps ================================================ FILE: LICENSE.txt ================================================ ISC License Copyright (c) 2017, Mapbox Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ================================================ FILE: README.md ================================================ # geojson-merge [![build status](https://secure.travis-ci.org/mapbox/geojson-merge.png)](http://travis-ci.org/mapbox/geojson-merge) Merge multiple [GeoJSON](http://geojson.org/) files into one FeatureCollection. ## install ```bash $ npm install --save @mapbox/geojson-merge ``` ## API ### merge 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. **Parameters** - `inputs` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>** a list of GeoJSON objects of any type **Examples** ```javascript 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)); ``` Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** a geojson FeatureCollection. ### mergeFeatureCollectionStream 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. **Parameters** - `inputs` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** a list of filenames of GeoJSON files **Examples** ```javascript var geojsonMerge = require('@mapbox/geojson-merge'); var mergedStream = geojsonMerge.mergeFeatureCollectionStream([ 'features.geojson', 'otherFeatures.geojson']) mergedStream.pipe(process.stdout); ``` Returns **[Stream](https://nodejs.org/api/stream.html)** output: a stringified JSON of a FeatureCollection. ## cli Options: > `-s` or `--stream` to use the high-performance streaming mode. This allows > you to combine very large GeoJSON files. Streaming mode requires every > GeoJSON file to contain a FeatureCollection at the top level. ```bash $ npm install -g @mapbox/geojson-merge $ geojson-merge file.geojson otherfile.geojson > combined.geojson ``` ## geojson-merge (for dummies) ### Windows Instructions: 1. Start the `node.js` application 2. Open `cmd.exe` 3. Browse to a folder where you'd like `geojson-merge` installed 4. In `cmd.exe` type the install string from above 5. Wait patiently, it could take a moment to start 6. Use `cd node_modules` to change directory to the `node_modules` folder 7. For simplicity sake, move your .geojson files into this `node_modules` directory 8. Run this command to merge your files: ```bash $ node geojson-merge file1.geojson file2.geojson > merged.geojson ``` **Merging multiple files in a folder** ```bash $ geojson-merge folder/*.geojson > combined.geojson ``` ================================================ FILE: fixtures/featureCollection.geojson ================================================ { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.7109375, 47.100044694025215 ], [ 13.7109375, 48.151428143221224 ], [ 15.710449218749998, 48.151428143221224 ], [ 15.710449218749998, 47.100044694025215 ], [ 13.7109375, 47.100044694025215 ] ] ] } } ] } ================================================ FILE: geojson-merge ================================================ #!/usr/bin/env node var geojsonMerge = require('./'), stream = geojsonMerge.mergeFeatureCollectionStream, merge = geojsonMerge.merge, fs = require('fs'), argv = require('minimist')(process.argv.slice(2)); if (!argv._.length || argv.help) { console.log('usage: geojson-merge [-s] FILE FILE2 FILE3'); console.log('\n --stream (or -s): use streaming mode for large files'); return; } if (argv.s || argv.stream) { stream(argv._).pipe(process.stdout) } else { process.stdout.write(JSON.stringify(merge(argv._.map(function(n) { return JSON.parse(fs.readFileSync(n)); })), null, 2)); } ================================================ FILE: index.js ================================================ 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} 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} 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; ================================================ FILE: package.json ================================================ { "name": "@mapbox/geojson-merge", "version": "1.1.1", "description": "merge multiple geojson files", "main": "index.js", "scripts": { "test": "tape test.js", "doc": "documentation readme -s API" }, "bin": { "geojson-merge": "geojson-merge" }, "repository": { "type": "git", "url": "git@github.com:mapbox/geojson-merge.git" }, "keywords": [ "geojson", "merge", "featurecollection" ], "author": "Tom MacWright", "license": "ISC", "bugs": { "url": "https://github.com/mapbox/geojson-merge/issues" }, "homepage": "https://github.com/mapbox/geojson-merge", "dependencies": { "@mapbox/geojson-normalize": "^0.0.1", "geojson-fixtures": "~0.1.0", "geojson-stream": "0.0.1", "minimist": "^1.2.0", "stream-concat": "0.1.0", "tape": "^4.9.0" }, "devDependencies": { "concat-stream": "^1.6.0", "cz-conventional-changelog": "1.2.0", "documentation": "^4.0.0-beta.18" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } } } ================================================ FILE: test.js ================================================ var geojsonMerge = require('./'), test = require('tape'), fixtures = require('geojson-fixtures') concat = require('concat-stream'); test('merge', function(t) { t.equal(geojsonMerge.merge([fixtures.geometry.point, fixtures.feature.one]).features.length, 2); t.end(); }); test('streaming merge', function (t) { var stream = geojsonMerge.mergeFeatureCollectionStream(['fixtures/featureCollection.geojson', 'fixtures/featureCollection.geojson'], { stream: true }); t.equal(typeof stream, 'object'); t.equal(typeof stream.pipe,'function'); stream.pipe(concat(function (combined) { t.equal(JSON.parse(combined).features.length, 2); t.end(); })); });