From b925b260764809ea0ac30f41c3c91bf5fe04f958 Mon Sep 17 00:00:00 2001 From: rldhont Date: Fri, 8 Mar 2024 09:21:10 +0100 Subject: [PATCH 1/2] [Bugfix] Apply min and max resolutions to base layers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now it is possible to define base layers in the project, so it is possible de defined min and max scale for these layers. Funded by Terre de Provence Agglomération --- assets/src/modules/map.js | 53 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/assets/src/modules/map.js b/assets/src/modules/map.js index 575c65dc15..b03983014a 100644 --- a/assets/src/modules/map.js +++ b/assets/src/modules/map.js @@ -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, @@ -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, @@ -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({ @@ -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, @@ -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, @@ -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); @@ -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, @@ -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, From edb18c84e8ae7965f454d31ff47b31083cb07b48 Mon Sep 17 00:00:00 2001 From: rldhont Date: Fri, 8 Mar 2024 11:04:40 +0100 Subject: [PATCH 2/2] Fix JS: addCommon projections after clearAllProjections --- assets/src/modules/Lizmap.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/assets/src/modules/Lizmap.js b/assets/src/modules/Lizmap.js index d47c5152cf..4b7b4f562f 100644 --- a/assets/src/modules/Lizmap.js +++ b/assets/src/modules/Lizmap.js @@ -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'; @@ -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; } @@ -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; }