From 9e18a9b030726d1cf76d263103271316dba3ebb8 Mon Sep 17 00:00:00 2001 From: Lawrence Siden Date: Mon, 2 Dec 2019 17:19:08 -0500 Subject: [PATCH] Responds with headers tileserver-zoom and tileserver-center when request is static/auto --- src/serve_rendered.js | 16 ++++++------- test/static.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/serve_rendered.js b/src/serve_rendered.js index 634d60d..3d92c35 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -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 }); diff --git a/test/static.js b/test/static.js index fc3b59b..0311467 100644 --- a/test/static.js +++ b/test/static.js @@ -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() {