import Feature from 'ol/Feature' ; import VectorSource from 'ol/source/Vector' ; import { Injectable } from '@angular/core' ; import { Geometry } from 'ol/geom' ; import { Layer , Vector as VectorLayer } from 'ol/layer' ; import { Cluster , Source } from 'ol/source' ; import { HsLanguageService } from '../language/language.service' ; import { HsLayerUtilsService } from '../utils/layer-utils.service' ; import { HsQueryVectorService } from './../query/query-vector.service' ; import { HsUtilsService } from './../utils/utils.service' ; import { getBase , getShowInLayerManager , getTitle , } from '../../common/layer-extensions' ; type FeatureDescriptor = { name : string ; feature : Feature < Geometry > ; attributes : { name ; value ; sanitizedValue ? ; } [ ] ; stats : any [ ] ; } ; @ Injectable ( { providedIn : 'root' , } ) export class HsFeatureTableService { sortReverse = false ; lastSortValue = '' ; features : FeatureDescriptor [ ] = [ ] ; constructor ( public HsUtilsService , public HsLayerUtilsService , public HsQueryVectorService , public HsLanguageService ) { } param layer description returns { any } addLayer ( layer ) { if ( ! getBase ( layer ) && this . HsUtilsService . instOf ( layer , VectorLayer ) && ( getShowInLayerManager ( layer ) === undefined || getShowInLayerManager ( layer ) == true ) ) { return this . wrapLayer ( layer ) ; } return ; } param layer description returns { any } wrapLayer ( layer ) { return { title : getTitle ( layer ) , lyr : layer , type : 'vector' , } ; } param layer description fillFeatureList ( layer ) { const source = this . HsLayerUtilsService . isLayerClustered ( layer ) ? ( layer . getSource ( ) as Cluster ) . getSource ( ) : ( layer . getSource ( ) as VectorSource < Geometry > ) ; this . features = source . getFeatures ( ) . map ( ( f ) => this . describeFeature ( f ) ) . filter ( ( f ) => f ?. attributes ?. length > 0 ) ; } updateFeatureDescription ( feature ) { const newDescriptor = this . describeFeature ( feature ) ; const currentIx = this . features . findIndex ( ( f ) => f . feature == feature ) ; if ( newDescriptor && currentIx > - 1 ) { this . features [ currentIx ] = newDescriptor ; } } addFeatureDescription ( feature ) { const newDescriptor = this . describeFeature ( feature ) ; if ( newDescriptor ) { this . features . push ( newDescriptor ) ; } } removeFeatureDescription ( feature ) { const currentIx = this . features . findIndex ( ( f ) => f . feature == feature ) ; if ( currentIx > - 1 ) { this . features . splice ( currentIx , 1 ) ; } } describeFeature ( feature ) { const attribWrapper = this . HsQueryVectorService . getFeatureAttributes ( feature ) . pop ( ) ; if ( ! attribWrapper ) { return null ; } return { name : this . setFeatureName ( attribWrapper . attributes ) , feature , attributes : this . attributesWithoutFeatureName ( attribWrapper . attributes ) , stats : attribWrapper . stats , } ; } param attributes description returns { any } setFeatureName ( attributes ) { if ( attributes . length > 0 ) { let name = 'Feature' ; for ( const attribute of attributes ) { if ( attribute . name === 'name' ) { name = attribute . value ; } } return name ; } else { return 'Feature' ; } } param attributes description returns { any } attributesWithoutFeatureName ( attributes ) { return attributes . filter ( ( attr ) => attr . name !== 'name' ) ; } param valueName description sortFeaturesBy ( valueName ) { if ( this . features !== undefined && this . features . length > 1 ) { this . lastSortValue === valueName ? ( this . sortReverse = ! this . sortReverse ) : ( this . sortReverse = false ) ; this . features = this . features . sort ( ( a , b ) => this . sortFeatures ( a , b , valueName ) ) ; } } param a param b param valueName description returns { number } sortFeatures ( a , b , valueName ) { let aFeature , bFeature ; let position ; if ( valueName === 'name' ) { aFeature = a [ valueName ] ; bFeature = b [ valueName ] ; } else { aFeature = this . getValue ( a . attributes , valueName ) ; bFeature = this . getValue ( b . attributes , valueName ) ; } if ( aFeature === null ) { position = 1 ; } if ( bFeature === null ) { position = - 1 ; } if ( typeof aFeature == 'string' && typeof bFeature == 'string' ) { position = aFeature . charAt ( 0 ) > bFeature . charAt ( 0 ) ? 1 : aFeature . charAt ( 0 ) < bFeature . charAt ( 0 ) ? - 1 : 0 ; } if ( typeof aFeature == 'number' && typeof bFeature == 'number' ) { position = aFeature - bFeature ; } this . sortReverse ? ( position = position * - 1 ) : position ; this . lastSortValue = valueName ; return position ; } param attributes param valueName description returns { number | string } getValue ( attributes , valueName ) { let value = attributes . filter ( ( attr ) => attr . name == valueName ) . map ( ( attr ) => attr . value ) ; if ( value . length == 0 || value === undefined ) { value = null ; } else { value = value [ 0 ] ; } return value ; } translate ( text ) { const translation = this . HsLanguageService . getTranslationIgnoreNonExisting ( 'FEATURE_TABLE' , text ) ; return translation ; } }