Skip to content

Commit

Permalink
Polyfill sinh for IE11, add tests for wms replacement tokens
Browse files Browse the repository at this point in the history
(see #4814)
  • Loading branch information
bhousel committed Mar 2, 2018
1 parent 429c3b7 commit d9ebdda
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions data/update_imagery.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ sources.concat(whitelist).forEach(function(source) {
var supportedProjection = source.available_projections &&
supportedWMSProjections.find(function(supportedProjection) {
return source.available_projections.some(function(projection) {
return supportedProjection === projection;
return supportedProjection === projection;
})
});
if (source.type === 'wms' && supportedProjection === undefined) return;
Expand All @@ -67,7 +67,7 @@ sources.concat(whitelist).forEach(function(source) {
};

if (source.type === 'wms') {
im.projection = supportedProjection;
im.projection = supportedProjection;
}

var startDate, endDate, isValid;
Expand Down
8 changes: 7 additions & 1 deletion modules/renderer/background_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ function vintageRange(vintage) {
}

function getEPSG3857XY(x, y, z) {
//polyfill for IE11, PhantomJS
var sinh = Math.sinh || function(x) {
var y = Math.exp(x);
return (y - 1 / y) / 2;
};

var zoomSize = Math.pow(2, z);
var lon = x / zoomSize * Math.PI * 2 - Math.PI;
var lat = Math.atan(Math.sinh(Math.PI * (1 - 2 * y / zoomSize)));
var lat = Math.atan(sinh(Math.PI * (1 - 2 * y / zoomSize)));
var mercCoords = d3_geoMercatorRaw(lon, lat);
return {
x: 20037508.34 / Math.PI * mercCoords[0],
Expand Down
43 changes: 33 additions & 10 deletions test/spec/renderer/background_source.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
describe('iD.BackgroundSource', function() {
describe('iD.rendererBackgroundSource', function() {
it('does not error with blank template', function() {
var source = iD.BackgroundSource({ template: '' });
var source = iD.rendererBackgroundSource({ template: '' });
expect(source.url([0,1,2])).to.equal('');
});

it('generates a tile-generating source', function() {
var source = iD.BackgroundSource({ template: '{z}/{x}/{y}' });
it('supports tms replacement tokens', function() {
var source = iD.rendererBackgroundSource({
type: 'tms',
template: '{z}/{x}/{y}'
});
expect(source.url([0,1,2])).to.equal('2/0/1');
});

it('supports wms replacement tokens', function() {
var source = iD.rendererBackgroundSource({
type: 'wms',
projection: 'EPSG:3857',
template: 'SRS={proj}&FORMAT=image/jpeg&WIDTH={width}&HEIGHT={height}&BBOX={bbox}'
});

var result = iD.utilStringQs(source.url([0,1,2]));
expect(result.SRS).to.equal('EPSG:3857');
expect(result.FORMAT).to.equal('image/jpeg');
expect(result.WIDTH).to.equal('256');
expect(result.HEIGHT).to.equal('256');

var bbox = result.BBOX.split(',');
expect(+bbox[0]).to.be.closeTo(-20037508.34, 1e-3);
expect(+bbox[1]).to.be.closeTo(0, 1e-3);
expect(+bbox[2]).to.be.closeTo(-10018754.17, 1e-3);
expect(+bbox[3]).to.be.closeTo(10018754.17, 1e-3);
});

it('supports subdomains', function() {
var source = iD.BackgroundSource({ template: '{switch:a,b}/{z}/{x}/{y}'});
var source = iD.rendererBackgroundSource({ template: '{switch:a,b}/{z}/{x}/{y}'});
expect(source.url([0,1,2])).to.equal('b/2/0/1');
});

it('distributes requests between subdomains', function() {
var source = iD.BackgroundSource({ template: '{switch:a,b}/{z}/{x}/{y}' });
var source = iD.rendererBackgroundSource({ template: '{switch:a,b}/{z}/{x}/{y}' });
expect(source.url([0,1,1])).to.equal('b/1/0/1');
expect(source.url([0,2,1])).to.equal('a/1/0/2');
});

it('correctly displays an overlay with no overzoom specified', function() {
var source = iD.BackgroundSource({ scaleExtent: [6,16] });
var source = iD.rendererBackgroundSource({ scaleExtent: [6,16] });
expect(source.validZoom(10)).to.be.true;
expect(source.validZoom(3)).to.be.false;
expect(source.validZoom(17)).to.be.true;
});

it('correctly displays an overlay with an invalid overzoom', function() {
var source = iD.BackgroundSource({ scaleExtent: [6,16], overzoom: 'gibberish'});
var source = iD.rendererBackgroundSource({ scaleExtent: [6,16], overzoom: 'gibberish'});
expect(source.validZoom(10)).to.be.true;
expect(source.validZoom(3)).to.be.false;
expect(source.validZoom(17)).to.be.true;
});

it('correctly displays an overlay with overzoom:true', function() {
var source = iD.BackgroundSource({ scaleExtent: [6,16], overzoom: true});
var source = iD.rendererBackgroundSource({ scaleExtent: [6,16], overzoom: true});
expect(source.validZoom(10)).to.be.true;
expect(source.validZoom(3)).to.be.false;
expect(source.validZoom(17)).to.be.true;
});

it('correctly displays an overlay with overzoom:false', function() {
var source = iD.BackgroundSource({ scaleExtent: [6,16], overzoom: false});
var source = iD.rendererBackgroundSource({ scaleExtent: [6,16], overzoom: false});
expect(source.validZoom(10)).to.be.true;
expect(source.validZoom(3)).to.be.false;
expect(source.validZoom(17)).to.be.false;
Expand Down

0 comments on commit d9ebdda

Please sign in to comment.