Created
July 10, 2013 13:38
-
-
Save Rulexec/5966342 to your computer and use it in GitHub Desktop.
node.js kml to json converter
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
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