Skip to content

Commit

Permalink
Merge pull request #735 from geonetwork/fix-extent
Browse files Browse the repository at this point in the history
Map context: fix WMTS layer extent
  • Loading branch information
fgravin authored Dec 20, 2023
2 parents f49cddd + 206676f commit 95f0151
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 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
1 change: 1 addition & 0 deletions libs/feature/map/src/lib/map-context/map-context.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface MapContextLayerWmsModel {
export interface MapContextLayerWmtsModel {
type: 'wmts'
options: Options
extent?: Extent
}

interface MapContextLayerWfsModel {
Expand Down
62 changes: 49 additions & 13 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,6 +26,7 @@ import {
} from 'ol/interaction'
import { DatasetServiceDistribution } from '@geonetwork-ui/common/domain/model/record'
import MapBrowserEvent from 'ol/MapBrowserEvent'
import type { MapContextLayerWmtsModel } from '../map-context/map-context.model'

jest.mock('ol/proj/proj4', () => {
const fromEPSGCodeMock = jest.fn()
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('MapUtilsService', () => {
window.fetch = originalFetch
})
describe('nominal', () => {
let wmtsOptions: Options
let wmtsLayer: MapContextLayerWmtsModel
beforeEach(async () => {
;(window as any).fetch = jest.fn(() =>

Check warning on line 449 in libs/feature/map/src/lib/utils/map-utils.service.spec.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
Promise.resolve({
Expand All @@ -453,8 +453,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 +463,49 @@ 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?'],
},
})
})
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(() =>

Check warning on line 488 in libs/feature/map/src/lib/utils/map-utils.service.spec.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
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()
})
})
})
})
Expand All @@ -489,7 +525,7 @@ describe('MapUtilsService', () => {
)
try {
await readFirst(
service.getWmtsOptionsFromCapabilities(SAMPLE_WMTS_LINK)
service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK)
)
} catch (e) {
error = e
Expand All @@ -515,7 +551,7 @@ describe('MapUtilsService', () => {
)
try {
await readFirst(
service.getWmtsOptionsFromCapabilities(SAMPLE_WMTS_LINK)
service.getWmtsLayerFromCapabilities(SAMPLE_WMTS_LINK)
)
} catch (e) {
error = e
Expand Down
28 changes: 23 additions & 5 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 @@ -150,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 All @@ -163,9 +171,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 +191,20 @@ ${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',
})
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) {

Check warning on line 208 in libs/feature/map/src/lib/utils/map-utils.service.ts

View workflow job for this annotation

GitHub Actions / Format check, lint, unit tests

Unexpected any. Specify a different type
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 95f0151

Please sign in to comment.