Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var crypto = require('crypto');
var mapnik = require('mapnik');
var util = require('util');
var sm = new (require('sphericalmercator'))();
var queue = require('d3-queue');

module.exports = Backend;

Expand Down Expand Up @@ -75,7 +76,7 @@ Backend.prototype.getTile = function(z, x, y, callback) {

var size = 0;
var headers = {};
var lookbacks = 0;
var lookbacks = true;

// Overzooming support.
if (bz > backend._maxzoom) {
Expand All @@ -85,7 +86,32 @@ Backend.prototype.getTile = function(z, x, y, callback) {
headers['x-vector-backend-object'] = 'overzoom';
}

source.getTile(bz, bx, by, function sourceGet(err, body, head) {
function testAsync(callback) {
// do nothing
return callback();
}

function loadAsync(lz, lx, ly, callback) {
source.getTile(lz, lx, ly, function (err, body, head) {
if (err && err.message !== 'Tile does not exist') return callback(err);
return callback(null, {
err: err,
body: body,
head: head,
z: lz,
x: lx,
y: ly
});
});
}

function loadMultipleWrapper(err, body, head, callback) {
return callback(err, body, head)
}

source.getTile(bz, bx, by, sourceGet);

function sourceGet(err, body, head) {
if (typeof backend._fillzoom === 'number' &&
err && err.message === 'Tile does not exist' &&
bz > backend._fillzoom) {
Expand All @@ -96,13 +122,26 @@ Backend.prototype.getTile = function(z, x, y, callback) {
return source.getTile(bz, bx, by, sourceGet);
} else if (typeof backend._lookback === 'number' &&
err && err.message === 'Tile does not exist' &&
lookbacks <= backend._lookback) {
lookbacks += 1
bz = bz - 1;
bx = Math.floor(x / Math.pow(2, z - bz));
by = Math.floor(y / Math.pow(2, z - bz));
lookbacks === true) {
lookbacks == false;
var q = new queue.queue();
headers['x-vector-backend-object'] = 'fillzoom';
return source.getTile(bz, bx, by, sourceGet);
for (var lb = 1; lb <= backend._lookback; lb++) {
bz-= 1
bx = Math.floor(x / Math.pow(2, z - bz));
by = Math.floor(y / Math.pow(2, z - bz));
q.defer(loadAsync, bz, bx, by);
}
return q.awaitAll(function(err, data) {
data = data.filter(function(d, i) {
return d === null || i === 0;
});
bz = data[0].z;
bx = data[0].x;
by = data[0].y;
source.getTile(bz, bx, by, sourceGet);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yhahn - Trying to work out a way to not use source.getTile again, and use what we've already loaded here: https://github.com/mapbox/tilelive-vector/pull/131/files#diff-6d11f931df6358ebec2a997c99b90d66R95

});

}
if (err && err.message !== 'Tile does not exist') return callback(err);

Expand Down Expand Up @@ -135,7 +174,7 @@ Backend.prototype.getTile = function(z, x, y, callback) {
headers = head || {};
return makevtile(body);
}
});
};

function makevtile(data, type) {
// If no last modified is provided, use epoch.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"dependencies": {
"aws-sdk": "^2.2.30",
"d3-queue": "^2.0.3",
"s3urls": "^1.3.0",
"mapnik": "~3.5.0",
"tilelive": "~5.12.0",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var tests = {
// Image sources.
'i@2x': ['0.0.0', '1.0.0'],
// Image sources for multiple lookback
l: ['0.0.0', '1.0.0', '1.1.1', '2.0.0', '2.3.3'],
l: ['0.0.0', '1.0.0', '1.1.1', '2.0.0', '2.3.3', '3.1.1'],
// Invalid tiles that are empty
invalid: ['1.1.0', '1.1.1'],

Expand Down