Responds with headers tileserver-zoom and tileserver-center

when request is static/auto
This commit is contained in:
Lawrence Siden 2019-12-02 17:19:08 -05:00
parent 4a602ca8fc
commit 9e18a9b030
2 changed files with 62 additions and 8 deletions

View file

@ -387,7 +387,7 @@ module.exports = function(options, repo, params, id, publicUrl, dataResolver) {
var respondImage = function(z, lon, lat, bearing, pitch,
width, height, scale, format, res, next,
opt_overlay, opt_bbox) {
opt_overlay) {
if (Math.abs(lon) > 180 || Math.abs(lat) > 85.06 ||
lon != lon || lat != lat) {
return res.status(400).send('Invalid center');
@ -419,7 +419,7 @@ module.exports = function(options, repo, params, id, publicUrl, dataResolver) {
params.width *= 2;
params.height *= 2;
}
var tileMargin = Math.max(options.tileMargin || 0, 0);
if (z > 2 && tileMargin > 0) {
params.width += tileMargin * 2 * scale;
@ -440,7 +440,7 @@ module.exports = function(options, repo, params, id, publicUrl, dataResolver) {
channels: 4
}
});
if (z > 2 && tileMargin > 0) {
image.extract({ left: tileMargin * scale, top: tileMargin * scale, width: width * scale, height: height * scale });
}
@ -481,12 +481,12 @@ module.exports = function(options, repo, params, id, publicUrl, dataResolver) {
if (!buffer) {
return res.status(404).send('Not found');
}
if (opt_bbox) {
console.log(opt_bbox)
}
var hdr_center = 'lat=$lat,lon=$lon'
.replace('$lat', lat)
.replace('$lon', lon);
res.set({
'tileserver-Center': hdr_center,
'tileserver-Zoom': z,
'Last-Modified': lastModified,
'Content-Type': 'image/' + format
});

View file

@ -12,6 +12,36 @@ var testStatic = function(prefix, q, format, status, scale, type, query) {
});
};
var testStaticPathCustomHeaders = function(path, expectCenterLat, expectCenterLng, expectZoom) {
it(path + ' returns headers tileserver-center and tileserver-zoom', function(done) {
var test = supertest(app).get(path);
test.expect(200);
test.end(function(err, res) {
if (err) {
return done(err);
}
res.headers['tileserver-center'].should.be.ok();
res.headers['tileserver-zoom'].should.be.ok();
var epsilon = 0.000005;
var match = /^lat=([^,]+),lon=(.*)/.exec(res.headers['tileserver-center']);
match.should.be.ok()
match.should.have.lengthOf(3);
var lat = parseFloat(match[1])
lat.should.be.within(expectCenterLat - epsilon, expectCenterLat + epsilon);
var lng = parseFloat(match[2])
lng.should.be.within(expectCenterLng - epsilon, expectCenterLng + epsilon);
var zoom = parseFloat(res.headers['tileserver-zoom'])
zoom.should.be.within(expectZoom - epsilon, expectZoom + epsilon)
done();
})
})
}
var prefix = 'test-style';
describe('Static endpoints', function() {
@ -91,6 +121,30 @@ describe('Static endpoints', function() {
testStatic(prefix, 'auto/20x20', 'png', 200, 2, /image\/png/, '?path=10,10|20,20');
testStatic(prefix, 'auto/200x200', 'png', 200, 3, /image\/png/, '?path=-10,-10|-20,-20');
});
describe('custom response headers', function() {
var url = '/styles/' + prefix + '/static/auto/800x600.png'
+ '?path=33.060812,-117.183899|48.759505,-98.151760|24.815825,-80.524364'
+ '&latlng=true&padding=0.2'
;
testStaticPathCustomHeaders(
url,
37.746285, // expected lat
-98.854131, // exected lng
4.310567 // exected zoom
);
var url = '/styles/' + prefix + '/static/auto/800x600.png'
+ '?path=53.225355,-168.610992|69.559099,-141.079404'
+ '&latlng=true&padding=0.2'
;
testStaticPathCustomHeaders(
url,
62.487815, // expected lat
-154.845198, // exected lng
4.103840 // exected zoom
);
});
});
describe('invalid requests return 4xx', function() {