diff options
author | Andrew Harvey <andrew@alantgeo.com.au> | 2021-05-20 13:52:42 +1000 |
---|---|---|
committer | Andrew Harvey <andrew@alantgeo.com.au> | 2021-05-20 13:52:42 +1000 |
commit | 0fab36574f2d34dbfa31c27861017eac8fa0fe66 (patch) | |
tree | 36042a18948ccb3d4e9a0dac6f20778f6ee9d132 | |
parent | f967af3e8584636b2d18e3244ff3c15092c29bc2 (diff) |
script to set id
-rwxr-xr-x | bin/id.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/bin/id.js b/bin/id.js new file mode 100755 index 0000000..de60e99 --- /dev/null +++ b/bin/id.js @@ -0,0 +1,68 @@ +#!/usr/bin/env node + +/** + * Set GeoJSON id + */ + +const fs = require('fs') +const { Transform, pipeline } = require('stream') +const ndjson = require('ndjson') + +const argv = require('yargs/yargs')(process.argv.slice(2)) + .option('property', { + type: 'boolean', + description: 'Also set id as a property' + }) + .argv + +if (argv._.length < 2) { + console.error("Usage: ./id.js input.geojson output.geojson") + process.exit(1) +} + +const inputFile = argv._[0] +const outputFile = argv._[1] + +if (!fs.existsSync(inputFile)) { + console.error(`${inputFile} not found`) + process.exit(1) +} + +let index = 0 +const id = new Transform({ + readableObjectMode: true, + writableObjectMode: true, + transform(feature, encoding, callback) { + index++ + + if (process.stdout.isTTY && index % 10000 === 0) { + process.stdout.write(` ${index.toLocaleString()}\r`) + } + + feature.id = index + + if (argv.property) { + feature.properties.id = index + } + + this.push(feature) + + callback() + } +}) + +pipeline( + fs.createReadStream(inputFile), + ndjson.parse(), + id, + ndjson.stringify(), + fs.createWriteStream(outputFile), + err => { + if (err) { + console.log(err) + process.exit(1) + } else { + process.exit(0) + } + } +) |