Skip to content

Instantly share code, notes, and snippets.

@Rulexec
Created July 10, 2013 13:38
Show Gist options
  • Save Rulexec/5966342 to your computer and use it in GitHub Desktop.
Save Rulexec/5966342 to your computer and use it in GitHub Desktop.
node.js kml to json converter
var sax = require('sax');
var saxStream = sax.createStream(true, {
lowercase: true
});
var layout = {};
saxStream.on('error', function() {
console.log('error', arguments);
process.exit(1);
});
var isCoordinates = false;
saxStream.on('opentag', function(tag) {
if (tag.name === 'coordinates') {
isCoordinates = true;
}
}).on('text', function(text) {
if (isCoordinates) {
var result = text.trim().split(' ').map(function(point) {
var arr = point.split(',').slice(0, 2).map(parseFloat),
tmp = arr[0];
arr[0] = arr[1];
arr[1] = tmp;
return arr;
});
console.log(JSON.stringify(result));
process.exit(0);
}
});
require('fs').createReadStream(process.argv[2]).pipe(saxStream);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment