Last active
April 10, 2018 19:40
-
-
Save drewbo/c2f792869ef07315667cb96f677fe86b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filters the initial geojson "xView_train.geojson" into separate files matching each training tif | |
// use like `node index.js` | |
const fs = require('fs') | |
const _ = require('lodash') | |
const gs = require('geojson-stream') | |
const through2 = require('through2') | |
const log = require('single-line-log').stdout | |
const IMAGES = 3000 | |
// match features with their corresponding image | |
function filterBy (image) { | |
return through2({ objectMode: true }, function (feature, enc, callback) { | |
if (+feature.properties.image_id.replace('.tif', '') === image) { | |
this.push(feature) | |
} | |
callback() | |
}) | |
} | |
// counter for convenience | |
let features = 0 | |
const count = through2({ objectMode: true }, function (feature, enc, callback) { | |
features++ | |
log(features) | |
this.push(feature) | |
callback() | |
}) | |
const inputStream = fs.createReadStream('xView_train.geojson') | |
.pipe(gs.parse()) | |
.pipe(count) | |
inputStream.setMaxListeners(IMAGES) | |
// create write streams | |
_.range(IMAGES).forEach(image => { | |
var w = fs.createWriteStream(`train_${image}.geojson`) | |
inputStream.pipe(filterBy(image)).pipe(gs.stringify()).pipe(w) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment