Skip to content

Commit

Permalink
Merge pull request #4278 from rldhont/fix-baselayer-min-max-resolution
Browse files Browse the repository at this point in the history
[Bugfix] Apply min and max resolutions to base layers
  • Loading branch information
rldhont authored Mar 8, 2024
2 parents 6b792e6 + edb18c8 commit 9aa1863
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
13 changes: 12 additions & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Search from './Search.js';
import WMSCapabilities from 'ol/format/WMSCapabilities.js';
import { Coordinate as olCoordinate } from 'ol/coordinate.js'
import { Extent as olExtent, intersects as olExtentIntersects} from 'ol/extent.js';
import { Projection as olProjection, transform as olTransform, transformExtent as olTransformExtent, get as getProjection, clearAllProjections } from 'ol/proj.js';
import { Projection as olProjection, transform as olTransform, transformExtent as olTransformExtent, get as getProjection, clearAllProjections, addCommon } from 'ol/proj.js';
import { register } from 'ol/proj/proj4.js';

import proj4 from 'proj4';
Expand Down Expand Up @@ -94,7 +94,13 @@ export default class Lizmap {
&& Math.abs(extent[3] - bbox.extent[2]) < Math.abs(extent[3] - bbox.extent[3])) {
// If inverted axis are closest, we have to update the projection definition
proj4.defs(configProj.ref, configProj.proj4+' +axis=neu');
// Clear all cached projections and transforms.
clearAllProjections();
// Add transforms to and from EPSG:4326 and EPSG:3857. This function should
// need to be called again after `clearAllProjections()`
// @see ol/proj.js#L731
addCommon();
// Need to register projections again
register(proj4);
break;
}
Expand All @@ -105,6 +111,11 @@ export default class Lizmap {
// if extents do not intersect, we have to update the projection definition
proj4.defs(configProj.ref, configProj.proj4+' +axis=neu');
clearAllProjections();
// Add transforms to and from EPSG:4326 and EPSG:3857. This function should
// need to be called again after `clearAllProjections()`
// @see ol/proj.js#L731
addCommon();
// Need to re register projections again
register(proj4);
break;
}
Expand Down
53 changes: 37 additions & 16 deletions assets/src/modules/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,33 @@ export default class map extends olMap {
0
) - 1;

const proj3857 = getProjection('EPSG:3857');
const max3857Resolution = getWidth(proj3857.getExtent()) / 256;
this._hasEmptyBaseLayer = false;
const baseLayers = [];

for (const baseLayerState of mainLizmap.state.baseLayers.getBaseLayers()) {
let baseLayer;
let layerMinResolution;
let layerMaxResolution;
if (baseLayerState.hasItemState && baseLayerState.hasLayerConfig) {
layerMinResolution = baseLayerState.itemState.wmsMinScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.minScale, metersPerUnit);
layerMaxResolution = baseLayerState.itemState.wmsMaxScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.maxScale, metersPerUnit);
}
if (baseLayerState.type === BaseLayerTypes.XYZ) {
const zMinResolution = max3857Resolution / Math.pow(2, baseLayerState.zmin);
// Get max resolution
if (layerMaxResolution !== undefined || layerMaxResolution > zMinResolution) {
layerMaxResolution = zMinResolution;
}
const zMaxResolution = max3857Resolution / Math.pow(2, baseLayerState.zmax);
// Get min resolution
if (layerMinResolution === undefined || layerMinResolution < zMaxResolution) {
layerMinResolution = zMaxResolution;
}
baseLayer = new TileLayer({
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new XYZ({
url: baseLayerState.url,
projection: baseLayerState.crs,
Expand All @@ -352,11 +372,9 @@ export default class map extends olMap {
})
});
} else if (baseLayerState.type === BaseLayerTypes.WMS) {
let minResolution = baseLayerState.wmsMinScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.minScale, metersPerUnit);
let maxResolution = baseLayerState.wmsMaxScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.maxScale, metersPerUnit);
baseLayer = new ImageLayer({
minResolution: minResolution,
maxResolution: maxResolution,
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new ImageWMS({
url: baseLayerState.url,
projection: baseLayerState.crs,
Expand All @@ -369,14 +387,16 @@ export default class map extends olMap {
})
});
} else if (baseLayerState.type === BaseLayerTypes.WMTS) {
const proj3857 = getProjection('EPSG:3857');
const maxResolution = getWidth(proj3857.getExtent()) / 256;
const resolutions = [];
const matrixIds = [];

for (let i = 0; i < baseLayerState.numZoomLevels; i++) {
matrixIds[i] = i.toString();
resolutions[i] = maxResolution / Math.pow(2, i);
resolutions[i] = max3857Resolution / Math.pow(2, i);
}
// Get min resolution
if (layerMinResolution === undefined || layerMinResolution < resolutions[resolutions.length-1]) {
layerMinResolution = resolutions[resolutions.length-1];
}

const tileGrid = new WMTSTileGrid({
Expand All @@ -391,6 +411,8 @@ export default class map extends olMap {
}

baseLayer = new TileLayer({
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new WMTS({
url: url,
layer: baseLayerState.layer,
Expand All @@ -403,6 +425,8 @@ export default class map extends olMap {
});
} else if (baseLayerState.type === BaseLayerTypes.Bing) {
baseLayer = new TileLayer({
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
preload: Infinity,
source: new BingMaps({
key: baseLayerState.key,
Expand All @@ -413,9 +437,6 @@ export default class map extends olMap {
}),
});
} else if (baseLayerState.type === BaseLayerTypes.Lizmap) {
let minResolution = baseLayerState.wmsMinScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.minScale, metersPerUnit);
let maxResolution = baseLayerState.wmsMaxScaleDenominator <= 1 ? undefined : Utils.getResolutionFromScale(baseLayerState.layerConfig.maxScale, metersPerUnit);

if (baseLayerState.layerConfig.cached) {
const parser = new WMTSCapabilities();
const result = parser.read(lizMap.wmtsCapabilities);
Expand All @@ -425,15 +446,15 @@ export default class map extends olMap {
});

baseLayer = new TileLayer({
minResolution: minResolution,
maxResolution: maxResolution,
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new WMTS(options)
});
} else {
baseLayer = new ImageLayer({
// extent: extent,
minResolution: minResolution,
maxResolution: maxResolution,
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new ImageWMS({
url: mainLizmap.serviceURL,
projection: qgisProjectProjection,
Expand All @@ -450,8 +471,8 @@ export default class map extends olMap {
if (useTileWms) {
baseLayer = new TileLayer({
// extent: extent,
minResolution: minResolution,
maxResolution: maxResolution,
minResolution: layerMinResolution,
maxResolution: layerMaxResolution,
source: new TileWMS({
url: mainLizmap.serviceURL,
projection: qgisProjectProjection,
Expand Down

2 comments on commit 9aa1863

@3liz-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest weekly run of end2end "playwright" tests failed with this latest commit on the branch release_3_6 😣

CC @nboisteault and @Gustry, please have a look to the logs. Maybe it's a false positive ?

Visit https://github.com/3liz/lizmap-web-client/actions/runs/8227187714

@3liz-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest weekly run of end2end "cypress" tests failed with this latest commit on the branch master 😣

CC @nboisteault and @Gustry, please have a look to the logs. Maybe it's a false positive ?

Visit https://github.com/3liz/lizmap-web-client/actions/runs/8227187613

Please sign in to comment.