Last active
April 10, 2017 05:00
-
-
Save jaskiratr/85069a95aff0ad46604bb9e8ae167223 to your computer and use it in GitHub Desktop.
Convert JSON to geoJSON
This file contains 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
/* | |
Warning : This is overwrite .geojson filename in the directory with same filename as .json file | |
To run the script: | |
node convertJson.js <JSON Filename without extension> | |
*/ | |
var Readable = require('stream').Readable | |
var fs = require('fs') | |
var filename | |
process.argv.forEach(function(val, index, array) { | |
filename = val | |
}) | |
var input = `${filename}.json` | |
var output = `${filename}.geojson` | |
var json = fs.createReadStream(input) | |
var header = new Readable() | |
header.push(`{ "type": "FeatureCollection","features":`) | |
header.push(null) | |
var footer = new Readable() | |
footer.push(`}`) | |
footer.push(null) | |
header.pipe(fs.createWriteStream(output)) | |
header.on('end', function() { | |
json.pipe(fs.createWriteStream(output, { 'flags': 'a', 'encoding': 'utf8', 'mode': '0666' })) | |
json.on('end', function() { | |
footer.pipe(fs.createWriteStream(output, { 'flags': 'a', 'encoding': 'utf8', 'mode': '0666' })) | |
footer.on('end', function() { | |
console.log('done') | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment