forked from cutting-room-floor/tilelive-gl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
297 lines (277 loc) · 8.24 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
var CPUCount = require("os").cpus().length;
var debug = require("debug")("tilelive-gl");
var fs = require("fs");
var genericPool = require("generic-pool");
var mbgl = require("@mapbox/mapbox-gl-native");
var request = require("request");
var sharp = require("sharp");
var sm = new (require("@mapbox/sphericalmercator"))();
mbgl.on("message", function(e) {
debug("mbgl: ", e);
if (e.severity == "WARNING" || e.severity == "ERROR") {
console.log("mbgl:", e);
}
});
debug("simd available: " + sharp.simd(true));
module.exports = GL;
function GL(uri, callback) {
this._uri = uri.pathname;
this._style = require(uri.pathname);
this._scale = +uri.query.scale || 1;
this._tilesize = +uri.query.baseTileSize || 256;
this._zoomOffset = uri.query.zoomOffset;
if (uri.query.zoomOffset === undefined) {
this._zoomOffset = -1;
} else {
this._zoomOffset = +uri.query.zoomOffset;
}
this._imageOptions = {};
var imageFormat = uri.query.format || "png";
if (imageFormat.startsWith("png")) {
this._mimetype = "image/png";
this._imageFormat = "png";
//TODO: add support for png pallete options
} else if (imageFormat.startsWith("jpeg")) {
this._mimetype = "image/jpeg";
this._imageFormat = "jpeg";
} else if (imageFormat.startsWith("webp")) {
this._mimetype = "image/webp";
this._imageFormat = "webp";
} else {
throw "Invalid image format";
}
if (imageFormat.startsWith("jpeg") || imageFormat.startsWith("webp")) {
if (imageFormat.endsWith("70")) {
this._imageOptions["quality"] = 70;
} else if (imageFormat.endsWith("80")) {
this._imageOptions["quality"] = 80;
} else if (imageFormat.endsWith("90")) {
this._imageOptions["quality"] = 90;
} else if (imageFormat.endsWith("100")) {
this._imageOptions["quality"] = 100;
}
}
if (imageFormat.startsWith("png")) {
this._imageOptions["adaptiveFiltering"] = false;
this._imageOptions["progressive"] = false;
this._imageOptions["compressionLevel"] = 9;
}
debug("image format:" + imageFormat + "image options:" + JSON.stringify(this._imageOptions));
var thisGL = this;
const factory = {
create: function() {
return new Promise(function(resolve, reject) {
try {
var map = thisGL._getMap();
resolve(map);
} catch (err) {
console.error("Error creating map:", err);
reject(err);
}
});
},
destroy: function(resource) {
return new Promise(function(resolve) {
debug(
"Destroying map for style: " +
thisGL._uri +
" used " +
resource.useCount +
" times."
);
resource.release();
resolve();
});
}
};
var maxMapUses = 0;
if (maxMapUses > 0) {
factory["validate"] = function(resource) {
debug("validate");
return new Promise(function(resolve) {
console.log("validate: usecount:" + resource.useCount);
if (resource.useCount != undefined && resource.useCount > maxMapUses) {
resolve(false);
} else {
resolve(true);
}
});
};
}
var opts = {
max: 20, // maximum size of the pool
min: 0, // minimum size of the pool
testOnBorrow: maxMapUses > 0,
idleTimeoutMillis: 15 * 60 * 1000,
evictionRunIntervalMillis: maxMapUses > 0 ? 5 * 60 * 1000 : 0
};
debug("Creating pool with opts:", opts);
this._pool = genericPool.createPool(factory, opts);
return callback(null, this);
}
GL.prototype._getMap = function() {
debug("Creating map for style: " + this._uri);
var _map = new mbgl.Map({
mode: "tile",
ratio: this._scale,
request: function(req, callback) {
var start = Date.now();
var protocol = req.url.split(":")[0];
if (protocol == "file") {
var path = req.url.split("://")[1];
fs.readFile(path, function(err, data) {
if (err) {
return callback(err);
}
var response = {};
response.data = data;
callback(null, response);
debug(
"Request for " +
req.url +
" complete in " +
(Date.now() - start) +
"ms"
);
});
} else {
request(
{
url: req.url,
encoding: null,
gzip: true
},
function(err, res, body) {
var duration = Date.now() - start;
if (duration > 500) {
if(res === undefined) { // If request timed out response will be undefined
debug(
"Request for " +
req.url +
" failed in " +
duration +
"ms."
);
} else {
// Headers are needed for debugging cases of slow responses from AWS s3
debug(
"Request for " +
req.url +
" complete in " +
duration +
"ms. Headers:" +
JSON.stringify(res.headers || null)
);
}
} else {
debug(
"Request for " + req.url + " complete in " + duration + "ms"
);
}
if (err) {
callback(err);
} else if (res.statusCode == 200) {
var response = {};
if (res.headers.modified) {
response.modified = new Date(res.headers.modified);
}
if (res.headers.expires) {
response.expires = new Date(res.headers.expires);
}
if (res.headers.etag) {
response.etag = res.headers.etag;
}
response.data = body;
callback(null, response);
} else {
//Dont make rendering fail if a resource is missing
return callback(null, {});
}
}
);
}
}
});
_map.load(this._style);
return _map;
};
GL.registerProtocols = function(tilelive) {
tilelive.protocols["gl:"] = GL;
};
GL.prototype.getTile = function(z, x, y, callback) {
var bbox = sm.bbox(+x, +y, +z, false, "900913");
var center = sm.inverse([
bbox[0] + (bbox[2] - bbox[0]) * 0.5,
bbox[1] + (bbox[3] - bbox[1]) * 0.5
]);
var options = {
center: center,
width: this._tilesize,
height: this._tilesize,
zoom: z + this._zoomOffset
};
this.getStatic(options, callback);
};
GL.prototype.getStatic = function(options, callback) {
var start = Date.now();
const mapPromise = this._pool.acquire();
var thisGL = this;
mapPromise.then(function(map) {
debug("Got map in " + (Date.now() - start) + "ms");
if (map.useCount == undefined) {
map.useCount = 1;
} else {
map.useCount++;
}
debug("Map used " + map.useCount + " times.");
start = Date.now();
map.render(options, function(err, data) {
debug("Rendering complete in " + (Date.now() - start) + "ms");
start = Date.now();
thisGL._pool.release(map);
if (err) return callback(err);
var size = thisGL._tilesize * thisGL._scale;
var image = sharp(data, {
raw: {
width: size,
height: size,
channels: 4
}
});
if (thisGL._imageFormat == "png") {
image = image.png(thisGL._imageOptions);
} else if (thisGL._imageFormat == "jpeg") {
image = image.jpeg(thisGL._imageOptions);
} else {
image = image.webp(thisGL._imageOptions);
}
image.toBuffer(function(err, data, info) {
debug("Saving image complete in " + (Date.now() - start) + "ms");
return callback(null, data, { "Content-Type": thisGL._mimetype });
});
});
});
};
GL.prototype.getInfo = function(callback) {
debug("getInfo for style: " + this._uri);
var info = {
minzoom: 0,
maxzoom: 22,
format: this._imageFormat
};
if (this._style["name"]) {
info["name"] = this._style["name"];
}
if (this._style["center"]) {
info["center"] = [
this._style["center"][0],
this._style["center"][1],
this._style["zoom"] || 10
];
}
if (callback) {
return callback(null, info);
} else {
return info;
}
};