Skip to content

Commit

Permalink
Fix circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kumer committed Aug 1, 2024
1 parent 64ee8ab commit dbd239a
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 77 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ import {
} from './dataimport/const';

import { ProcessingPayload } from './layer/processing';
import { StatisticalApi } from './statistics/StatisticalApi';
import { Fis } from './statistics/Fis';

registerInitialAxiosInterceptors();

Expand Down Expand Up @@ -293,6 +295,7 @@ export {
setTPDIServiceBaseURL,
PlanetARPSType,
PlanetARPSId,
StatisticalApi,
Fis,
};

export * from './statistics/';
31 changes: 17 additions & 14 deletions src/layer/AbstractSentinelHubV1OrV2Layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ import moment from 'moment';
import { stringify } from 'query-string';

import { BBox } from '../bbox';
import { Fis } from '../statistics/Fis';
import { CACHE_CONFIG_NOCACHE } from '../utils/cacheHandlers';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { ensureTimeout } from '../utils/ensureTimeout';
import { AbstractLayer } from './AbstractLayer';
import {
GetMapParams,
ApiType,
PaginatedTiles,
DEFAULT_FIND_TILES_MAX_COUNT_PARAMETER,
GetMapParams,
GetStatsParams,
Stats,
MosaickingOrder,
Interpolator,
Link,
DEFAULT_FIND_TILES_MAX_COUNT_PARAMETER,
MosaickingOrder,
OgcServiceTypes,
PaginatedTiles,
Stats,
} from './const';
import { wmsGetMapUrl } from './wms';
import { AbstractLayer } from './AbstractLayer';
import { fetchLayersFromGetCapabilitiesXml } from './utils';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { ensureTimeout } from '../utils/ensureTimeout';
import { CACHE_CONFIG_NOCACHE } from '../utils/cacheHandlers';
import { getStatisticsProvider, StatisticsProviderType } from '../statistics/StatisticsProvider';
import { wmsGetMapUrl } from './wms';
import { StatisticsProviderType } from '../statistics';

