Skip to content

Commit

Permalink
refactor(wmts): return the layer from the getCapabilities
Browse files Browse the repository at this point in the history
instead of just returning the Options
  • Loading branch information
fgravin committed Dec 19, 2023
1 parent 19be416 commit 76d7975
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ export class AddLayerRecordPreviewComponent extends RecordPreviewComponent {
name: link.name,
})
} else if (link.accessServiceProtocol === 'wmts') {
return this.mapUtils.getWmtsOptionsFromCapabilities(link).pipe(
map((options) => ({
type: MapContextLayerTypeEnum.WMTS,
options: options,
}))
)
return this.mapUtils.getWmtsLayerFromCapabilities(link)
}
return throwError(() => 'protocol not supported')
}
Expand Down
28 changes: 16 additions & 12 deletions libs/feature/map/src/lib/utils/map-utils.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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'

jest.mock('ol/proj/proj4', () => {
const fromEPSGCodeMock = jest.fn()
Expand Down Expand Up @@ -444,7 +445,7 @@ describe('MapUtilsService', () => {
window.fetch = originalFetch
})
describe('nominal', () => {
let wmtsOptions: Options
let wmtsLayer: MapContextLayerWmtsModel
beforeEach(async () => {
;(window as any).fetch = jest.fn(() =>
Promise.resolve({
Expand All @@ -453,8 +454,8 @@ describe('MapUtilsService', () => {
text: () => Promise.resolve(SAMPLE_WMTS_CAPABILITIES),
})
)
wmtsOptions = await readFirst(
service.getWmtsOptionsFromCapabilities(SAMPLE_WMTS_LINK)
wmtsLayer = await readFirst(
service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK)
)
})
it('appends query params to the URL', () => {
Expand All @@ -463,13 +464,16 @@ describe('MapUtilsService', () => {
)
})
it('returns appropriate WMTS options', () => {
expect(wmtsOptions).toMatchObject({
format: 'image/jpeg',
layer: 'GEOGRAPHICALGRIDSYSTEMS.ETATMAJOR10',
matrixSet: 'PM',
requestEncoding: 'KVP',
style: 'normal',
urls: ['https://wxs.ign.fr/cartes/geoportail/wmts?'],
expect(wmtsLayer).toMatchObject({
type: 'wmts',
options: {
format: 'image/jpeg',
layer: 'GEOGRAPHICALGRIDSYSTEMS.ETATMAJOR10',
matrixSet: 'PM',
requestEncoding: 'KVP',
style: 'normal',
urls: ['https://wxs.ign.fr/cartes/geoportail/wmts?'],
},
})
})
})
Expand All @@ -489,7 +493,7 @@ describe('MapUtilsService', () => {
)
try {
await readFirst(
service.getWmtsOptionsFromCapabilities(SAMPLE_WMTS_LINK)
service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK)
)
} catch (e) {
error = e
Expand All @@ -515,7 +519,7 @@ describe('MapUtilsService', () => {
)
try {
await readFirst(
service.getWmtsOptionsFromCapabilities(SAMPLE_WMTS_LINK)
service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK)
)
} catch (e) {
error = e
Expand Down
16 changes: 12 additions & 4 deletions libs/feature/map/src/lib/utils/map-utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
import WMTSCapabilities from 'ol/format/WMTSCapabilities'
import { from, Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
import { MapContextLayerModel } from '../map-context/map-context.model'
import {
MapContextLayerModel,
MapContextLayerTypeEnum,
MapContextLayerWmtsModel,
} from '../map-context/map-context.model'
import { MapUtilsWMSService } from './map-utils-wms.service'
import Collection from 'ol/Collection'
import MapBrowserEvent from 'ol/MapBrowserEvent'
Expand Down Expand Up @@ -163,9 +167,9 @@ export class MapUtilsService {
)
}

getWmtsOptionsFromCapabilities(
getWmtsLayerFromCapabilities(
link: DatasetDistribution
): Observable<Options> {
): Observable<MapContextLayerWmtsModel> {
const getCapabilitiesUrl = new URL(link.url, window.location.toString())
getCapabilitiesUrl.searchParams.set('SERVICE', 'WMTS')
getCapabilitiesUrl.searchParams.set('REQUEST', 'GetCapabilities')
Expand All @@ -183,10 +187,14 @@ ${await response.text()}`)
.then(function (text) {
try {
const result = new WMTSCapabilities().read(text)
return optionsFromCapabilities(result, {
const options = optionsFromCapabilities(result, {
layer: link.name,
matrixSet: 'EPSG:3857',
})
return {
options,
type: MapContextLayerTypeEnum.WMTS as 'wmts',
}
} catch (e: any) {
throw new Error(`WMTS GetCapabilities parsing failed:
${e.stack || e.message || e}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class MapUtilsServiceMock {
}
})
})
getWmtsOptionsFromCapabilities = jest.fn(function () {
getWmtsLayerFromCapabilities = jest.fn(function () {
return new Observable((observer) => {
observer.next(null)
observer.next({ type: 'wmts', options: null })
})
})
prioritizePageScroll = jest.fn()
Expand Down
7 changes: 1 addition & 6 deletions libs/feature/record/src/lib/map-view/map-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,7 @@ export class MapViewComponent implements OnInit, OnDestroy {
link.type === 'service' &&
link.accessServiceProtocol === 'wmts'
) {
return this.mapUtils.getWmtsOptionsFromCapabilities(link).pipe(
map((options) => ({
type: MapContextLayerTypeEnum.WMTS,
options: options,
}))
)
return this.mapUtils.getWmtsLayerFromCapabilities(link)
} else if (
(link.type === 'service' &&
(link.accessServiceProtocol === 'wfs' ||
Expand Down

0 comments on commit 76d7975

Please sign in to comment.