Skip to content

Instantly share code, notes, and snippets.

@drewbo
Last active April 10, 2018 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewbo/c2f792869ef07315667cb96f677fe86b to your computer and use it in GitHub Desktop.
Save drewbo/c2f792869ef07315667cb96f677fe86b to your computer and use it in GitHub Desktop.
// 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