Skip to content

Commit

Permalink
fix(wmts): get extent from capabilities instead of from the tilegrid
Browse files Browse the repository at this point in the history
This fix the zoom to layer option on the map context while loading a WMTS layer
  • Loading branch information
fgravin committed Dec 19, 2023
1 parent afc7fe6 commit 206676f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
36 changes: 34 additions & 2 deletions libs/feature/map/src/lib/utils/map-utils.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 '../map-context/map-context.model'

jest.mock('ol/proj/proj4', () => {
const fromEPSGCodeMock = jest.fn()
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion libs/feature/map/src/lib/utils/map-utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 206676f

Please sign in to comment.