-
Notifications
You must be signed in to change notification settings - Fork 1
/
live.js
executable file
·88 lines (74 loc) · 2.32 KB
/
live.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
#!/usr/bin/env node
//get features from a live vector map
//input specifies tile. E.g.
var request = require('request');
var vtfx = require('vtfx');
var zlib = require('zlib');
var path = require('path');
var fs = require('fs');
var mapnik = require('mapnik');
var flatten = require('flatten');
var tilebelt = require('tilebelt');
if (!process.env.MapboxAccessToken) throw new Error('MapboxAccessToken env var must be set');
var zxy = process.argv[2] || '';
if (!/^\d+\/\d+\/\d+$/.test(zxy)) {
console.log('Usage: ./live.js {z}/{x}/{y}');
process.exit(1);
}
var z = parseInt(zxy.split('/')[0]),
x = parseInt(zxy.split('/')[1]),
y = parseInt(zxy.split('/')[2]);
var mapid = "mapbox.mapbox-streets-v5" //process.argv[3] || '';
var maxz=14;
xyz=[x,y,z]
function getzmaxxy(xyz, maxz) {
if (xyz[2]-1 < maxz){
return(xyz);
} else {
return getzmaxxy(tilebelt.getParent(xyz),maxz);
}
}
var zmaxxyz=getzmaxxy(xyz, maxz);
var xmaxz=zmaxxyz[0];
var ymaxz=zmaxxyz[1];
var maxzxy=[maxz,xmaxz,ymaxz].join('/')
//console.log(zmaxxyz,'https://a.tiles.mapbox.com/v4/' + mapid + '/' + maxzxy + '.vector.pbf?access_token=' + process.env.MapboxAccessToken);
var total_nodes = 0;
request({
uri: 'https://a.tiles.mapbox.com/v4/'+mapid+'/' + maxzxy + '.vector.pbf?access_token=' + process.env.MapboxAccessToken,
encoding: null
}, function(err, res, zbody) {
if (res.statusCode == '404') {
console.log('-1');
return
} else {
if (err) throw err;
zlib.gunzip(zbody, function(err, body) {
if (err) throw err;
//var vt = vtfx.decode(body);
//console.log("Tile: %j", vt.layers.length);
var input_vt = new mapnik.VectorTile(maxz,xmaxz,ymaxz);
input_vt.setData( body );
input_vt.parse();
var output_vt = new mapnik.VectorTile(z,x,y);
output_vt.composite([input_vt], {buffer_size:0} );
output_vt.parse();
//console.log(output_vt);
try{
var layers = output_vt.toGeoJSON('__array__');
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
for (var j = 0; j < layer.features.length; j++) {
flat=flatten(layer.features[j].geometry.coordinates);
total_nodes += flat.length;
}
}
} catch (e) {
//console.log("emtpy");
console.log('-1');
return
}
console.log(total_nodes);
});
}
});