import { Injectable , NgZone } from '@angular/core' ; import Feature from 'ol/Feature' ; import FeatureFormat from 'ol/format/Feature' ; import TileLayer from 'ol/layer/Tile' ; import { Cluster , ImageWMS , Source , TileArcGISRest , TileWMS , Vector as VectorSource , WMTS , XYZ , } from 'ol/source' ; import { GPX , GeoJSON , KML , TopoJSON } from 'ol/format' ; import { Geometry } from 'ol/geom' ; import { Image as ImageLayer , Layer , Vector as VectorLayer } from 'ol/layer' ; import { isEmpty } from 'ol/extent' ; import { HsLanguageService } from '../language/language.service' ; import { HsLayerDescriptor } from '../layermanager/layer-descriptor.interface' ; import { HsMapService } from '../map/map.service' ; import { HsUtilsService } from './utils.service' ; import { METERS_PER_UNIT } from 'ol/proj' ; import { WmsLayer } from '../../common/get-capabilities/wms-get-capabilities-response.interface' ; import { getCluster , getEditor , getName , getShowInLayerManager , getTitle , } from '../../common/layer-extensions' ; @ Injectable ( ) export class HsLayerUtilsService { constructor ( public HsUtilsService , public HsLanguageService , public hsMapService , private zone ) { } param layer returns layerIsZoomable ( layer ) { if ( typeof layer == 'undefined' ) { return false ; } if ( layer . getExtent ( ) ) { return true ; } if ( this . isLayerWMS ( layer ) ) { return true ; } const src = layer . getSource ( ) ; if ( src . getExtent && src . getExtent ( ) && ! isEmpty ( src . getExtent ( ) ) ) { return true ; } return false ; } param layer returns hasNestedLayers ( layer ) { if ( layer === undefined ) { return false ; } return layer . Layer !== undefined ; } param layer returns layerIsStyleable ( layer ) { if ( typeof layer == 'undefined' ) { return false ; } if ( this . HsUtilsService . instOf ( layer , VectorLayer ) ) { return true ; } return false ; } param layer returns isLayerQueryable ( layer ) { return this . isLayerWMS ( layer ) && ! ! this . getLayerParams ( layer ) . INFO_FORMAT ; } param layer returns getLayerTitle ( layer ) { if ( getTitle ( layer ) !== undefined && getTitle ( layer ) != '' ) { return getTitle ( layer ) . replace ( /&#47;/g , '/' ) ; } else { return 'Void' ; } } getURL ( layer ) { const src = layer . getSource ( ) ; if ( this . HsUtilsService . instOf ( src , ImageWMS ) ) { return ( src as ImageWMS ) . getUrl ( ) ; } if ( this . HsUtilsService . instOf ( src , TileArcGISRest ) ) { return ( src as TileArcGISRest ) . getUrls ( ) [ 0 ] ; } if ( this . HsUtilsService . instOf ( src , TileWMS ) ) { return ( src as TileWMS ) . getUrls ( ) [ 0 ] ; } if ( this . HsUtilsService . instOf ( src , WMTS ) ) { return ( src as WMTS ) . getUrls ( ) [ 0 ] ; } if ( this . HsUtilsService . instOf ( src , XYZ ) ) { return ( src as XYZ ) . getUrls ( ) [ 0 ] ; } } param layer returns isLayerWMS ( layer ) { if ( this . HsUtilsService . instOf ( layer , TileLayer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , TileWMS ) ) { return true ; } if ( this . HsUtilsService . instOf ( layer , ImageLayer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , ImageWMS ) ) { return true ; } return false ; } isLayerWMTS ( layer ) { if ( this . HsUtilsService . instOf ( layer , TileLayer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , WMTS ) ) { return true ; } return false ; } isLayerXYZ ( layer ) { if ( this . HsUtilsService . instOf ( layer , TileLayer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , XYZ ) ) { return true ; } return false ; } isLayerArcgis ( layer ) { if ( this . HsUtilsService . instOf ( layer , TileLayer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , TileArcGISRest ) ) { return true ; } return false ; } getLayerSourceFormat ( layer ) { if ( ! this . isLayerVectorLayer ( layer ) ) { return ; } return ( layer as VectorLayer < VectorSource < Geometry > > ) . getSource ( ) ?. getFormat ( ) ; } param layer returns isLayerVectorLayer ( layer ) { if ( this . HsUtilsService . instOf ( layer , VectorLayer ) && ( this . HsUtilsService . instOf ( layer . getSource ( ) , Cluster ) || this . HsUtilsService . instOf ( layer . getSource ( ) , VectorSource ) ) ) { return true ; } return false ; } param layer returns isLayerGeoJSONSource ( layer ) { if ( this . HsUtilsService . instOf ( this . getLayerSourceFormat ( layer ) , GeoJSON ) ) { return true ; } return false ; } param layer returns isLayerTopoJSONSource ( layer ) { if ( this . HsUtilsService . instOf ( this . getLayerSourceFormat ( layer ) , TopoJSON ) ) { return true ; } return false ; } param layer returns isLayerKMLSource ( layer ) { if ( this . HsUtilsService . instOf ( this . getLayerSourceFormat ( layer ) , KML ) ) { return true ; } return false ; } param layer returns isLayerGPXSource ( layer ) { if ( this . HsUtilsService . instOf ( this . getLayerSourceFormat ( layer ) , GPX ) ) { return true ; } return false ; } param layer returns isLayerInManager ( layer ) { return ( getShowInLayerManager ( layer ) === undefined || getShowInLayerManager ( layer ) == true ) ; } getSourceParams ( source ) { return source . getParams ( ) ; } getLayerParams ( layer ) { const src = layer . getSource ( ) ; if ( this . HsUtilsService . instOf ( src , ImageWMS ) ) { return this . getSourceParams ( src as ImageWMS ) ; } if ( this . HsUtilsService . instOf ( src , TileWMS ) ) { return this . getSourceParams ( src as TileWMS ) ; } if ( this . HsUtilsService . instOf ( src , TileArcGISRest ) ) { return this . getSourceParams ( src as TileArcGISRest ) ; } } updateLayerParams ( layer , params ) { const src = layer . getSource ( ) ; if ( this . HsUtilsService . instOf ( src , ImageWMS ) ) { ( src as ImageWMS ) . updateParams ( params ) ; } if ( this . HsUtilsService . instOf ( src , TileWMS ) ) { ( src as TileWMS ) . updateParams ( params ) ; } } param layer returns hasLayerTitle ( layer ) { return getTitle ( layer ) !== undefined && getTitle ( layer ) !== '' ; } param layer returns isLayerEditable ( layer ) { if ( getEditor ( layer ) === undefined ) { return true ; } const editorConfig = getEditor ( layer ) ; if ( editorConfig . editable === undefined ) { return true ; } return editorConfig . editable ; } param layer getLayerName ( layer ) { if ( layer === undefined || ( getShowInLayerManager ( layer ) !== undefined && getShowInLayerManager ( layer ) === false ) ) { return '' ; } else { const layerName = getTitle ( layer ) || getName ( layer ) ; return layerName ; } } param featuresUnder param layer highlightFeatures ( featuresUnder , layer , list ) { const highlightedFeatures = list . filter ( ( record ) => record . highlighted ) . map ( ( record ) => layer . getSource ( ) . getFeatureById ( record . featureId ) ) ; const dontHighlight = highlightedFeatures . filter ( ( feature ) => ! featuresUnder . includes ( feature ) ) . map ( ( f ) => f . getId ( ) ) ; const highlight = featuresUnder . filter ( ( feature ) => ! highlightedFeatures . includes ( feature ) ) . map ( ( f ) => f . getId ( ) ) ; if ( dontHighlight . length > 0 || highlight . length > 0 ) { this . zone . run ( ( ) => { for ( const record of list ) { if ( highlight . includes ( record . featureId ) ) { record . highlighted = true ; } if ( dontHighlight . includes ( record . featureId ) ) { record . highlighted = false ; } } } ) ; } } param layer returns isLayerDrawable ( layer ) { return ( this . HsUtilsService . instOf ( layer , VectorLayer ) && layer . getVisible ( ) && this . isLayerInManager ( layer ) && this . hasLayerTitle ( layer ) && this . isLayerEditable ( layer ) ) ; } param layer returns isLayerClustered ( layer ) { return this . isLayerVectorLayer ( layer ) && getCluster ( layer ) && this . HsUtilsService . instOf ( layer . getSource ( ) , Cluster ) ? true : false ; } translateTitle ( title ) { return this . HsLanguageService . getTranslationIgnoreNonExisting ( 'LAYERS' , title ) ; } param layer returns layerLoaded ( layer ) { return layer . loadProgress ?. loaded ; } param layer returns layerInvalid ( layer ) { return layer . loadProgress ?. error ; } calculateResolutionFromScale ( denominator ) { if ( ! denominator ) { return denominator ; } const view = this . hsMapService . map . getView ( ) ; const units = view . getProjection ( ) . getUnits ( ) ; const dpi = 25.4 / 0.28 ; const mpu = METERS_PER_UNIT [ units ] ; return denominator / ( mpu * 39.37 * dpi ) ; } }