landxml/bin/land2geo

56 lines
1.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
const landxml = require('../bundles/landxml.node');
const fs = require('fs');
const argv = require('yargs')
.scriptName("land2geo")
.usage('$0 <cmd> [args]')
.command('convert <file>', 'convert a LandXML file to GeoJSON', {
alias: 'projection',
default: landxml.PROJECTION_AUTO_DETECT,
describe: 'use the specified source projection',
type: 'string',
builder: yargs => {
yargs
.positional('file', {
describe: 'the LandXML file to convert',
type: 'string'
})
.require('file')
.coerce('file', function (arg) {
return require('fs').readFileSync(arg, 'utf8')
})
.option('p', {
alias: 'projection',
describe: 'the source coordinates projection to use for the LandXML file',
type: 'string'
})
.option('o', {
alias: 'output',
describe: 'the path to the output file. default to stdout',
type: 'string'
})
;
},
handler: argv => {
const converter = new landxml.Converter();
converter.toGeoJSON(argv.file, { projection: argv.projection })
.then(geojson => {
const data = JSON.stringify(geojson, null, 2);
if (argv.output) {
fs.writeFileSync(argv.output, data);
} else {
console.log(data);
}
})
.catch(err => {
console.error(err);
})
;
}
})
.demandCommand()
.help()
.argv