import axios , { AxiosRequestConfig } from 'axios' ; import { Polygon , BBox as BBoxTurf , MultiPolygon } from '@turf/helpers' ; import { getAuthToken } from '../auth' ; import { MimeType , GetMapParams , Interpolator , PreviewMode , MosaickingOrder , DataProductId } from './const' ; import { Dataset } from './dataset' ; import { getAxiosReqParams , RequestConfiguration } from '../utils/cancelRequests' ; import { CACHE_CONFIG_30MIN } from '../utils/cacheHandlers' ; enum PreviewModeString { DETAIL = 'DETAIL' , PREVIEW = 'PREVIEW' , EXTENDED_PREVIEW = 'EXTENDED_PREVIEW' } type DataProduct = { '@id' : DataProductId ; } ; export type ProcessingPayload = { input : { bounds : { bbox ? : BBoxTurf ; geometry ? : Polygon | MultiPolygon ; properties : { crs : string ; } ; } ; data : ProcessingPayloadDatasource [ ] ; } ; output : { width : number ; height : number ; responses : [ { identifier : string ; format : { type : MimeType ; } ; } ] ; } ; evalscript ? : string ; dataProduct ? : DataProduct ; } ; export type ProcessingPayloadDatasource = { id ? : string ; dataFilter : { timeRange : { from : string ; to : string ; } ; previewMode ? : PreviewModeString ; mosaickingOrder ? : MosaickingOrder ; [ key ] : any ; } ; processing ? : { upsampling ? : Interpolator ; downsampling ? : Interpolator ; [ key ] : any ; } ; type : string ; } ; export function convertPreviewToString ( preview ) { switch ( preview ) { case 0 : return PreviewModeString . DETAIL ; case 1 : return PreviewModeString . PREVIEW ; case 2 : case 3 : return PreviewModeString . EXTENDED_PREVIEW ; default : throw new Error ( 'Preview mode does not exist, options are 0 (DETAIL), 1 (PREVIEW) or 2/3 (EXTENDED_PREVIEW)' ) ; } } export function createProcessingPayload ( dataset , params , evalscript = null , dataProduct = null , mosaickingOrder = null , upsampling = null , downsampling = null ) { const { bbox } = params ; const payload = { input : { bounds : { bbox : [ bbox . minX , bbox . minY , bbox . maxX , bbox . maxY ] , properties : { crs : params . bbox . crs . opengisUrl , } , } , data : [ { dataFilter : { timeRange : { from : params . fromTime . toISOString ( ) , to : params . toTime . toISOString ( ) , } , mosaickingOrder : mosaickingOrder ? mosaickingOrder : MosaickingOrder . MOST_RECENT , } , processing : { } , type : dataset . shProcessingApiDatasourceAbbreviation , } , ] , } , output : { width : params . width , height : params . height , responses : [ { identifier : params . outputResponseId ? params . outputResponseId : 'default' , format : { type : params . format as MimeType , } , } , ] , } , } ; if ( params . upsampling || upsampling ) { payload . input . data [ 0 ] . processing . upsampling = params . upsampling ? params . upsampling : upsampling ; } if ( params . downsampling || downsampling ) { payload . input . data [ 0 ] . processing . downsampling = params . downsampling ? params . downsampling : downsampling ; } if ( params . geometry !== undefined ) { payload . input . bounds . geometry = params . geometry ; } if ( params . preview !== undefined ) { payload . input . data [ 0 ] . dataFilter . previewMode = convertPreviewToString ( params . preview ) ; } if ( evalscript ) { payload . evalscript = evalscript ; } else if ( dataProduct ) { payload . dataProduct = { '@id' : dataProduct , } ; payload . evalscript = '' ; } else { throw new Error ( 'Either evalscript or dataProduct should be defined with Processing API' ) ; } return payload ; } export async function processingGetMap ( shServiceHostname , payload , reqConfig ) { const authToken = reqConfig && reqConfig . authToken ? reqConfig . authToken : getAuthToken ( ) ; if ( ! authToken ) { throw new Error ( 'Must be authenticated to use Processing API' ) ; } const requestConfig = { headers : { Authorization : 'Bearer ' + authToken , 'Content-Type' : 'application/json' , Accept : '*/*' , } , responseType : typeof window !== 'undefined' && window . Blob ? 'blob' : 'arraybuffer' , ... getAxiosReqParams ( reqConfig , CACHE_CONFIG_30MIN ) , } ; const response = await axios . post ( `${ shServiceHostname }api/v1/process` , payload , requestConfig ) ; return response . data ; }