Skip to content

Commit

Permalink
Merge pull request #1123 from openlayers/fractional-layer-zoom
Browse files Browse the repository at this point in the history
Properly handle fractional layer minzoom and maxzoom
  • Loading branch information
ahocevar authored Mar 29, 2024
2 parents ce0d9ca + 22475ee commit 7d5c1dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import {bbox as bboxStrategy} from 'ol/loadingstrategy.js';
import {createXYZ} from 'ol/tilegrid.js';
import {
defaultResolutions,
defaultTileGrid,
fetchResource,
getFilterCache,
getFunctionCache,
getGlStyle,
getResolutionForZoom,
getStyleFunctionKey,
getTileJson,
getZoomForResolution,
Expand Down Expand Up @@ -317,7 +317,10 @@ export function applyStyle(
tileGrid.getMinZoom() > 0
) {
layer.setMaxResolution(
tileGrid.getResolution(tileGrid.getMinZoom()) + 1e-15,
getResolutionForZoom(
tileGrid.getMinZoom(),
defaultResolutions,
) + 1e-15,
);
}
});
Expand Down Expand Up @@ -1233,21 +1236,21 @@ export function finalizeLayer(
if (minZoom > 0 || sourceMinZoom > 0) {
layer.setMaxResolution(
Math.min(
defaultTileGrid.getResolution(minZoom),
getResolutionForZoom(minZoom, defaultResolutions),
tileGrid.getResolution(sourceMinZoom),
) + 1e-15,
);
}
if (maxZoom < 24) {
layer.setMinResolution(
defaultTileGrid.getResolution(maxZoom) + 1e-15,
getResolutionForZoom(maxZoom, defaultResolutions),
);
}
}
} else {
if (minZoom > 0) {
layer.setMaxResolution(
defaultTileGrid.getResolution(minZoom) + 1e-15,
getResolutionForZoom(minZoom, defaultResolutions) + 1e-15,
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import TileGrid from 'ol/tilegrid/TileGrid.js';
import TileState from 'ol/TileState.js';
import {VectorTile} from 'ol';
import {expandUrl} from 'ol/tileurlfunction.js';
import {get as getProjection} from 'ol/proj.js';
import {getUid} from 'ol/util.js';
import {normalizeSourceUrl, normalizeStyleUrl} from './mapbox.js';
import {toPromise} from 'ol/functions.js';
Expand Down Expand Up @@ -75,11 +73,6 @@ export const defaultResolutions = (function () {
return resolutions;
})();

export const defaultTileGrid = new TileGrid({
extent: getProjection('EPSG:3857').getExtent(),
resolutions: defaultResolutions,
});

/**
* @param {number} width Width of the canvas.
* @param {number} height Height of the canvas.
Expand Down Expand Up @@ -108,6 +101,12 @@ export function getZoomForResolution(resolution, resolutions) {
return ii - 1;
}

export function getResolutionForZoom(zoom, resolutions) {
const base = Math.floor(zoom);
const factor = Math.pow(2, zoom - base);
return resolutions[base] / factor;
}

const pendingRequests = {};
/**
* @param {ResourceType} resourceType Type of resource to load.
Expand Down
28 changes: 26 additions & 2 deletions test/apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,31 @@ describe('ol-mapbox-style', function () {
defaultResolutions[10] + 1e-15,
);
should(map.getLayers().item(0).getMinResolution()).eql(
defaultResolutions[12] + 1e-15,
defaultResolutions[12],
);
done();
})
.catch(function (err) {
done(err);
});
});

it('respects fractional layer minzoom and maxzoom', function (done) {
context.layers[0].minzoom = 10.5;
context.layers[0].maxzoom = 12.5;
apply(target, context)
.then(function (map) {
should(map.getLayers().item(0).getMaxResolution()).greaterThan(
defaultResolutions[11],
);
should(map.getLayers().item(0).getMaxResolution()).lessThan(
defaultResolutions[10],
);
should(map.getLayers().item(0).getMinResolution()).greaterThan(
defaultResolutions[13],
);
should(map.getLayers().item(0).getMinResolution()).lessThan(
defaultResolutions[12],
);
done();
})
Expand Down Expand Up @@ -893,7 +917,7 @@ describe('ol-mapbox-style', function () {
defaultResolutions[7] + 1e-15,
);
should(map.getLayers().item(0).getMinResolution()).eql(
defaultResolutions[23] + 1e-15,
defaultResolutions[23],
);
done();
})
Expand Down

0 comments on commit 7d5c1dd

Please sign in to comment.