import VectorSource from 'ol/source/Vector' ; import { Cluster , Source } from 'ol/source' ; import { Geometry , Point } from 'ol/geom' ; import { Injectable } from '@angular/core' ; import { Layer , Vector as VectorLayer } from 'ol/layer' ; import { HsConfig } from './../../../config.service' ; import { HsMapService } from '../../map/map.service' ; import { HsStylerService } from './../../styles/styler.service' ; import { HsUtilsService } from '../../utils/utils.service' ; @ Injectable ( { providedIn : 'root' , } ) export class HsLayerEditorVectorLayerService { layersClusteredFromStart = [ ] ; constructor ( public HsMapService , public HsUtilsService , public HsStylerService , public HsConfig ) { } param newValue param distance async cluster ( newValue , layer , distance , generateStyle ) { if ( newValue == true ) { if ( ! this . HsUtilsService . instOf ( layer . getSource ( ) , Cluster ) ) { layer . setSource ( this . createClusteredSource ( layer , distance ) ) ; if ( generateStyle ) { await this . HsStylerService . styleClusteredLayer ( layer as VectorLayer < Cluster > ) ; } this . updateFeatureTableLayers ( layer as VectorLayer < VectorSource < Geometry > > ) ; } } else if ( this . HsUtilsService . instOf ( layer . getSource ( ) , Cluster ) ) { layer . setSource ( ( layer . getSource ( ) as Cluster ) . getSource ( ) ) ; } } createClusteredSource ( layer , distance ) { return new Cluster ( { distance , source : layer . getSource ( ) as VectorSource < Geometry > , geometryFunction : function ( feature ) { if ( ! feature ) { return null ; } switch ( feature . getGeometry ( ) . getType ( ) ) { case 'Point' : return feature . getGeometry ( ) ; case 'Circle' : return new Point ( feature . getGeometry ( ) . getCenter ( ) ) ; case 'Polygon' : return feature . getGeometry ( ) . getInteriorPoint ( ) ; case 'LineString' : return new Point ( feature . getGeometry ( ) . getFirstCoordinate ( ) ) ; default : return null ; } } , } ) ; } updateFeatureTableLayers ( layer ) { const currentLayerIndex = this . HsConfig . layersInFeatureTable ?. findIndex ( ( l ) => l == layer ) ; if ( layer && currentLayerIndex > - 1 ) { this . HsConfig . layersInFeatureTable [ currentLayerIndex ] = layer ; } } }