import { Injectable } from '@angular/core' ; import VectorSource from 'ol/source/Vector' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { Feature } from 'ol' ; import { Geometry } from 'ol/geom' ; import { Layer } from 'ol/layer' ; import { Source } from 'ol/source' ; import { HsLanguageService } from '../language/language.service' ; import { HsLayerUtilsService } from '../utils/layer-utils.service' ; import { HsMapService } from '../map/map.service' ; import { HsQueryVectorService } from './query-vector.service' ; import { HsToastService } from '../layout/toast/toast.service' ; import { getTitle } from '../../common/layer-extensions' ; export interface exportFormats { name : 'WKT' | 'GeoJSON' ; ext : string ; serializedData ? : string ; mimeType : string ; downloadData ? : any ; } [ ] ; @ Injectable ( { providedIn : 'root' , } ) export class HsFeatureCommonService { private listSubject = new BehaviorSubject < Layer < Source > [ ] > ( [ ] as Layer < Source > [ ] ) ; availableLayer$ : Observable < Layer < Source > [ ] > = this . listSubject . asObservable ( ) ; constructor ( public hsQueryVectorService , public hsToastService , public hsLanguageService , public hsMapService , public hsLayerUtilsService ) { this . hsMapService . loaded ( ) . then ( ( map ) => { map . getLayers ( ) . on ( 'change:length' , ( ) => { this . updateLayerList ( ) ; } ) ; } ) ; } translateString ( module , text ) { return this . hsLanguageService . getTranslationIgnoreNonExisting ( module , text ) ; } updateLayerList ( ) { const layers = this . hsMapService . getLayersArray ( ) . filter ( ( layer ) => { return this . hsLayerUtilsService . isLayerDrawable ( layer ) ; } ) ; this . listSubject . next ( layers ) ; } toggleExportMenu ( exportFormats , features ) { for ( const format of exportFormats ) { format . serializedData = this . hsQueryVectorService . exportData ( format . name , features ) ; } } moveOrCopyFeature ( type , features , toLayer ) { features . forEach ( ( feature ) => { feature . setStyle ( null ) ; toLayer . getSource ( ) . addFeature ( feature . clone ( ) ) ; if ( type == 'move' ) { this . hsQueryVectorService . removeFeature ( feature ) ; } } ) ; this . hsToastService . createToastPopupMessage ( this . hsLanguageService . getTranslation ( 'QUERY.feature.featureEdited' ) , this . hsLanguageService . getTranslation ( `QUERY.feature.feature${ type }Succ` ) + getTitle ( toLayer ) , { toastStyleClasses : 'bg-success text-light' , serviceCalledFrom : 'HsFeatureCommonService' , } ) ; } }