View / refactor how zoom and resolution are computed

This commit aims to simplify the computation of zoom and resolution in the
View class.

Previously zoom levels and resolution computations were mixed in different places,
ie resolution constraints, initial values, etc.

Now the View class only has the `getZoomForResolution` and `getResolutionForZoom`
methods to convert from one system to another.

Other than that, most computations use the resolution system internally.

The `constrainResolution` method also does not exist anymore, and is replaced
by `getValidResolution` and `getValidZoomLevel` public methods.
This commit is contained in:
Olivier Guyot
2019-01-14 16:34:29 +01:00
parent 1f379a06a4
commit 1c5fd62e43
5 changed files with 252 additions and 217 deletions

View File

@@ -103,7 +103,7 @@ describe('ol.interaction.DragZoom', function() {
setTimeout(function() {
const view = map.getView();
const resolution = view.getResolution();
expect(resolution).to.eql(view.constrainResolution(0.5));
expect(resolution).to.eql(view.getValidResolution(0.5));
done();
}, 50);
}, 50);

View File

@@ -12,30 +12,30 @@ describe('ol.resolutionconstraint', function() {
[1000, 500, 250, 100]);
});
describe('delta 0', function() {
describe('direction 0', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, 0, 0)).to.eql(1000);
expect(resolutionConstraint(500, 0, 0)).to.eql(500);
expect(resolutionConstraint(250, 0, 0)).to.eql(250);
expect(resolutionConstraint(100, 0, 0)).to.eql(100);
expect(resolutionConstraint(1000, 0)).to.eql(1000);
expect(resolutionConstraint(500, 0)).to.eql(500);
expect(resolutionConstraint(250, 0)).to.eql(250);
expect(resolutionConstraint(100, 0)).to.eql(100);
});
});
describe('zoom in', function() {
describe('direction 1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, 1, 0)).to.eql(500);
expect(resolutionConstraint(500, 1, 0)).to.eql(250);
expect(resolutionConstraint(250, 1, 0)).to.eql(100);
expect(resolutionConstraint(100, 1, 0)).to.eql(100);
expect(resolutionConstraint(1000, 1)).to.eql(1000);
expect(resolutionConstraint(500, 1)).to.eql(500);
expect(resolutionConstraint(250, 1)).to.eql(250);
expect(resolutionConstraint(100, 1)).to.eql(100);
});
});
describe('zoom out', function() {
describe('direction -1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1000, -1, 0)).to.eql(1000);
expect(resolutionConstraint(500, -1, 0)).to.eql(1000);
expect(resolutionConstraint(250, -1, 0)).to.eql(500);
expect(resolutionConstraint(100, -1, 0)).to.eql(250);
expect(resolutionConstraint(1000, -1)).to.eql(1000);
expect(resolutionConstraint(500, -1)).to.eql(500);
expect(resolutionConstraint(250, -1)).to.eql(250);
expect(resolutionConstraint(100, -1)).to.eql(100);
});
});
});
@@ -50,42 +50,42 @@ describe('ol.resolutionconstraint', function() {
[1000, 500, 250, 100]);
});
describe('delta 0', function() {
describe('direction 0', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0, 0)).to.eql(1000);
expect(resolutionConstraint(950, 0, 0)).to.eql(1000);
expect(resolutionConstraint(550, 0, 0)).to.eql(500);
expect(resolutionConstraint(400, 0, 0)).to.eql(500);
expect(resolutionConstraint(300, 0, 0)).to.eql(250);
expect(resolutionConstraint(200, 0, 0)).to.eql(250);
expect(resolutionConstraint(150, 0, 0)).to.eql(100);
expect(resolutionConstraint(50, 0, 0)).to.eql(100);
expect(resolutionConstraint(1050, 0)).to.eql(1000);
expect(resolutionConstraint(950, 0)).to.eql(1000);
expect(resolutionConstraint(550, 0)).to.eql(500);
expect(resolutionConstraint(400, 0)).to.eql(500);
expect(resolutionConstraint(300, 0)).to.eql(250);
expect(resolutionConstraint(200, 0)).to.eql(250);
expect(resolutionConstraint(150, 0)).to.eql(100);
expect(resolutionConstraint(50, 0)).to.eql(100);
});
});
describe('zoom in', function() {
describe('direction 1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 1, 0)).to.eql(500);
expect(resolutionConstraint(950, 1, 0)).to.eql(500);
expect(resolutionConstraint(550, 1, 0)).to.eql(250);
expect(resolutionConstraint(450, 1, 0)).to.eql(250);
expect(resolutionConstraint(300, 1, 0)).to.eql(100);
expect(resolutionConstraint(200, 1, 0)).to.eql(100);
expect(resolutionConstraint(150, 1, 0)).to.eql(100);
expect(resolutionConstraint(50, 1, 0)).to.eql(100);
expect(resolutionConstraint(1050, 1)).to.eql(1000);
expect(resolutionConstraint(950, 1)).to.eql(1000);
expect(resolutionConstraint(550, 1)).to.eql(1000);
expect(resolutionConstraint(450, 1)).to.eql(500);
expect(resolutionConstraint(300, 1)).to.eql(500);
expect(resolutionConstraint(200, 1)).to.eql(250);
expect(resolutionConstraint(150, 1)).to.eql(250);
expect(resolutionConstraint(50, 1)).to.eql(100);
});
});
describe('zoom out', function() {
describe('direction -1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, -1, 0)).to.eql(1000);
expect(resolutionConstraint(950, -1, 0)).to.eql(1000);
expect(resolutionConstraint(550, -1, 0)).to.eql(1000);
expect(resolutionConstraint(450, -1, 0)).to.eql(1000);
expect(resolutionConstraint(300, -1, 0)).to.eql(500);
expect(resolutionConstraint(200, -1, 0)).to.eql(500);
expect(resolutionConstraint(150, -1, 0)).to.eql(250);
expect(resolutionConstraint(50, -1, 0)).to.eql(250);
expect(resolutionConstraint(1050, -1)).to.eql(1000);
expect(resolutionConstraint(950, -1)).to.eql(500);
expect(resolutionConstraint(550, -1)).to.eql(500);
expect(resolutionConstraint(450, -1)).to.eql(250);
expect(resolutionConstraint(300, -1)).to.eql(250);
expect(resolutionConstraint(200, -1)).to.eql(100);
expect(resolutionConstraint(150, -1)).to.eql(100);
expect(resolutionConstraint(50, -1)).to.eql(100);
});
});
});
@@ -96,54 +96,54 @@ describe('ol.resolutionconstraint', function() {
beforeEach(function() {
resolutionConstraint =
createSnapToPower(2, 1024, 10);
createSnapToPower(2, 1024, 1);
});
describe('delta 0', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, 0, 0)).to.eql(1024);
expect(resolutionConstraint(512, 0, 0)).to.eql(512);
expect(resolutionConstraint(256, 0, 0)).to.eql(256);
expect(resolutionConstraint(128, 0, 0)).to.eql(128);
expect(resolutionConstraint(64, 0, 0)).to.eql(64);
expect(resolutionConstraint(32, 0, 0)).to.eql(32);
expect(resolutionConstraint(16, 0, 0)).to.eql(16);
expect(resolutionConstraint(8, 0, 0)).to.eql(8);
expect(resolutionConstraint(4, 0, 0)).to.eql(4);
expect(resolutionConstraint(2, 0, 0)).to.eql(2);
expect(resolutionConstraint(1, 0, 0)).to.eql(1);
expect(resolutionConstraint(1024, 0)).to.eql(1024);
expect(resolutionConstraint(512, 0)).to.eql(512);
expect(resolutionConstraint(256, 0)).to.eql(256);
expect(resolutionConstraint(128, 0)).to.eql(128);
expect(resolutionConstraint(64, 0)).to.eql(64);
expect(resolutionConstraint(32, 0)).to.eql(32);
expect(resolutionConstraint(16, 0)).to.eql(16);
expect(resolutionConstraint(8, 0)).to.eql(8);
expect(resolutionConstraint(4, 0)).to.eql(4);
expect(resolutionConstraint(2, 0)).to.eql(2);
expect(resolutionConstraint(1, 0)).to.eql(1);
});
});
describe('zoom in', function() {
describe('direction 1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, 1, 0)).to.eql(512);
expect(resolutionConstraint(512, 1, 0)).to.eql(256);
expect(resolutionConstraint(256, 1, 0)).to.eql(128);
expect(resolutionConstraint(128, 1, 0)).to.eql(64);
expect(resolutionConstraint(64, 1, 0)).to.eql(32);
expect(resolutionConstraint(32, 1, 0)).to.eql(16);
expect(resolutionConstraint(16, 1, 0)).to.eql(8);
expect(resolutionConstraint(8, 1, 0)).to.eql(4);
expect(resolutionConstraint(4, 1, 0)).to.eql(2);
expect(resolutionConstraint(2, 1, 0)).to.eql(1);
expect(resolutionConstraint(1, 1, 0)).to.eql(1);
expect(resolutionConstraint(1024, 1)).to.eql(1024);
expect(resolutionConstraint(512, 1)).to.eql(512);
expect(resolutionConstraint(256, 1)).to.eql(256);
expect(resolutionConstraint(128, 1)).to.eql(128);
expect(resolutionConstraint(64, 1)).to.eql(64);
expect(resolutionConstraint(32, 1)).to.eql(32);
expect(resolutionConstraint(16, 1)).to.eql(16);
expect(resolutionConstraint(8, 1)).to.eql(8);
expect(resolutionConstraint(4, 1)).to.eql(4);
expect(resolutionConstraint(2, 1)).to.eql(2);
expect(resolutionConstraint(1, 1)).to.eql(1);
});
});
describe('zoom out', function() {
describe('direction -1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1024, -1, 0)).to.eql(1024);
expect(resolutionConstraint(512, -1, 0)).to.eql(1024);
expect(resolutionConstraint(256, -1, 0)).to.eql(512);
expect(resolutionConstraint(128, -1, 0)).to.eql(256);
expect(resolutionConstraint(64, -1, 0)).to.eql(128);
expect(resolutionConstraint(32, -1, 0)).to.eql(64);
expect(resolutionConstraint(16, -1, 0)).to.eql(32);
expect(resolutionConstraint(8, -1, 0)).to.eql(16);
expect(resolutionConstraint(4, -1, 0)).to.eql(8);
expect(resolutionConstraint(2, -1, 0)).to.eql(4);
expect(resolutionConstraint(1, -1, 0)).to.eql(2);
expect(resolutionConstraint(1024, -1)).to.eql(1024);
expect(resolutionConstraint(512, -1)).to.eql(512);
expect(resolutionConstraint(256, -1)).to.eql(256);
expect(resolutionConstraint(128, -1)).to.eql(128);
expect(resolutionConstraint(64, -1)).to.eql(64);
expect(resolutionConstraint(32, -1)).to.eql(32);
expect(resolutionConstraint(16, -1)).to.eql(16);
expect(resolutionConstraint(8, -1)).to.eql(8);
expect(resolutionConstraint(4, -1)).to.eql(4);
expect(resolutionConstraint(2, -1)).to.eql(2);
expect(resolutionConstraint(1, -1)).to.eql(1);
});
});
});
@@ -154,87 +154,87 @@ describe('ol.resolutionconstraint', function() {
beforeEach(function() {
resolutionConstraint =
createSnapToPower(2, 1024, 10);
createSnapToPower(2, 1024, 1);
});
describe('delta 0, direction 0', function() {
describe('direction 0', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0, 0)).to.eql(1024);
expect(resolutionConstraint(9050, 0, 0)).to.eql(1024);
expect(resolutionConstraint(550, 0, 0)).to.eql(512);
expect(resolutionConstraint(450, 0, 0)).to.eql(512);
expect(resolutionConstraint(300, 0, 0)).to.eql(256);
expect(resolutionConstraint(250, 0, 0)).to.eql(256);
expect(resolutionConstraint(150, 0, 0)).to.eql(128);
expect(resolutionConstraint(100, 0, 0)).to.eql(128);
expect(resolutionConstraint(75, 0, 0)).to.eql(64);
expect(resolutionConstraint(50, 0, 0)).to.eql(64);
expect(resolutionConstraint(40, 0, 0)).to.eql(32);
expect(resolutionConstraint(30, 0, 0)).to.eql(32);
expect(resolutionConstraint(20, 0, 0)).to.eql(16);
expect(resolutionConstraint(12, 0, 0)).to.eql(16);
expect(resolutionConstraint(9, 0, 0)).to.eql(8);
expect(resolutionConstraint(7, 0, 0)).to.eql(8);
expect(resolutionConstraint(5, 0, 0)).to.eql(4);
expect(resolutionConstraint(3.5, 0, 0)).to.eql(4);
expect(resolutionConstraint(2.1, 0, 0)).to.eql(2);
expect(resolutionConstraint(1.9, 0, 0)).to.eql(2);
expect(resolutionConstraint(1.1, 0, 0)).to.eql(1);
expect(resolutionConstraint(0.9, 0, 0)).to.eql(1);
expect(resolutionConstraint(1050, 0)).to.eql(1024);
expect(resolutionConstraint(9050, 0)).to.eql(1024);
expect(resolutionConstraint(550, 0)).to.eql(512);
expect(resolutionConstraint(450, 0)).to.eql(512);
expect(resolutionConstraint(300, 0)).to.eql(256);
expect(resolutionConstraint(250, 0)).to.eql(256);
expect(resolutionConstraint(150, 0)).to.eql(128);
expect(resolutionConstraint(100, 0)).to.eql(128);
expect(resolutionConstraint(75, 0)).to.eql(64);
expect(resolutionConstraint(50, 0)).to.eql(64);
expect(resolutionConstraint(40, 0)).to.eql(32);
expect(resolutionConstraint(30, 0)).to.eql(32);
expect(resolutionConstraint(20, 0)).to.eql(16);
expect(resolutionConstraint(12, 0)).to.eql(16);
expect(resolutionConstraint(9, 0)).to.eql(8);
expect(resolutionConstraint(7, 0)).to.eql(8);
expect(resolutionConstraint(5, 0)).to.eql(4);
expect(resolutionConstraint(3.5, 0)).to.eql(4);
expect(resolutionConstraint(2.1, 0)).to.eql(2);
expect(resolutionConstraint(1.9, 0)).to.eql(2);
expect(resolutionConstraint(1.1, 0)).to.eql(1);
expect(resolutionConstraint(0.9, 0)).to.eql(1);
});
});
describe('delta 0, direction > 0', function() {
describe('direction 1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0, 1)).to.eql(1024);
expect(resolutionConstraint(9050, 0, 1)).to.eql(1024);
expect(resolutionConstraint(550, 0, 1)).to.eql(1024);
expect(resolutionConstraint(450, 0, 1)).to.eql(512);
expect(resolutionConstraint(300, 0, 1)).to.eql(512);
expect(resolutionConstraint(250, 0, 1)).to.eql(256);
expect(resolutionConstraint(150, 0, 1)).to.eql(256);
expect(resolutionConstraint(100, 0, 1)).to.eql(128);
expect(resolutionConstraint(75, 0, 1)).to.eql(128);
expect(resolutionConstraint(50, 0, 1)).to.eql(64);
expect(resolutionConstraint(40, 0, 1)).to.eql(64);
expect(resolutionConstraint(30, 0, 1)).to.eql(32);
expect(resolutionConstraint(20, 0, 1)).to.eql(32);
expect(resolutionConstraint(12, 0, 1)).to.eql(16);
expect(resolutionConstraint(9, 0, 1)).to.eql(16);
expect(resolutionConstraint(7, 0, 1)).to.eql(8);
expect(resolutionConstraint(5, 0, 1)).to.eql(8);
expect(resolutionConstraint(3.5, 0, 1)).to.eql(4);
expect(resolutionConstraint(2.1, 0, 1)).to.eql(4);
expect(resolutionConstraint(1.9, 0, 1)).to.eql(2);
expect(resolutionConstraint(1.1, 0, 1)).to.eql(2);
expect(resolutionConstraint(0.9, 0, 1)).to.eql(1);
expect(resolutionConstraint(1050, 1)).to.eql(1024);
expect(resolutionConstraint(9050, 1)).to.eql(1024);
expect(resolutionConstraint(550, 1)).to.eql(1024);
expect(resolutionConstraint(450, 1)).to.eql(512);
expect(resolutionConstraint(300, 1)).to.eql(512);
expect(resolutionConstraint(250, 1)).to.eql(256);
expect(resolutionConstraint(150, 1)).to.eql(256);
expect(resolutionConstraint(100, 1)).to.eql(128);
expect(resolutionConstraint(75, 1)).to.eql(128);
expect(resolutionConstraint(50, 1)).to.eql(64);
expect(resolutionConstraint(40, 1)).to.eql(64);
expect(resolutionConstraint(30, 1)).to.eql(32);
expect(resolutionConstraint(20, 1)).to.eql(32);
expect(resolutionConstraint(12, 1)).to.eql(16);
expect(resolutionConstraint(9, 1)).to.eql(16);
expect(resolutionConstraint(7, 1)).to.eql(8);
expect(resolutionConstraint(5, 1)).to.eql(8);
expect(resolutionConstraint(3.5, 1)).to.eql(4);
expect(resolutionConstraint(2.1, 1)).to.eql(4);
expect(resolutionConstraint(1.9, 1)).to.eql(2);
expect(resolutionConstraint(1.1, 1)).to.eql(2);
expect(resolutionConstraint(0.9, 1)).to.eql(1);
});
});
describe('delta 0, direction < 0', function() {
describe('direction -1', function() {
it('returns expected resolution value', function() {
expect(resolutionConstraint(1050, 0, -1)).to.eql(1024);
expect(resolutionConstraint(9050, 0, -1)).to.eql(1024);
expect(resolutionConstraint(550, 0, -1)).to.eql(512);
expect(resolutionConstraint(450, 0, -1)).to.eql(256);
expect(resolutionConstraint(300, 0, -1)).to.eql(256);
expect(resolutionConstraint(250, 0, -1)).to.eql(128);
expect(resolutionConstraint(150, 0, -1)).to.eql(128);
expect(resolutionConstraint(100, 0, -1)).to.eql(64);
expect(resolutionConstraint(75, 0, -1)).to.eql(64);
expect(resolutionConstraint(50, 0, -1)).to.eql(32);
expect(resolutionConstraint(40, 0, -1)).to.eql(32);
expect(resolutionConstraint(30, 0, -1)).to.eql(16);
expect(resolutionConstraint(20, 0, -1)).to.eql(16);
expect(resolutionConstraint(12, 0, -1)).to.eql(8);
expect(resolutionConstraint(9, 0, -1)).to.eql(8);
expect(resolutionConstraint(7, 0, -1)).to.eql(4);
expect(resolutionConstraint(5, 0, -1)).to.eql(4);
expect(resolutionConstraint(3.5, 0, -1)).to.eql(2);
expect(resolutionConstraint(2.1, 0, -1)).to.eql(2);
expect(resolutionConstraint(1.9, 0, -1)).to.eql(1);
expect(resolutionConstraint(1.1, 0, -1)).to.eql(1);
expect(resolutionConstraint(0.9, 0, -1)).to.eql(1);
expect(resolutionConstraint(1050, -1)).to.eql(1024);
expect(resolutionConstraint(9050, -1)).to.eql(1024);
expect(resolutionConstraint(550, -1)).to.eql(512);
expect(resolutionConstraint(450, -1)).to.eql(256);
expect(resolutionConstraint(300, -1)).to.eql(256);
expect(resolutionConstraint(250, -1)).to.eql(128);
expect(resolutionConstraint(150, -1)).to.eql(128);
expect(resolutionConstraint(100, -1)).to.eql(64);
expect(resolutionConstraint(75, -1)).to.eql(64);
expect(resolutionConstraint(50, -1)).to.eql(32);
expect(resolutionConstraint(40, -1)).to.eql(32);
expect(resolutionConstraint(30, -1)).to.eql(16);
expect(resolutionConstraint(20, -1)).to.eql(16);
expect(resolutionConstraint(12, -1)).to.eql(8);
expect(resolutionConstraint(9, -1)).to.eql(8);
expect(resolutionConstraint(7, -1)).to.eql(4);
expect(resolutionConstraint(5, -1)).to.eql(4);
expect(resolutionConstraint(3.5, -1)).to.eql(2);
expect(resolutionConstraint(2.1, -1)).to.eql(2);
expect(resolutionConstraint(1.9, -1)).to.eql(1);
expect(resolutionConstraint(1.1, -1)).to.eql(1);
expect(resolutionConstraint(0.9, -1)).to.eql(1);
});
});
});

