From 70852cfb951ecfea3ad2e9fcaa0bb192cb2f1712 Mon Sep 17 00:00:00 2001 From: Florent Gravin Date: Tue, 19 Dec 2023 15:28:58 +0100 Subject: [PATCH] fix(wmts): get extent from capabilities instead of from the tilegrid This fix the zoom to layer option on the map context while loading a WMTS layer --- .../src/lib/utils/map-utils.service.spec.ts | 36 +++++++++++++++++-- .../map/src/lib/utils/map-utils.service.ts | 12 ++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/libs/feature/map/src/lib/utils/map-utils.service.spec.ts b/libs/feature/map/src/lib/utils/map-utils.service.spec.ts index 9ac0a29341..68c5b09fc3 100644 --- a/libs/feature/map/src/lib/utils/map-utils.service.spec.ts +++ b/libs/feature/map/src/lib/utils/map-utils.service.spec.ts @@ -9,7 +9,6 @@ import Map from 'ol/Map' import ImageWMS from 'ol/source/ImageWMS' import TileWMS from 'ol/source/TileWMS' import XYZ from 'ol/source/XYZ' -import { Options } from 'ol/source/WMTS' import { of } from 'rxjs' import { MapUtilsWMSService } from './map-utils-wms.service' import { @@ -27,7 +26,7 @@ import { } from 'ol/interaction' import { DatasetServiceDistribution } from '@geonetwork-ui/common/domain/model/record' import MapBrowserEvent from 'ol/MapBrowserEvent' -import { MapContextLayerWmtsModel } from '@geonetwork-ui/feature/map' +import type { MapContextLayerWmtsModel } from '@geonetwork-ui/feature/map' jest.mock('ol/proj/proj4', () => { const fromEPSGCodeMock = jest.fn() @@ -476,6 +475,39 @@ describe('MapUtilsService', () => { }, }) }) + describe('layer extent', () => { + describe('when the WGS84BoundingBox is defined', () => { + it('set the WGS84BoundingBox', () => { + expect(wmtsLayer.extent).toEqual([ + 1.82682, 48.3847, 2.79738, 49.5142, + ]) + }) + }) + describe('when the WGS84BoundingBox is not defined', () => { + beforeEach(async () => { + ;(window as any).fetch = jest.fn(() => + Promise.resolve({ + ok: true, + status: 200, + text: () => + Promise.resolve( + SAMPLE_WMTS_CAPABILITIES.replace( + /WGS84BoundingBox/g, + 'NoWGS84BoundingBox' + ) + ), + }) + ) + wmtsLayer = await readFirst( + service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK) + ) + }) + + it('set the WGS84BoundingBox', () => { + expect(wmtsLayer.extent).toBeUndefined() + }) + }) + }) }) describe('http error', () => { let error diff --git a/libs/feature/map/src/lib/utils/map-utils.service.ts b/libs/feature/map/src/lib/utils/map-utils.service.ts index daf9304b5a..92ab59ac87 100644 --- a/libs/feature/map/src/lib/utils/map-utils.service.ts +++ b/libs/feature/map/src/lib/utils/map-utils.service.ts @@ -154,7 +154,11 @@ export class MapUtilsService { } else if (layer && layer.type === 'wms') { geographicExtent = this.wmsUtils.getLayerLonLatBBox(layer) } else if (layer && layer.type === 'wmts') { - return of(layer.options.tileGrid.getExtent()) + if (layer.extent) { + geographicExtent = of(layer.extent) + } else { + return of(layer.options.tileGrid.getExtent()) + } } else { return of(null) } @@ -191,9 +195,15 @@ ${await response.text()}`) layer: link.name, matrixSet: 'EPSG:3857', }) + const layerCap = result?.Contents?.Layer.find( + (layer) => layer.Identifier === link.name + ) return { options, type: MapContextLayerTypeEnum.WMTS as 'wmts', + ...(layerCap?.WGS84BoundingBox + ? { extent: layerCap.WGS84BoundingBox } + : {}), } } catch (e: any) { throw new Error(`WMTS GetCapabilities parsing failed: