├── .travis.yml ├── CHANGELOG.md ├── README.md ├── index.js ├── package.json └── test ├── basic.js └── data ├── extraprop.result ├── featurecollection.geojson └── featurecollection.result /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "4" 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | # [0.1.0](https://github.com/tmcw/geojson-stream/compare/v0.0.1...v0.1.0) (2019-01-16) 7 | 8 | 9 | ### Features 10 | 11 | * Support passing a mapping function, plus add example to readme. ([#7](https://github.com/tmcw/geojson-stream/issues/7)) ([5f8c647](https://github.com/tmcw/geojson-stream/commit/5f8c647)) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geojson-stream 2 | 3 | [![Greenkeeper badge](https://badges.greenkeeper.io/tmcw/geojson-stream.svg)](https://greenkeeper.io/) 4 | [![build status](https://secure.travis-ci.org/tmcw/geojson-stream.svg)](http://travis-ci.org/tmcw/geojson-stream) 5 | 6 | Stream features into and out of [GeoJSON](http://geojson.org/) objects 7 | and Feature Collections. Little more than [JSONStream](https://github.com/dominictarr/JSONStream) 8 | with pre-filled settings. 9 | 10 | ## usage 11 | 12 | npm install --save geojson-stream 13 | 14 | ## api 15 | 16 | ### `geojsonStream.stringify()` 17 | 18 | Returns a transform stream that accepts GeoJSON Feature objects and emits 19 | a stringified FeatureCollection. 20 | 21 | ### `geojsonStream.parse(mapFunc)` 22 | 23 | Returns a transform stream that accepts a GeoJSON FeatureCollection as a stream 24 | and emits Feature objects. 25 | 26 | `mapFunc(feature, index)` is an optional function which takes a `Feature`, and its zero-based index in the `FeatureCollection` and returns either a `Feature`, or null/undefined 27 | if the feature should be omitted from output. 28 | 29 | ## example 30 | 31 | ``` 32 | const geojsonStream = require('geojson-stream'); 33 | const fs = require('fs'); 34 | const out = fs.createWriteStream('buildings-with-id.geojson'); 35 | fs 36 | .createReadStream(`buildings.geojson`) 37 | .pipe(geojsonStream.parse((building, index) => { 38 | if (building.geometry.coordinates === null) { 39 | return null; 40 | } 41 | building.id = index; 42 | return building; 43 | })) 44 | .pipe(geojsonStream.stringify()) 45 | .pipe(out); 46 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var JSONStream = require('JSONStream'); 2 | 3 | var open = '{"type":"FeatureCollection","features":[', 4 | close = ']}'; 5 | 6 | module.exports.parse = function(mapFunc) { 7 | var indexFunc; 8 | if (mapFunc) { 9 | indexFunc = function(feature, context) { 10 | return mapFunc(feature, context[1]); 11 | } 12 | } 13 | var jsonstream = JSONStream.parse('features.*', indexFunc); 14 | return jsonstream; 15 | }; 16 | 17 | module.exports.stringify = function() { 18 | var jsonstream = JSONStream.stringify(open, '\n,\n', close); 19 | return jsonstream; 20 | }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geojson-stream", 3 | "version": "0.1.0", 4 | "description": "stream features into and out of geojson", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tap test/*.js", 8 | "release": "standard-version" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/tmcw/geojson-stream.git" 13 | }, 14 | "keywords": [ 15 | "geojson", 16 | "stream", 17 | "maps", 18 | "json", 19 | "pipe" 20 | ], 21 | "author": "Tom MacWright", 22 | "license": "BSD-2-Clause", 23 | "bugs": { 24 | "url": "https://github.com/tmcw/geojson-stream/issues" 25 | }, 26 | "dependencies": { 27 | "JSONStream": "^1.0.0", 28 | "through": "^2.3.4" 29 | }, 30 | "devDependencies": { 31 | "concat-stream": "~1.6.0", 32 | "standard-version": "^4.4.0", 33 | "tap": "~10.3.2" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var test = require('tap').test, 2 | fs = require('fs'), 3 | concat = require('concat-stream'), 4 | geojsonStream = require('../'); 5 | 6 | test('geojson-stream: read', function(t) { 7 | var s = geojsonStream.parse(); 8 | fs.createReadStream(__dirname + '/data/featurecollection.geojson') 9 | .pipe(s).pipe(concat(function(d) { 10 | t.deepEqual(d, JSON.parse(fs.readFileSync(__dirname + '/data/featurecollection.result'))); 11 | t.end(); 12 | })); 13 | }); 14 | 15 | test('geojson-stream: read with map', function(t) { 16 | function addExtraProp(feature, index) { 17 | feature.properties.extraProp = true; 18 | feature.id = index; 19 | return feature; 20 | } 21 | var s = geojsonStream.parse(addExtraProp); 22 | fs.createReadStream(__dirname + '/data/featurecollection.geojson') 23 | .pipe(s).pipe(concat(function(d) { 24 | t.deepEqual(d, JSON.parse(fs.readFileSync(__dirname + '/data/extraprop.result'))); 25 | t.end(); 26 | })); 27 | }); 28 | 29 | 30 | test('geojson-stream: write', function(t) { 31 | var pt = { 32 | type: 'Feature', 33 | geometry: { 34 | type: 'Point', 35 | coordinates: [0, 0] 36 | }, 37 | properties: {} 38 | }; 39 | 40 | var s = geojsonStream.stringify(); 41 | s.pipe(concat(finish)); 42 | s.write(pt); 43 | s.end(); 44 | 45 | function finish(str) { 46 | t.deepEqual(JSON.parse(str), { 47 | type: 'FeatureCollection', 48 | features: [pt] 49 | }); 50 | t.end(); 51 | } 52 | }); 53 | -------------------------------------------------------------------------------- /test/data/extraprop.result: -------------------------------------------------------------------------------- 1 | [ 2 | { "type": "Feature", 3 | "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, 4 | "properties": {"prop0": "value0", "extraProp": true}, 5 | "id": 0 6 | }, 7 | { "type": "Feature", 8 | "geometry": { 9 | "type": "LineString", 10 | "coordinates": [ 11 | [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] 12 | ] 13 | }, 14 | "properties": { 15 | "prop0": "value0", 16 | "prop1": 0.0, 17 | "extraProp": true 18 | }, 19 | "id": 1 20 | }, 21 | { "type": "Feature", 22 | "geometry": { 23 | "type": "Polygon", 24 | "coordinates": [ 25 | [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], 26 | [100.0, 1.0], [100.0, 0.0] ] 27 | ] 28 | }, 29 | "properties": { 30 | "prop0": "value0", 31 | "prop1": {"this": "that"}, 32 | "extraProp": true 33 | }, 34 | "id": 2 35 | } 36 | 37 | ] 38 | -------------------------------------------------------------------------------- /test/data/featurecollection.geojson: -------------------------------------------------------------------------------- 1 | { "type": "FeatureCollection", 2 | "features": [ 3 | { "type": "Feature", 4 | "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, 5 | "properties": {"prop0": "value0"} 6 | }, 7 | { "type": "Feature", 8 | "geometry": { 9 | "type": "LineString", 10 | "coordinates": [ 11 | [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] 12 | ] 13 | }, 14 | "properties": { 15 | "prop0": "value0", 16 | "prop1": 0.0 17 | } 18 | }, 19 | { "type": "Feature", 20 | "geometry": { 21 | "type": "Polygon", 22 | "coordinates": [ 23 | [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], 24 | [100.0, 1.0], [100.0, 0.0] ] 25 | ] 26 | }, 27 | "properties": { 28 | "prop0": "value0", 29 | "prop1": {"this": "that"} 30 | } 31 | } 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /test/data/featurecollection.result: -------------------------------------------------------------------------------- 1 | [ 2 | { "type": "Feature", 3 | "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, 4 | "properties": {"prop0": "value0"} 5 | }, 6 | { "type": "Feature", 7 | "geometry": { 8 | "type": "LineString", 9 | "coordinates": [ 10 | [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] 11 | ] 12 | }, 13 | "properties": { 14 | "prop0": "value0", 15 | "prop1": 0.0 16 | } 17 | }, 18 | { "type": "Feature", 19 | "geometry": { 20 | "type": "Polygon", 21 | "coordinates": [ 22 | [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], 23 | [100.0, 1.0], [100.0, 0.0] ] 24 | ] 25 | }, 26 | "properties": { 27 | "prop0": "value0", 28 | "prop1": {"this": "that"} 29 | } 30 | } 31 | ] 32 | --------------------------------------------------------------------------------