From 71ba6345529edf306ddee3f8bc5762e9ad54a981 Mon Sep 17 00:00:00 2001 From: stefano bovio Date: Thu, 14 Sep 2023 15:33:13 +0200 Subject: [PATCH] #9346 ensure the zoom is always round before using it as index (#9357) (#9438) --- web/client/components/map/cesium/Map.jsx | 2 +- web/client/components/map/leaflet/Layer.jsx | 5 +++-- web/client/components/map/leaflet/Map.jsx | 2 +- web/client/utils/MapUtils.js | 2 +- web/client/utils/__tests__/MapUtils-test.js | 7 ++++++- web/client/utils/grids/MapGridsUtils.js | 2 +- web/client/utils/grids/__tests__/MapGridsUtils-test.js | 4 ++-- 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/web/client/components/map/cesium/Map.jsx b/web/client/components/map/cesium/Map.jsx index f5144a1fa5..a0f1f8ffc6 100644 --- a/web/client/components/map/cesium/Map.jsx +++ b/web/client/components/map/cesium/Map.jsx @@ -579,7 +579,7 @@ class CesiumMap extends React.Component { roll: this.map.camera.roll } }, - getResolutions()[zoom] + getResolutions()[Math.round(zoom)] ); }; diff --git a/web/client/components/map/leaflet/Layer.jsx b/web/client/components/map/leaflet/Layer.jsx index f33499cb8f..c111dfeecf 100644 --- a/web/client/components/map/leaflet/Layer.jsx +++ b/web/client/components/map/leaflet/Layer.jsx @@ -127,8 +127,9 @@ class LeafletLayer extends React.Component { maxResolution = Infinity, disableResolutionLimits } = options || {}; - if (!disableResolutionLimits && !isNil(resolutions[zoom])) { - const resolution = resolutions[zoom]; + const zoomRound = Math.round(zoom); + if (!disableResolutionLimits && !isNil(resolutions[zoomRound])) { + const resolution = resolutions[zoomRound]; // use similar approach of ol // maxResolution is exclusive // minResolution is inclusive diff --git a/web/client/components/map/leaflet/Map.jsx b/web/client/components/map/leaflet/Map.jsx index 53e7b35f1d..e99ea2e8f0 100644 --- a/web/client/components/map/leaflet/Map.jsx +++ b/web/client/components/map/leaflet/Map.jsx @@ -463,7 +463,7 @@ class LeafletMap extends React.Component { this.props.id, this.props.projection, viewerOptions, // viewerOptions - this.getResolutions()[zoom] // resolution + this.getResolutions()[Math.round(zoom)] // resolution ); }; diff --git a/web/client/utils/MapUtils.js b/web/client/utils/MapUtils.js index 68fdc37d5c..380817b304 100644 --- a/web/client/utils/MapUtils.js +++ b/web/client/utils/MapUtils.js @@ -912,7 +912,7 @@ export function calculateExtent(center = {x: 0, y: 0, crs: "EPSG:3857"}, resolut export const reprojectZoom = (zoom, mapProjection, printProjection) => { const multiplier = METERS_PER_UNIT[getUnits(mapProjection)] / METERS_PER_UNIT[getUnits(printProjection)]; - const mapResolution = getResolutions(mapProjection)[zoom] * multiplier; + const mapResolution = getResolutions(mapProjection)[Math.round(zoom)] * multiplier; const printResolutions = getResolutions(printProjection); const printResolution = printResolutions.reduce((nearest, current) => { diff --git a/web/client/utils/__tests__/MapUtils-test.js b/web/client/utils/__tests__/MapUtils-test.js index 1a1cb6719a..0c6619477a 100644 --- a/web/client/utils/__tests__/MapUtils-test.js +++ b/web/client/utils/__tests__/MapUtils-test.js @@ -40,7 +40,8 @@ import { addRootParentGroup, mapUpdated, getZoomFromResolution, - getResolutionObject + getResolutionObject, + reprojectZoom } from '../MapUtils'; import { VisualizationModes } from '../MapTypeUtils'; @@ -2132,6 +2133,10 @@ describe('Test the MapUtils', () => { const resolution = 1000; // ~zoom 7 in Web Mercator expect(getZoomFromResolution(resolution)).toBe(7); }); + it('reprojectZoom', () => { + expect(reprojectZoom(5, 'EPSG:3857', 'EPSG:4326')).toBe(4); + expect(reprojectZoom(5.2, 'EPSG:3857', 'EPSG:4326')).toBe(4); + }); describe("getResolutionObject tests", () => { const resolutions = [156543, 78271, 39135, 19567, 9783, 4891, 2445, 1222]; it('getResolutionObject for visibility limit type scale', ()=> { diff --git a/web/client/utils/grids/MapGridsUtils.js b/web/client/utils/grids/MapGridsUtils.js index 6a4858b627..183e2919cc 100644 --- a/web/client/utils/grids/MapGridsUtils.js +++ b/web/client/utils/grids/MapGridsUtils.js @@ -408,7 +408,7 @@ export function getGridGeoJson({ pixelRatio = devicePixelRatio, frameSize = 0.0 }) { - const resolution = (resolutions ?? getResolutions(mapProjection))[zoom]; + const resolution = (resolutions ?? getResolutions(mapProjection))[Math.round(zoom)]; const mapToGrid = proj4(mapProjection, gridProjection).forward; const gridToMap = proj4(gridProjection, mapProjection).forward; const projectionCenter = mapToGrid(getCenter(getProjection(gridProjection).extent)); diff --git a/web/client/utils/grids/__tests__/MapGridsUtils-test.js b/web/client/utils/grids/__tests__/MapGridsUtils-test.js index 06dce6d8ac..26b4128135 100644 --- a/web/client/utils/grids/__tests__/MapGridsUtils-test.js +++ b/web/client/utils/grids/__tests__/MapGridsUtils-test.js @@ -245,12 +245,12 @@ describe("getGridGeoJson", () => { expect(geoJson.features[5].geometry.coordinates).toEqual([[-180, -90], [180, -90]]); expect(geoJson.features[7].geometry.coordinates).toEqual([[-180, 90], [180, 90]]); }); - it("map and grid in EPSG:4326 and zoom 5, no labels", () => { + it("map and grid in EPSG:4326 and zoom 5.2, no labels", () => { const geoJson = getGridGeoJson({ mapProjection: "EPSG:4326", gridProjection: "EPSG:4326", extent: [5, 40, 10, 44], - zoom: 5 + zoom: 5.2 }); expect(geoJson.type).toBe("FeatureCollection"); expect(geoJson.features.length).toBe(7);