import type { Map as MbMap } from '@kbn/mapbox-gl' ; import _ from 'lodash' ; import { AbstractLayer } from '../layer' ; import { SOURCE_DATA_REQUEST_ID , LAYER_TYPE , LAYER_STYLE_TYPE } from '../../../../common/constants' ; import { LayerDescriptor } from '../../../../common/descriptor_types' ; import { TileStyle } from '../../styles/tile/tile_style' ; import { ITMSSource } from '../../sources/tms_source' ; import { DataRequestContext } from '../../../actions' ; export class RasterTileLayer extends AbstractLayer { static createDescriptor ( options ) { const tileLayerDescriptor = super . createDescriptor ( options ) ; tileLayerDescriptor . type = LAYER_TYPE . RASTER_TILE ; tileLayerDescriptor . alpha = _ . get ( options , 'alpha' , 1 ) ; tileLayerDescriptor . style = { type : LAYER_STYLE_TYPE . TILE } ; return tileLayerDescriptor ; } private readonly _style : TileStyle ; constructor ( { source , layerDescriptor , } ) { super ( { source , layerDescriptor } ) ; this . _style = new TileStyle ( ) ; } getSource ( ) { return super . getSource ( ) as ITMSSource ; } getStyleForEditing ( ) { return this . _style ; } getStyle ( ) { return this . _style ; } getCurrentStyle ( ) { return this . _style ; } async syncData ( { startLoading , stopLoading , onLoadError , dataFilters } ) { const sourceDataRequest = this . getSourceDataRequest ( ) ; if ( sourceDataRequest ) { return ; } const requestToken = Symbol ( `layer-source-refresh:${ this . getId ( ) } - source` ) ; startLoading ( SOURCE_DATA_REQUEST_ID , requestToken , dataFilters ) ; try { const url = await this . getSource ( ) . getUrlTemplate ( ) ; stopLoading ( SOURCE_DATA_REQUEST_ID , requestToken , { url } , { } ) ; } catch ( error ) { onLoadError ( SOURCE_DATA_REQUEST_ID , requestToken , error . message ) ; } } _getMbLayerId ( ) { return this . makeMbLayerId ( 'raster' ) ; } getMbLayerIds ( ) { return [ this . _getMbLayerId ( ) ] ; } ownsMbLayerId ( mbLayerId ) { return this . _getMbLayerId ( ) === mbLayerId ; } ownsMbSourceId ( mbSourceId ) { return this . getId ( ) === mbSourceId ; } syncLayerWithMB ( mbMap ) { const source = mbMap . getSource ( this . getId ( ) ) ; const mbLayerId = this . _getMbLayerId ( ) ; if ( ! source ) { const sourceDataRequest = this . getSourceDataRequest ( ) ; if ( ! sourceDataRequest ) { return ; } const tmsSourceData = sourceDataRequest . getData ( ) as { url ? : string ; } ; if ( ! tmsSourceData || ! tmsSourceData . url ) { return ; } const mbSourceId = this . getMbSourceId ( ) ; mbMap . addSource ( mbSourceId , { type : 'raster' , tiles : [ tmsSourceData . url ] , tileSize : 256 , scheme : 'xyz' , } ) ; mbMap . addLayer ( { id : mbLayerId , type : 'raster' , source : mbSourceId , minzoom : this . _descriptor . minZoom , maxzoom : this . _descriptor . maxZoom , } ) ; } this . _setTileLayerProperties ( mbMap , mbLayerId ) ; } _setTileLayerProperties ( mbMap , mbLayerId ) { this . syncVisibilityWithMb ( mbMap , mbLayerId ) ; mbMap . setLayerZoomRange ( mbLayerId , this . getMinZoom ( ) , this . getMaxZoom ( ) ) ; mbMap . setPaintProperty ( mbLayerId , 'raster-opacity' , this . getAlpha ( ) ) ; } getLayerTypeIconName ( ) { return 'grid' ; } isBasemap ( order ) { return order === 0 ; } }