This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-kml-reduce-density.js
87 lines (73 loc) · 1.85 KB
/
node-kml-reduce-density.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// reduce the coordinate density of a KML file.
// by Ben Buckman, newleafdigital.com
// (please copy with credit.)
var async = require('async')
, fs = require('fs')
, path = require('path');
var argv = require('optimist')
.usage('Usage: $0 --in [PATH] --out [PATH] --reduce [NUM]')
.describe('in', 'source KML file')
.describe('out', 'write to new KML file')
.describe('reduce', 'Preserve 1/N coordinates. 1=all, 4=25%, 10=10%. Default 2 (50%).')
.demand(['in', 'out', 'reduce'])
.default('reduce', 2)
.argv;
try {
if (isNaN(argv.reduce) || argv.reduce < 1 || argv.reduce % 1 !== 0) {
throw("--reduce has to be an integer >= 1");
}
}
catch(e) {
console.error(e);
process.exit(1);
}
var origXml, newXml = '';
async.series([
// make sure input file exists
function(next) {
path.exists(argv.in, function(exists){
if (exists) next();
else {
console.error("Input file %s does not exist.", argv.in);
process.exit(1);
}
});
},
// read it
function(next) {
fs.readFile(argv.in, 'utf8', function (err, data) {
if (err) throw err;
origXml = data;
next();
});
},
// parse it
function(next) {
var lines = origXml.split("\n")
, coordRegex = /-?\d+\.\d+,-?\d+\.\d+/g
, ind
, coordCounter = 0;
for (ind = 0; ind < lines.length; ind++) {
isCoordLine = lines[ind].match(coordRegex);
if (! isCoordLine) {
newXml += lines[ind] + '\n';
}
else {
coordCounter++;
if (coordCounter === argv.reduce) {
newXml += lines[ind] + '\n';
coordCounter = 0;
}
// (otherwise skip)
}
}
next();
},
// write
function(next) {
fs.writeFile(argv.out, newXml, 'utf8', function(err) {
if (err) throw(err);
else next();
});
}
]);