Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change AbstractSentinelHubV3Layer to receive evalscript value from dataProduct endpoint #278

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/layer/AbstractSentinelHubV3Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import { wmsGetMapUrl } from './wms';

import { Effects } from '../mapDataManipulation/const';
import { runEffectFunctions } from '../mapDataManipulation/runEffectFunctions';
import { StatisticsProviderType } from '../statistics/const';
import { Fis } from '../statistics/Fis';
import { StatisticalApi } from '../statistics/StatisticalApi';
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_NOCACHE } from '../utils/cacheHandlers';
import { fetchLayerParamsFromConfigurationService, getSHServiceRootUrl } from './utils';
import { StatisticsProviderType } from '../statistics/const';
import { fetchDataProduct, fetchLayerParamsFromConfigurationService, getSHServiceRootUrl } from './utils';

interface ConstructorParameters {
instanceId?: string | null;
Expand Down Expand Up @@ -127,6 +127,19 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer {
if (!layerParams) {
throw new Error('Layer params could not be found');
}

if (
!layerParams['evalscript'] &&
layerParams['dataProduct'] &&
!SUPPORTED_DATA_PRODUCTS_PROCESSING.includes(layerParams['dataProduct'])
) {
const response = await fetchDataProduct(layerParams['dataProduct'], reqConfig);
const evalScript = response?.data?.evalScript;
if (evalScript) {
layerParams['evalscript'] = evalScript;
}
}

return layerParams;
}

Expand Down Expand Up @@ -236,10 +249,10 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer {
}

public supportsApiType(api: ApiType): boolean {
if (this.dataProduct && !SUPPORTED_DATA_PRODUCTS_PROCESSING.includes(this.dataProduct)) {
return api === ApiType.WMS;
if (this.dataProduct && SUPPORTED_DATA_PRODUCTS_PROCESSING.includes(this.dataProduct)) {
return api === ApiType.PROCESSING;
}
return api === ApiType.WMS || (api === ApiType.PROCESSING && !!this.dataset);
return api === ApiType.WMS || (api === ApiType.PROCESSING && !!this.dataset && !!this.evalscript);
}

protected getWmsGetMapUrlAdditionalParameters(): Record<string, any> {
Expand Down Expand Up @@ -679,7 +692,7 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer {
if (!this.downsampling && layerParams.downsampling) {
this.downsampling = layerParams.downsampling;
}
// this is a hotfix for `supportsApiType()` not having enough information - should be fixed properly later:

this.dataProduct = layerParams['dataProduct'] ? layerParams['dataProduct'] : null;
}, reqConfig);
}
Expand Down
28 changes: 28 additions & 0 deletions src/layer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ export async function fetchLayerParamsFromConfigurationService(
return layersParams;
}

export async function fetchDataProduct(url: string, reqConfig: RequestConfiguration): Promise<any> {
const authToken = reqConfig && reqConfig.authToken ? reqConfig.authToken : getAuthToken();
if (!authToken) {
throw new Error('Must be authenticated to fetch layer params');
}
const headers = {
Authorization: `Bearer ${authToken}`,
};

const requestConfig: AxiosRequestConfig = {
responseType: 'json',
headers: headers,
...getAxiosReqParams(
{
...reqConfig,
// Do not override cache if cache is disabled with `expiresIn: 0`
cache:
reqConfig && reqConfig.cache && reqConfig.cache.expiresIn === 0
? reqConfig.cache
: CACHE_CONFIG_30MIN_MEMORY,
},
null,
),
};
const res = await axios.get(url, requestConfig);
return res;
}

export function ensureMercatorBBox(bbox: BBox): BBox {
if (bbox.crs.authId === CRS_EPSG3857.authId) {
return bbox;
Expand Down