-
Notifications
You must be signed in to change notification settings - Fork 5
/
tile-conv.js
executable file
·137 lines (123 loc) · 3.13 KB
/
tile-conv.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env node
var tilelive = require('tilelive');
var path = require('path');
var fs = require("fs");
var zlib = require("zlib");
const
MAX_ZOOM = 14;
require('tilelive-bridge').registerProtocols(tilelive);
var args = process.argv;
args.splice(0, 2); // Remove 'node' and the name of the script
var recursive = false;
var compress = false;
var outputPrefix = null;
// Check for flags
while (args.length > 0) {
if (args[0] == '-r') { // Recursive mode
recursive = true;
args.splice(0, 1);
} else if (args[0] == '-p') { // Output prefix
outputPrefix = args[1];
args.splice(0, 2);
} else if (args[0] == '-c') { // Compression
compress = true;
args.splice(0, 1);
} else {
break; // Done processing flags
}
}
if (args.length < 4) {
console.error('Usage tile-conv <mapnik xlm file> <z> <x> <y>');
process.exit(1);
}
var mapFile = path.resolve(__dirname, args[0]);
var originZ = args[1];
var originX = args[2];
var originY = args[3];
if (outputPrefix == null) {
outputPrefix = path.parse(mapFile).name;
}
var counter = 0;
var zoomLevels = MAX_ZOOM - originZ;
var totalCount = 0;
if (recursive) {
while (zoomLevels >= 0) {
totalCount += Math.pow(4, zoomLevels);
zoomLevels--;
}
} else {
totalCount = 1;
}
console.log('Loading data from ' + mapFile);
tilelive.load("bridge://" + mapFile, function(err, source) {
if (err)
throw err;
if (recursive) {
processTileRecursive(source, originZ, originX, originY, function() {
console.log('Done!');
process.exit(0);
});
} else {
processTile(source, originZ, originX, originY, function() {
console.log('Done!');
process.exit(0);
});
}
});
function processTileRecursive(source, z, x, y, callback) {
processTile(source, z, x, y, function(empty) {
if (z == MAX_ZOOM || empty) {
callback();
return;
}
var nextZ = ++z;
var nextX = x * 2;
var nextY = y * 2;
// Process the four sub quads
processTileRecursive(source, nextZ, nextX, nextY, function() {
processTileRecursive(source, nextZ, nextX + 1, nextY, function() {
processTileRecursive(source, nextZ, nextX, nextY + 1, function() {
processTileRecursive(source, nextZ, nextX + 1, nextY + 1, function() {
callback();
});
});
});
});
});
}
function processTile(source, z, x, y, callback) {
console.log('Processing tile ' + z + ' ' + x + ' ' + y);
// Interface is in XYZ/Google coordinates.
// Use `y = (1 << z) - 1 - y` to flip TMS coordinates.
source.getTile(z, x, y, function(err, tile, headers) {
if (err) {
counter++;
callback(true);
return;
}
var fileName = 'out/' + outputPrefix + '-' + z + '-' + x + '-' + y + '.pbf';
if (compress) {
fileName += '.gz';
}
var finishedCallback = function() {
counter++;
console.log('Completed ' + counter + '/' + totalCount);
callback(false);
};
if (compress) {
writeFile(fileName, tile, finishedCallback);
} else {
zlib.gunzip(tile, function(err, decompressed) {
writeFile(fileName, decompressed, finishedCallback);
});
}
});
}
function writeFile(fileName, data, callback) {
var out = fs.createWriteStream(fileName);
out.on('finish', function() {
callback();
});
out.write(data);
out.end();
}