interface ConstructorParameters {
instanceId?: string | null;
Expand Down Expand Up @@ -244,9 +245,11 @@ export class AbstractSentinelHubV1OrV2Layer extends AbstractLayer {
statsProvider: StatisticsProviderType = StatisticsProviderType.FIS,
): Promise<Stats> {
const stats = await ensureTimeout(async (innerReqConfig) => {
const sp = getStatisticsProvider(statsProvider);
const data: Stats = await sp.getStats(this, params, innerReqConfig);
return data;
if (statsProvider === StatisticsProviderType.FIS) {
return await new Fis().handleV1orV2(this, params, innerReqConfig);
} else {
throw new Error(`Unssuported statistics provider ${statsProvider}`);
}
}, reqConfig);
return stats;
}
Expand Down
42 changes: 26 additions & 16 deletions src/layer/AbstractSentinelHubV3Layer.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import { Geometry } from '@turf/helpers';
import axios, { AxiosRequestConfig } from 'axios';
import moment, { Moment } from 'moment';
import { Geometry } from '@turf/helpers';

import { getAuthToken } from '../auth';
import { BBox } from '../bbox';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { ensureTimeout } from '../utils/ensureTimeout';
import { AbstractLayer } from './AbstractLayer';
import {
GetMapParams,
ApiType,
PaginatedTiles,
MosaickingOrder,
CATALOG_SEARCH_MAX_LIMIT,
DataProductId,
DEFAULT_FIND_TILES_MAX_COUNT_PARAMETER,
FindTilesAdditionalParameters,
GetMapParams,
GetStatsParams,
Stats,
Interpolator,
Link,
LinkType,
DEFAULT_FIND_TILES_MAX_COUNT_PARAMETER,
MosaickingOrder,
PaginatedTiles,
Stats,
SUPPORTED_DATA_PRODUCTS_PROCESSING,
DataProductId,
FindTilesAdditionalParameters,
CATALOG_SEARCH_MAX_LIMIT,
} from './const';
import { createProcessingPayload, processingGetMap, ProcessingPayload } from './processing';
import { wmsGetMapUrl } from './wms';
import { processingGetMap, createProcessingPayload, ProcessingPayload } from './processing';
import { AbstractLayer } from './AbstractLayer';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { ensureTimeout } from '../utils/ensureTimeout';

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

interface ConstructorParameters {
instanceId?: string | null;
layerId?: string | null;
Expand Down Expand Up @@ -645,10 +648,17 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer {
statsProvider: StatisticsProviderType = StatisticsProviderType.FIS,
): Promise<Stats> {
const stats = await ensureTimeout(async (innerReqConfig) => {
const sp = getStatisticsProvider(statsProvider);
const data: Stats = await sp.getStats(this, params, innerReqConfig);
let data: Stats;
if (statsProvider === StatisticsProviderType.FIS) {
data = await new Fis().handleV3(this, params, innerReqConfig);
} else if (statsProvider === StatisticsProviderType.STAPI) {
data = await new StatisticalApi().getStats(this, params, innerReqConfig);
} else {
throw new Error(`Unssuported statistics provider ${statsProvider}`);
}
return data;
}, reqConfig);

return stats;
}

Expand Down
2 changes: 1 addition & 1 deletion src/layer/BYOCLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import { ProcessingPayload } from './processing';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { ensureTimeout } from '../utils/ensureTimeout';
import { CACHE_CONFIG_30MIN } from '../utils/cacheHandlers';
import { StatisticsProviderType } from '../statistics/StatisticsProvider';
import { getSHServiceRootUrl, metersPerPixel } from './utils';
import { StatisticsProviderType } from '../statistics/const';

interface ConstructorParameters {
instanceId?: string | null;
Expand Down
4 changes: 4 additions & 0 deletions src/layer/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,7 @@ export const PLANET_FALSE_COLOR_TEMPLATES = [
{ description: '', titleSuffix: 'TGI', resourceUrlParams: { proc: 'tgi' } },
{ description: '', titleSuffix: 'CIR', resourceUrlParams: { proc: 'cir' } },
];

export const EQUATOR_RADIUS = 6378137.0;
export const DEGREE_TO_RADIAN = Math.PI / 180;
export const RADIAN_TO_DEGREE = 180 / Math.PI;
5 changes: 2 additions & 3 deletions src/layer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import axios, { AxiosRequestConfig } from 'axios';
import { stringify, parseUrl, stringifyUrl } from 'query-string';
import { parseStringPromise } from 'xml2js';

import { OgcServiceTypes, SH_SERVICE_HOSTNAMES_V3, SH_SERVICE_ROOT_URL } from './const';
import { EQUATOR_RADIUS, OgcServiceTypes, SH_SERVICE_HOSTNAMES_V3, SH_SERVICE_ROOT_URL } from './const';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import { CACHE_CONFIG_30MIN, CACHE_CONFIG_30MIN_MEMORY } from '../utils/cacheHandlers';
import { EQUATOR_RADIUS, GetCapabilitiesWmtsXml } from './wmts.utils';
import type { GetCapabilitiesWmtsXml } from './wmts.utils';
import { getAuthToken } from '../auth';
import { BBox } from '../bbox';
import { CRS_EPSG3857 } from '../crs';
Expand Down
6 changes: 1 addition & 5 deletions src/layer/wmts.utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { RequestConfiguration } from '..';
import { BBox } from '../bbox';
import { CRS_EPSG3857, CRS_EPSG4326 } from '../crs';
import { OgcServiceTypes } from './const';
import { DEGREE_TO_RADIAN, EQUATOR_RADIUS, OgcServiceTypes, RADIAN_TO_DEGREE } from './const';
import { fetchGetCapabilitiesXml, GetCapabilitiesXmlLayer } from './utils';

const DEGREE_TO_RADIAN = Math.PI / 180;
const RADIAN_TO_DEGREE = 180 / Math.PI;
export const EQUATOR_RADIUS = 6378137.0;

export type GetCapabilitiesWmtsXml = {
Capabilities: {
'ows:ServiceIdentification': any[][];
Expand Down
30 changes: 9 additions & 21 deletions src/statistics/Fis.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import axios, { AxiosRequestConfig } from 'axios';
import moment from 'moment';
import WKT from 'terraformer-wkt-parser';
import { RequestConfiguration } from '../utils/cancelRequests';
import { CRS_EPSG4326, CRS_WGS84, findCrsFromUrn } from '../crs';
import { AbstractLayer } from '../layer/AbstractLayer';
import type { AbstractLayer } from '../layer/AbstractLayer';
import { AbstractSentinelHubV1OrV2Layer } from '../layer/AbstractSentinelHubV1OrV2Layer';
import { AbstractSentinelHubV3Layer } from '../layer/AbstractSentinelHubV3Layer';
import type { AbstractSentinelHubV3Layer } from '../layer/AbstractSentinelHubV3Layer';
import { FisPayload, FisResponse, GetStatsParams, HistogramType } from '../layer/const';
import { CACHE_CONFIG_NOCACHE } from '../utils/cacheHandlers';
import { getAxiosReqParams } from '../utils/cancelRequests';
import { StatisticsProvider } from './StatisticsProvider';
import { StatisticalApiResponse } from './const';
import { getAxiosReqParams, RequestConfiguration } from '../utils/cancelRequests';
import type { StatisticsProvider } from './StatisticsProvider';

export class Fis implements StatisticsProvider {
public getStatistics(): Promise<StatisticalApiResponse> {
throw new Error('Method not implemented.');
}

private createFISPayload(
layer: AbstractSentinelHubV3Layer | AbstractSentinelHubV1OrV2Layer,
params: GetStatsParams,
Expand Down Expand Up @@ -63,7 +57,7 @@ export class Fis implements StatisticsProvider {
} else {
payload.evalscript = Buffer.from(layer.getEvalscript(), 'utf8').toString('base64');
}
if (layer instanceof AbstractSentinelHubV1OrV2Layer) {
if ('getEvalsource' in layer && layer.getEvalsource instanceof Function) {
payload.evalsource = layer.getEvalsource();
}
}
Expand All @@ -83,19 +77,13 @@ export class Fis implements StatisticsProvider {

public async getStats(
layer: AbstractLayer,
params: GetStatsParams,
_params: GetStatsParams,
reqConfig?: RequestConfiguration,
): Promise<FisResponse> {
if (layer instanceof AbstractSentinelHubV3Layer) {
return this.handleV3(layer, params, reqConfig);
} else if (layer instanceof AbstractSentinelHubV1OrV2Layer) {
return this.handleV1orV2(layer, params, reqConfig);
} else {
throw new Error('Not supported');
}
return layer.getStats(layer, reqConfig);
}

private async handleV3(
public async handleV3(
layer: AbstractSentinelHubV3Layer,
params: GetStatsParams,
reqConfig?: RequestConfiguration,
Expand All @@ -115,7 +103,7 @@ export class Fis implements StatisticsProvider {
return this.convertFISResponse(data);
}

private async handleV1orV2(
public async handleV1orV2(
layer: AbstractSentinelHubV1OrV2Layer,
params: GetStatsParams,
reqConfig?: RequestConfiguration,
Expand Down
7 changes: 3 additions & 4 deletions src/statistics/StatisticalApi.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import axios, { AxiosRequestConfig } from 'axios';
import { RequestConfiguration } from '..';
import { getAuthToken } from '../auth';
import { AbstractSentinelHubV3Layer } from '../layer/AbstractSentinelHubV3Layer';
import { GetStatsParams } from '../layer/const';
import { CACHE_CONFIG_30MIN } from '../utils/cacheHandlers';
import { getAxiosReqParams } from '../utils/cancelRequests';
import { StatisticsProvider } from './StatisticsProvider';
import { AbstractSentinelHubV3Layer } from '../layer/AbstractSentinelHubV3Layer';
import { StatisticsUtils } from './statistics.utils';

import { StatisticalApiResponse, StatisticalApiPayload } from './const';
import { StatisticsProvider } from './StatisticsProvider';
import { StatisticalApiPayload, StatisticalApiResponse } from './const';

const STATS_DEFAULT_OUTPUT = 'default';

Expand Down
12 changes: 1 addition & 11 deletions src/statistics/StatisticsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { AbstractLayer } from '../layer/AbstractLayer';
import { GetStatsParams, Stats } from '../layer/const';
import { RequestConfiguration } from '../utils/cancelRequests';
import { StatisticalApiPayload, StatisticalApiResponse } from './const';
import { StatisticsProviderType } from './const';
import { Fis } from './Fis';
import { StatisticalApi } from './StatisticalApi';

export enum StatisticsProviderType {
FIS = 'FIS',
STAPI = 'STAPI',
}

export interface StatisticsProvider {
getStats(layer: AbstractLayer, params: GetStatsParams, reqConfig?: RequestConfiguration): Promise<Stats>;
getStatistics(
shServiceHostname: string,
payload: StatisticalApiPayload,
reqConfig?: RequestConfiguration,
): Promise<StatisticalApiResponse>;
}

export function getStatisticsProvider(statsProvider: StatisticsProviderType): StatisticsProvider {
Expand Down
5 changes: 5 additions & 0 deletions src/statistics/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ export type StatisticalApiResponse = {

error?: StatisticalApiResponseError;
}[];

export enum StatisticsProviderType {
FIS = 'FIS',
STAPI = 'STAPI',
}
3 changes: 2 additions & 1 deletion src/statistics/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getStatisticsProvider } from './StatisticsProvider';
export {
BandHistogram,
BandStats,
Expand All @@ -7,6 +8,6 @@ export {
StatisticalApiOutput,
StatisticalApiPayload,
StatisticalApiResponse,
StatisticsProviderType,
} from './const';
export { StatisticsProviderType, getStatisticsProvider } from './StatisticsProvider';
export { StatisticsUtils } from './statistics.utils';

0 comments on commit dbd239a

Please sign in to comment.