app.get(tilePattern, function(req, res, next) { var z = req.params.z | 0, x = req.params.x | 0, y = req.params.y | 0; if (req.params.format != tileJSON.format) { return res.status(404).send('Invalid format'); } if (z < tileJSON.minzoom || 0 || x < 0 || y < 0 || z > tileJSON.maxzoom || x >= Math.pow(2, z) || y >= Math.pow(2, z)) { return res.status(404).send('Out of bounds'); } source.getTile(z, x, y, function(err, data, headers) { if (err) { console.log("Getting " + id + " tile " + z + "/" + x + "/" + y + "failed:" + err); if (/does not exist/.test(err.message)) { planetSource.getTile(z, x, y, function(err, data, headers) { console.log("" + id + " tile not found, getting planet tile " + z + "/" + x + "/" + y); if (err) { console.log("Getting planet tile " + z + "/" + x + "/" + y + "failed:" + err); if (/does not exist/.test(err.message)) { return res.status(404).send(err.message); } else { return res.status(500).send(err.message); } } else { if (tileJSON['format'] == 'pbf') { headers['Content-Type'] = 'application/x-protobuf'; headers['Content-Encoding'] = 'gzip'; } delete headers['ETag']; // do not trust the tile ETag -- regenerate res.set(headers); if (data == null) { return res.status(404).send('Not found'); } else { console.log("Planet tile " + z + "/" + x + "/" + y + "found"); return res.status(200).send(data); } } }); } else { return res.status(500).send(err.message); } } else { if (tileJSON['format'] == 'pbf') { headers['Content-Type'] = 'application/x-protobuf'; headers['Content-Encoding'] = 'gzip'; } delete headers['ETag']; // do not trust the tile ETag -- regenerate res.set(headers); if (data == null) { return res.status(404).send('Not found'); } else { return res.status(200).send(data); } } }); });
app.get(tilePattern, function(req, res, next) { var z = req.params.z | 0, x = req.params.x | 0, y = req.params.y | 0; if (req.params.format != tileJSON.format) { return res.status(404).send('Invalid format'); } if (z < tileJSON.minzoom || 0 || x < 0 || y < 0 || z > tileJSON.maxzoom || x >= Math.pow(2, z) || y >= Math.pow(2, z)) { return res.status(404).send('Out of bounds'); } source.getTile(z, x, y, function(err, data, headers) { if (err) { if (/does not exist/.test(err.message)) { return res.status(404).send(err.message); } else { return res.status(500).send(err.message); } } else { var md5 = crypto.createHash('md5').update(data).digest('base64'); headers['content-md5'] = md5; if (tileJSON['format'] == 'pbf') { headers['content-type'] = 'application/x-protobuf'; headers['content-encoding'] = 'gzip'; } res.set(headers); if (data == null) { return res.status(404).send('Not found'); } else { return res.status(200).send(data); } } }); });
.pipe(through(function (tile) { var parts = tile.split('/') var z = parts[0] var x = parts[1] var y = parts[2] var streamSelf = this; var outputFile = outputDir + '/' + tile + '.pbf' if (fs.existsSync(outputFile)) { return } streamSelf.pause() source.getTile(z, x, y, function(err, data, headers) { if (err) { // console.error(err) streamSelf.resume() return } if (data == null) { // console.error('Not found') streamSelf.resume() return } zlib.gunzip(data, function (err, ungzipped) { if (err) { // console.error(err) streamSelf.resume() return } mkdirp(path.dirname(outputFile), function () { fs.writeFile(outputFile, ungzipped, function () { streamSelf.resume() }) }) }) }); }))