From ac5fcb613e745e7652cd0ade5f7982a61cbd2d42 Mon Sep 17 00:00:00 2001 From: Anze Skerlavaj Date: Fri, 28 Feb 2020 16:04:30 +0100 Subject: [PATCH] Use Moment.js for all findTiles/findFlyovers times --- package-lock.json | 5 +++++ package.json | 1 + src/layer/AbstractLayer.ts | 19 ++++++++++--------- src/layer/AbstractSentinelHubV1OrV2Layer.ts | 7 ++++--- src/layer/AbstractSentinelHubV3Layer.ts | 11 ++++++----- src/layer/AbstractSentinelHubV3WithCCLayer.ts | 8 +++++--- src/layer/BYOCLayer.ts | 8 +++++--- src/layer/S1GRDAWSEULayer.ts | 8 +++++--- src/layer/S3OLCILayer.ts | 8 +++++--- src/layer/S3SLSTRLayer.ts | 7 ++++--- src/layer/S5PL2Layer.ts | 7 ++++--- src/layer/const.ts | 10 ++++++---- 12 files changed, 60 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index d560b80c..a9507172 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10534,6 +10534,11 @@ } } }, + "moment": { + "version": "2.24.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", + "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index 11d56546..640f941d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "@turf/helpers": "^6.1.4", "@types/xml2js": "^0.4.4", "axios": "^0.18.1", + "moment": "^2.24.0", "polygon-clipping": "^0.14.3", "query-string": "^6.4.2", "xml2js": "^0.4.19" diff --git a/src/layer/AbstractLayer.ts b/src/layer/AbstractLayer.ts index 92615ac0..f3c37751 100644 --- a/src/layer/AbstractLayer.ts +++ b/src/layer/AbstractLayer.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import { Moment } from 'moment'; import { GetMapParams, ApiType, PaginatedTiles, FlyoverInterval } from 'src/layer/const'; import { BBox } from 'src/bbox'; @@ -40,8 +41,8 @@ export class AbstractLayer { public async findTiles( bbox: BBox, // eslint-disable-line @typescript-eslint/no-unused-vars - fromTime: Date, // eslint-disable-line @typescript-eslint/no-unused-vars - toTime: Date, // eslint-disable-line @typescript-eslint/no-unused-vars + fromTime: Moment, // eslint-disable-line @typescript-eslint/no-unused-vars + toTime: Moment, // eslint-disable-line @typescript-eslint/no-unused-vars maxCount: number = 50, // eslint-disable-line @typescript-eslint/no-unused-vars offset: number = 0, // eslint-disable-line @typescript-eslint/no-unused-vars ): Promise { @@ -50,8 +51,8 @@ export class AbstractLayer { public async findFlyovers( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxFindTilesRequests: number = 50, tilesPerRequest: number = 50, ): Promise { @@ -62,7 +63,7 @@ export class AbstractLayer { throw new Error('Currently, only EPSG:4326 in findFlyovers'); } - const orbitTimeMS = this.dataset.orbitTimeMinutes * 60 * 1000; + const orbitTimeS = this.dataset.orbitTimeMinutes * 60; const bboxGeometry: Geom = this.roundCoordinates([ [ [bbox.minX, bbox.minY], @@ -109,10 +110,10 @@ export class AbstractLayer { } // append the tile to flyovers: - const prevDateMS = flyovers[flyoverIndex].fromTime.getTime(); - const currDateMS = tiles[tileIndex].sensingTime.getTime(); - const diffMS = Math.abs(prevDateMS - currDateMS); - if (diffMS > orbitTimeMS) { + const prevDateS = flyovers[flyoverIndex].fromTime.unix(); + const currDateS = tiles[tileIndex].sensingTime.unix(); + const diffS = Math.abs(prevDateS - currDateS); + if (diffS > orbitTimeS) { // finish the old flyover: try { flyovers[flyoverIndex].coveragePercent = this.calculateCoveragePercent( diff --git a/src/layer/AbstractSentinelHubV1OrV2Layer.ts b/src/layer/AbstractSentinelHubV1OrV2Layer.ts index 1bfe6e6c..79296a59 100644 --- a/src/layer/AbstractSentinelHubV1OrV2Layer.ts +++ b/src/layer/AbstractSentinelHubV1OrV2Layer.ts @@ -1,5 +1,6 @@ import axios from 'axios'; import { stringify } from 'query-string'; +import moment, { Moment } from 'moment'; import { BBox } from 'src/bbox'; import { GetMapParams, ApiType, PaginatedTiles } from 'src/layer/const'; @@ -68,8 +69,8 @@ export class AbstractSentinelHubV1OrV2Layer extends AbstractLayer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount: number = 50, offset: number = 0, ): Promise { @@ -98,7 +99,7 @@ export class AbstractSentinelHubV1OrV2Layer extends AbstractLayer { return { tiles: responseTiles.map(tile => ({ geometry: tile.tileDrawRegionGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: this.extractFindTilesMeta(tile), })), hasMore: response.data.hasMore, diff --git a/src/layer/AbstractSentinelHubV3Layer.ts b/src/layer/AbstractSentinelHubV3Layer.ts index 7b689e71..ed924c73 100644 --- a/src/layer/AbstractSentinelHubV3Layer.ts +++ b/src/layer/AbstractSentinelHubV3Layer.ts @@ -1,4 +1,5 @@ import axios, { AxiosRequestConfig } from 'axios'; +import moment, { Moment } from 'moment'; import { getAuthToken, isAuthTokenSet } from 'src/auth'; import { BBox } from 'src/bbox'; @@ -150,8 +151,8 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -159,7 +160,7 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer { return { tiles: response.data.tiles.map(tile => ({ geometry: tile.dataGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: {}, })), hasMore: response.data.hasMore, @@ -168,8 +169,8 @@ export class AbstractSentinelHubV3Layer extends AbstractLayer { protected fetchTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount: number = 1, offset: number = 0, maxCloudCoverPercent?: number | null, diff --git a/src/layer/AbstractSentinelHubV3WithCCLayer.ts b/src/layer/AbstractSentinelHubV3WithCCLayer.ts index d51b49bc..37ceca0a 100644 --- a/src/layer/AbstractSentinelHubV3WithCCLayer.ts +++ b/src/layer/AbstractSentinelHubV3WithCCLayer.ts @@ -1,3 +1,5 @@ +import moment, { Moment } from 'moment'; + import { BBox } from 'src/bbox'; import { PaginatedTiles } from 'src/layer/const'; import { AbstractSentinelHubV3Layer } from './AbstractSentinelHubV3Layer'; @@ -22,8 +24,8 @@ export class AbstractSentinelHubV3WithCCLayer extends AbstractSentinelHubV3Layer public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -38,7 +40,7 @@ export class AbstractSentinelHubV3WithCCLayer extends AbstractSentinelHubV3Layer return { tiles: response.data.tiles.map(tile => ({ geometry: tile.dataGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: { cloudCoverPercent: tile.cloudCoverPercentage, }, diff --git a/src/layer/BYOCLayer.ts b/src/layer/BYOCLayer.ts index 13b7797f..bd935183 100644 --- a/src/layer/BYOCLayer.ts +++ b/src/layer/BYOCLayer.ts @@ -1,4 +1,6 @@ import { AxiosRequestConfig } from 'axios'; +import moment, { Moment } from 'moment'; + import { BBox } from 'src/bbox'; import { PaginatedTiles } from 'src/layer/const'; import { DATASET_BYOC } from 'src/layer/dataset'; @@ -51,8 +53,8 @@ export class BYOCLayer extends AbstractSentinelHubV3Layer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -78,7 +80,7 @@ export class BYOCLayer extends AbstractSentinelHubV3Layer { tiles: response.data.tiles.map(tile => { return { geometry: tile.dataGeometry, - sensingTime: tile.sensingTime, + sensingTime: moment.utc(tile.sensingTime), meta: { cloudCoverPercent: tile.cloudCoverPercentage, }, diff --git a/src/layer/S1GRDAWSEULayer.ts b/src/layer/S1GRDAWSEULayer.ts index 65941094..2af0e014 100644 --- a/src/layer/S1GRDAWSEULayer.ts +++ b/src/layer/S1GRDAWSEULayer.ts @@ -1,3 +1,5 @@ +import moment, { Moment } from 'moment'; + import { BBox } from 'src/bbox'; import { BackscatterCoeff, PaginatedTiles } from 'src/layer/const'; import { ProcessingPayload } from 'src/layer/processing'; @@ -108,8 +110,8 @@ export class S1GRDAWSEULayer extends AbstractSentinelHubV3Layer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -135,7 +137,7 @@ export class S1GRDAWSEULayer extends AbstractSentinelHubV3Layer { return { tiles: response.data.tiles.map(tile => ({ geometry: tile.dataGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: { orbitDirection: tile.orbitDirection, polarization: tile.polarization, diff --git a/src/layer/S3OLCILayer.ts b/src/layer/S3OLCILayer.ts index 12bedfa2..403610dc 100644 --- a/src/layer/S3OLCILayer.ts +++ b/src/layer/S3OLCILayer.ts @@ -1,3 +1,5 @@ +import moment, { Moment } from 'moment'; + import { BBox } from 'src/bbox'; import { PaginatedTiles } from 'src/layer/const'; import { DATASET_S3OLCI } from 'src/layer/dataset'; @@ -8,8 +10,8 @@ export class S3OLCILayer extends AbstractSentinelHubV3Layer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -17,7 +19,7 @@ export class S3OLCILayer extends AbstractSentinelHubV3Layer { return { tiles: response.data.tiles.map(tile => ({ geometry: tile.dataGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: {}, })), hasMore: response.data.hasMore, diff --git a/src/layer/S3SLSTRLayer.ts b/src/layer/S3SLSTRLayer.ts index 30b5d5ce..d1134694 100644 --- a/src/layer/S3SLSTRLayer.ts +++ b/src/layer/S3SLSTRLayer.ts @@ -4,6 +4,7 @@ import { DATASET_S3SLSTR } from 'src/layer/dataset'; import { AbstractSentinelHubV3Layer } from 'src/layer/AbstractSentinelHubV3Layer'; import { OrbitDirection } from 'src'; import { ProcessingPayload } from 'src/layer/processing'; +import moment, { Moment } from 'moment'; type S3SLSTRFindTilesDatasetParameters = { type?: string; @@ -48,8 +49,8 @@ export class S3SLSTRLayer extends AbstractSentinelHubV3Layer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -70,7 +71,7 @@ export class S3SLSTRLayer extends AbstractSentinelHubV3Layer { return { tiles: response.data.tiles.map(tile => ({ geometry: tile.dataGeometry, - sensingTime: new Date(tile.sensingTime), + sensingTime: moment.utc(tile.sensingTime), meta: { cloudCoverPercent: tile.cloudCoverPercentage, orbitDirection: tile.orbitDirection, diff --git a/src/layer/S5PL2Layer.ts b/src/layer/S5PL2Layer.ts index af5dcd06..2bdccf76 100644 --- a/src/layer/S5PL2Layer.ts +++ b/src/layer/S5PL2Layer.ts @@ -3,6 +3,7 @@ import { PaginatedTiles } from 'src/layer/const'; import { DATASET_S5PL2 } from 'src/layer/dataset'; import { AbstractSentinelHubV3Layer } from 'src/layer/AbstractSentinelHubV3Layer'; import { ProcessingPayload } from 'src/layer/processing'; +import moment, { Moment } from 'moment'; /* S-5P is a bit special in that we need to supply productType when searching @@ -63,8 +64,8 @@ export class S5PL2Layer extends AbstractSentinelHubV3Layer { public async findTiles( bbox: BBox, - fromTime: Date, - toTime: Date, + fromTime: Moment, + toTime: Moment, maxCount?: number, offset?: number, ): Promise { @@ -89,7 +90,7 @@ export class S5PL2Layer extends AbstractSentinelHubV3Layer { tiles: response.data.tiles.map(tile => { return { geometry: tile.tileDrawRegionGeometry, - sensingTime: tile.sensingTime, + sensingTime: moment.utc(tile.sensingTime), meta: {}, }; }), diff --git a/src/layer/const.ts b/src/layer/const.ts index 2de937f9..a849f403 100644 --- a/src/layer/const.ts +++ b/src/layer/const.ts @@ -1,6 +1,8 @@ -import { BBox } from 'src/bbox'; +import { Moment } from 'moment'; import { Polygon, MultiPolygon } from '@turf/helpers'; +import { BBox } from 'src/bbox'; + export type GetMapParams = { bbox: BBox; fromTime: Date; @@ -50,7 +52,7 @@ export enum BackscatterCoeff { export type Tile = { geometry: Polygon | MultiPolygon; - sensingTime: Date; + sensingTime: Moment; meta: Record; }; @@ -60,8 +62,8 @@ export type PaginatedTiles = { }; export type FlyoverInterval = { - fromTime: Date; - toTime: Date; + fromTime: Moment; + toTime: Moment; coveragePercent: number; meta: Record; };