diff --git a/app/scripts/components/analysis/define/use-stac-collection-search.ts b/app/scripts/components/analysis/define/use-stac-collection-search.ts index 2fed3097f..975172783 100644 --- a/app/scripts/components/analysis/define/use-stac-collection-search.ts +++ b/app/scripts/components/analysis/define/use-stac-collection-search.ts @@ -4,8 +4,9 @@ import axios from 'axios'; import { useQuery } from '@tanstack/react-query'; import booleanIntersects from '@turf/boolean-intersects'; import bboxPolygon from '@turf/bbox-polygon'; -import { areIntervalsOverlapping } from 'date-fns'; +import { areIntervalsOverlapping, eachDayOfInterval, eachMonthOfInterval, eachYearOfInterval } from 'date-fns'; +import { TimeseriesDataResult } from '../results/timeseries-data'; import { allAvailableDatasetsLayers } from '.'; import { utcString2userTzDate } from '$utils/date'; @@ -16,6 +17,12 @@ interface UseStacSearchProps { aoi?: FeatureCollection | null; } +const DATE_INTERVAL_FN = { + day: eachDayOfInterval, + month: eachMonthOfInterval, + year: eachYearOfInterval +}; + const collectionUrl = `${process.env.API_STAC_ENDPOINT}/collections`; export function useStacCollectionSearch({ @@ -47,13 +54,36 @@ export function useStacCollectionSearch({ } }, [result.data, aoi, start, end]); + const selectableDatasetLayersWithNumberOfItems = selectableDatasetLayers.map( + (l) => { + const numberOfItems = getNumberOfItemsWithinTimeRange( + start, + end, + l + ); + return { ...l, numberOfItems }; + } + ); + return { - selectableDatasetLayers: selectableDatasetLayers, + selectableDatasetLayers: selectableDatasetLayersWithNumberOfItems, stacSearchStatus: result.status, readyToLoadDatasets }; } +function getNumberOfItemsWithinTimeRange(start, end, collection) { + const {isPeriodic, timeDensity, domain, timeseries} = collection; + if (!isPeriodic) { + return timeseries.length; // Check in with back-end team + } + const eachOf = DATE_INTERVAL_FN[timeDensity]; + const statStart = +(new Date(domain[0])) > +(new Date(start))? new Date(domain[0]): new Date(start); + const statEnd = +(new Date(domain[1])) < +(new Date(end))? new Date(domain[1]): new Date(end); + + return eachOf({start: statStart, end: statEnd}).length; +} + function getInTemporalAndSpatialExtent(collectionData, aoi, timeRange) { const matchingCollectionIds = collectionData.reduce((acc, col) => { const id = col.id; @@ -95,7 +125,21 @@ function getInTemporalAndSpatialExtent(collectionData, aoi, timeRange) { } }, []); - return allAvailableDatasetsLayers.filter((l) => + const filteredDatasets = allAvailableDatasetsLayers.filter((l) => matchingCollectionIds.includes(l.stacCol) ); + + const filteredDatasetsWithCollections: TimeseriesDataResult[] = + filteredDatasets.map((l) => { + const collection = collectionData.find((c) => c.id === l.stacCol); + return { + ...l, + isPeriodic: collection['dashboard:is_periodic'], + timeDensity: collection['dashboard:time_density'], + domain: collection.extent.temporal.interval[0], + timeseries: collection.summaries.datetime + }; + }); + + return filteredDatasetsWithCollections; } diff --git a/app/scripts/components/analysis/results/timeseries-data.ts b/app/scripts/components/analysis/results/timeseries-data.ts index 8fc621d32..628c7e4b4 100644 --- a/app/scripts/components/analysis/results/timeseries-data.ts +++ b/app/scripts/components/analysis/results/timeseries-data.ts @@ -30,7 +30,7 @@ export interface TimeseriesDataUnit { percentile_98: number; } -interface TimeseriesDataResult { +export interface TimeseriesDataResult { isPeriodic: boolean; timeDensity: TimeDensity; domain: string[];