View File

@@ -1104,7 +1104,9 @@ describe('ol.View', function() {
const min = view.getMinZoom();
expect(view.getResolutionForZoom(max)).to.be(view.getMinResolution());
expect(view.getResolutionForZoom(max + 1)).to.be(view.getMinResolution());
expect(view.getResolutionForZoom(min)).to.be(view.getMaxResolution());
expect(view.getResolutionForZoom(min - 1)).to.be(view.getMaxResolution());
});
it('returns correct zoom levels for specifically configured resolutions', function() {
@@ -1112,11 +1114,30 @@ describe('ol.View', function() {
resolutions: [10, 8, 6, 4, 2]
});
expect(view.getResolutionForZoom(-1)).to.be(10);
expect(view.getResolutionForZoom(0)).to.be(10);
expect(view.getResolutionForZoom(1)).to.be(8);
expect(view.getResolutionForZoom(2)).to.be(6);
expect(view.getResolutionForZoom(3)).to.be(4);
expect(view.getResolutionForZoom(4)).to.be(2);
expect(view.getResolutionForZoom(5)).to.be(2);
});
it('returns correct zoom levels for resolutions with variable zoom levels', function() {
const view = new View({
resolutions: [50, 10, 5, 2.5, 1.25, 0.625]
});
expect(view.getResolutionForZoom(-1)).to.be(50);
expect(view.getResolutionForZoom(0)).to.be(50);
expect(view.getResolutionForZoom(0.5)).to.be(50 / Math.pow(5, 0.5));
expect(view.getResolutionForZoom(1)).to.be(10);
expect(view.getResolutionForZoom(2)).to.be(5);
expect(view.getResolutionForZoom(2.75)).to.be(5 / Math.pow(2, 0.75));
expect(view.getResolutionForZoom(3)).to.be(2.5);
expect(view.getResolutionForZoom(4)).to.be(1.25);
expect(view.getResolutionForZoom(5)).to.be(0.625);
expect(view.getResolutionForZoom(6)).to.be(0.625);
});
});
@@ -1470,6 +1491,41 @@ describe('ol.View', function() {
expect(view.getValidZoomLevel(8)).to.be(6);
});
});
describe('#getValidResolution()', function() {
let view;
const defaultMaxRes = 156543.03392804097;
it('works correctly by snapping to power of 2', function() {
view = new View();
expect(view.getValidResolution(1000000)).to.be(defaultMaxRes);
expect(view.getValidResolution(defaultMaxRes / 8)).to.be(defaultMaxRes / 8);
});
it('works correctly by snapping to a custom zoom factor', function() {
view = new View({
maxResolution: 2500,
zoomFactor: 5,
maxZoom: 4
});
expect(view.getValidResolution(90, 1)).to.be(100);
expect(view.getValidResolution(90, -1)).to.be(20);
expect(view.getValidResolution(20)).to.be(20);
expect(view.getValidResolution(5)).to.be(4);
expect(view.getValidResolution(1)).to.be(4);
});
it('works correctly with a specific resolution set', function() {
view = new View({
zoom: 0,
resolutions: [512, 256, 128, 64, 32, 16, 8]
});
expect(view.getValidResolution(1000, 1)).to.be(512);
expect(view.getValidResolution(260, 1)).to.be(512);
expect(view.getValidResolution(260)).to.be(256);
expect(view.getValidResolution(30)).to.be(32);
expect(view.getValidResolution(30, -1)).to.be(16);
expect(view.getValidResolution(4, -1)).to.be(8);
});
});
});
describe('ol.View.isNoopAnimation()', function() {