-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile-coverage.js
executable file
·74 lines (66 loc) · 2.25 KB
/
tile-coverage.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
#!/usr/bin/env node
var turf = require('turf'),
argv = require('yargs')
.usage('Usage: $0 [options] path/to/tiles/10')
.boolean(['tms', 'yx'])
.describe('tms', 'Use TMS y origin')
.describe('yx', 'load tiles as z/y/x, instead of z/x/y')
.help('h')
.alias('h', 'help')
.argv,
fs = require('fs'),
walk = require('walk'),
path = require('path'),
SphericalMercator = require('sphericalmercator'),
merc = new SphericalMercator({
size: 256
});
var yx = argv.yx;
var tms = argv.tms;
var processDirectory = function(directoryPath, callback) {
var features = [];
var dirInfo = path.parse(directoryPath);
var zoom = parseInt(dirInfo.name);
var walker = walk.walk(directoryPath, { followLinks: false });
walker.on("file", function (root, fileStats, next) {
var tilePath = path.join(root, fileStats.name);
var tilePathInfo = path.parse(tilePath);
var y = parseInt(tilePathInfo.name);
var parentPathInfo = path.parse(tilePathInfo.dir);
var x = parseInt(parentPathInfo.name)
if(yx) {
var tmp = x;
x = y;
y = tmp;
}
var tileBbox = merc.bbox(x, y, zoom, tms);
//console.log(tilePath + " " + zoom + "/" + x + "/" + y + " bbox: " + tileBbox);
features.push(turf.bboxPolygon(tileBbox));
next();
});
walker.on("end", function () {
var featureCollection = turf.featurecollection(features);
var merged = turf.merge(featureCollection);
merged.properties = {
path: directoryPath
};
var simplified = turf.simplify(merged, 0.00001, false);
callback(simplified);
});
};
var produceOutput = function(features) {
var dirFeatureCollection = turf.featurecollection(features);
console.log(JSON.stringify(dirFeatureCollection));
};
var processDirectories = function(directories) {
var dirFeatures = [];
for(var i = 0; i < directories.length; i++) {
processDirectory(directories[i], function(feature){
dirFeatures.push(feature);
if(dirFeatures.length == argv._.length) {
produceOutput(dirFeatures);
}
});
}
};
processDirectories(argv._);