import { Injectable } from '@angular/core' ; import WMTS , { optionsFromCapabilities } from 'ol/source/WMTS' ; import WMTSCapabilities from 'ol/format/WMTSCapabilities' ; import { Layer , Tile } from 'ol/layer' ; import { Source } from 'ol/source' ; import { CapabilitiesResponseWrapper } from '../../../../common/get-capabilities/capabilities-response-wrapper' ; import { DuplicateHandling , HsMapService } from '../../../map/map.service' ; import { HsAddDataCommonService } from '../../common/common.service' ; import { HsAddDataUrlService } from '../add-data-url.service' ; import { HsLayoutService } from '../../../layout/layout.service' ; import { HsUrlTypeServiceModel } from '../models/url-type-service.model' ; import { addAnchors } from '../../../../common/attribution-utils' ; import { urlDataObject } from '../types/data-object.type' ; @ Injectable ( { providedIn : 'root' } ) export class HsUrlWmtsService implements HsUrlTypeServiceModel { data : urlDataObject ; constructor ( public hsMapService , public hsLayoutService , public hsAddDataUrlService , public hsAddDataCommonService ) { this . setDataToDefault ( ) ; } setDataToDefault ( ) { this . data = { add_all : null , caps : null , description : '' , image_format : '' , services : [ ] , title : '' , version : '' , } ; } async addLayerFromCapabilities ( wrapper ) { const response = wrapper . response ; const error = wrapper . error ; if ( ! response && ! error ) { return ; } if ( error ) { this . hsAddDataCommonService . throwParsingError ( response . message ) ; return ; } try { await this . capabilitiesReceived ( response ) ; if ( this . hsAddDataCommonService . layerToSelect ) { this . hsAddDataCommonService . checkTheSelectedLayer ( this . data . services ) ; return this . addLayers ( true ) ; } } catch ( e ) { this . hsAddDataCommonService . throwParsingError ( e ) ; } } param response async capabilitiesReceived ( response ) { try { const parser = new WMTSCapabilities ( ) ; const caps = parser . read ( response ) ; this . data . caps = caps ; this . data . title = caps . ServiceIdentification . Title || 'Wmts layer' ; this . data . description = addAnchors ( caps . ServiceIdentification . Abstract ) ; this . data . version = caps . Version || caps . version ; this . data . services = caps . Contents . Layer ; this . hsAddDataCommonService . loadingInfo = false ; return this . data . title ; } catch ( e ) { throw new Error ( e ) ; } } addLayersRecursively ( layer , collection ) { if ( ! this . data . add_all || layer . checked ) { collection . push ( this . addLayer ( layer ) ) ; } if ( layer . Layer ) { for ( const sublayer of layer . Layer ) { this . addLayersRecursively ( sublayer , collection ) ; } } } addLayers ( checkedOnly ) { this . data . add_all = checkedOnly ; const collection = [ ] ; for ( const layer of this . data . services ) { this . addLayersRecursively ( layer , collection ) ; } this . hsLayoutService . setMainPanel ( 'layermanager' ) ; this . hsAddDataCommonService . clearParams ( ) ; this . setDataToDefault ( ) ; this . hsAddDataCommonService . setPanelToCatalogue ( ) ; return collection ; } param formats getPreferredFormat ( formats ) { const preferred = formats . find ( ( format ) => format . includes ( 'png' ) ) ; return preferred ? preferred : formats [ 0 ] ; } param sets getPreferredMatrixSet ( sets ) { const supportedFormats = [ '3857' , '4326' , '5514' ] ; const preferred = sets . filter ( ( set ) => supportedFormats . some ( ( v ) => set . TileMatrixSet . includes ( v ) ) ) ; if ( preferred . length != 0 ) { const preferCurrent = preferred . find ( ( set ) => set . TileMatrixSet . includes ( this . hsMapService . map . getView ( ) . getProjection ( ) . getCode ( ) ) ) ; return preferCurrent ? preferCurrent . TileMatrixSet : preferred [ 0 ] . TileMatrixSet ; } return 'EPSG:3857' ; } param response getPreferredInfoFormat ( formats ) { if ( formats ) { const supportedFormats = [ 'html' , 'xml' , 'gml' ] ; const infos = formats . filter ( ( format ) => format . resourceType == 'FeatureInfo' && supportedFormats . some ( ( v ) => format . format . includes ( v ) ) ) ; if ( infos . length != 0 ) { const preferHTML = infos . find ( ( format ) => format . format . includes ( 'html' ) ) ; return preferHTML ? preferHTML . format : infos [ 0 ] . format ; } } } param response addLayer ( layer ) { try { const wmts = new Tile ( { properties : { title : layer . Title , name : layer . Title , info_format : this . getPreferredInfoFormat ( layer . ResourceURL ) , queryCapabilities : false , removable : true , base : layer . base , } , source : new WMTS ( { } as any ) , } ) ; const options = optionsFromCapabilities ( this . data . caps , { layer : layer . Identifier , matrixSet : this . getPreferredMatrixSet ( layer . TileMatrixSetLink ) , format : this . getPreferredFormat ( layer . Format ) , } ) ; const wmtsSource = new WMTS ( options ) ; wmts . setSource ( wmtsSource ) ; this . hsMapService . addLayer ( wmts ) ; layer . base = false ; return wmts ; } catch ( e ) { throw new Error ( e ) ; } } }