import { AbstractSentinelHubV3Layer } from './AbstractSentinelHubV3Layer' ; import { ApiType , DataProductId , DEMInstanceType , GetMapParams , Interpolator , PaginatedTiles } from './const' ; import { ProcessingPayload } from './processing' ; import { RequestConfiguration } from '../utils/cancelRequests' ; import { ensureTimeout } from '../utils/ensureTimeout' ; import { BBox } from '../bbox' ; import { Polygon } from '@turf/helpers' ; import moment from 'moment' ; export interface ConstructorParameters { instanceId ? : string | null ; layerId ? : string | null ; evalscript ? : string | null ; evalscriptUrl ? : string | null ; dataProduct ? : DataProductId | null ; title ? : string | null ; description ? : string | null ; upsampling ? : Interpolator | null ; downsampling ? : Interpolator | null ; legendUrl ? : string | null ; demInstance ? : DEMInstanceType | null ; egm ? : boolean | null ; clampNegative ? : boolean | null ; } export class AbstractDEMLayer extends AbstractSentinelHubV3Layer { protected demInstance : DEMInstanceType ; protected egm : boolean = null ; protected clampNegative : boolean = null ; private layerUpdatedFromService : boolean = false ; public constructor ( { demInstance , egm , clampNegative , ... rest } ) { super ( rest ) ; this . demInstance = demInstance ; this . egm = egm ; this . clampNegative = clampNegative ; } private shouldUpdateLayerFromService ( ) { if ( this . layerUpdatedFromService ) { return false ; } return ! ( this . demInstance && isDefined ( this . egm ) && isDefined ( this . clampNegative ) ) ; } public async updateLayerFromServiceIfNeeded ( reqConfig ? ) { await ensureTimeout ( async ( innerReqConfig ) => { if ( ! ( this . instanceId && this . layerId ) ) { return ; } await super . updateLayerFromServiceIfNeeded ( innerReqConfig ) ; if ( this . shouldUpdateLayerFromService ( ) ) { const layerParams = await this . fetchLayerParamsFromSHServiceV3 ( innerReqConfig ) ; if ( ! this . demInstance ) { this . demInstance = layerParams [ 'demInstance' ] ? layerParams [ 'demInstance' ] : null ; } if ( ! isDefined ( this . clampNegative ) ) { this . clampNegative = layerParams [ 'clampNegative' ] ? layerParams [ 'clampNegative' ] : null ; } if ( ! isDefined ( this . egm ) ) { this . egm = layerParams [ 'EGM' ] ? layerParams [ 'EGM' ] : null ; } this . layerUpdatedFromService = true ; } } , reqConfig ) ; } public async getMap ( params , api , reqConfig ? ) { return await ensureTimeout ( async ( innerReqConfig ) => { params = await this . decideJpegOrPng ( params , innerReqConfig ) ; if ( api === ApiType . PROCESSING ) { await this . updateLayerFromServiceIfNeeded ( innerReqConfig ) ; } return await super . getMap ( params , api , innerReqConfig ) ; } , reqConfig ) ; } public async _updateProcessingGetMapPayload ( payload , datasetSeqNo = 0 ) { payload = await super . _updateProcessingGetMapPayload ( payload ) ; if ( this . demInstance ) { payload . input . data [ datasetSeqNo ] . dataFilter . demInstance = this . demInstance ; } if ( isDefined ( this . egm ) ) { payload . input . data [ datasetSeqNo ] . processing . egm = this . egm ; } if ( ( ! this . demInstance || this . demInstance === DEMInstanceType . MAPZEN ) && isDefined ( this . clampNegative ) ) { payload . input . data [ datasetSeqNo ] . processing . clampNegative = this . clampNegative ; } delete payload . input . data [ datasetSeqNo ] . dataFilter . mosaickingOrder ; delete payload . input . data [ datasetSeqNo ] . dataFilter . timeRange ; return payload ; } private bboxToPolygon ( bbox ) { const west = Number ( bbox . minX ) ; const south = Number ( bbox . minY ) ; const east = Number ( bbox . maxX ) ; const north = Number ( bbox . maxY ) ; const southWest = [ west , south ] ; const northWest = [ west , north ] ; const northEast = [ east , north ] ; const southEast = [ east , south ] ; return { type : 'Polygon' , crs : { type : 'name' , properties : { name : bbox . crs . urn , } , } , coordinates : [ [ southWest , southEast , northEast , northWest , southWest ] ] , } ; } public async findTiles ( bbox , fromTime , toTime , maxCount = null , offset = null , reqConfig ? ) { const tiles = [ ] ; let hasMore = false ; tiles . push ( { geometry : this . bboxToPolygon ( bbox ) , sensingTime : moment . utc ( toTime ) . toDate ( ) , meta : { } , } ) ; return { tiles : tiles , hasMore : hasMore } ; } public async findDatesUTC ( bbox , fromTime , toTime , reqConfig ? ) { const { tiles } = await this . findTiles ( bbox , fromTime , toTime ) ; return tiles . map ( tile => moment . utc ( tile . sensingTime ) . toDate ( ) ) ; } } const isDefined = ( value ) : boolean => value !== null && value !== undefined ;