Skip to content

Commit

Permalink
Return the guessed number of items based on collection metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nerik committed Sep 25, 2023
1 parent db3fd08 commit 441f059
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,6 +17,12 @@ interface UseStacSearchProps {
aoi?: FeatureCollection<Polygon> | null;
}

const DATE_INTERVAL_FN = {
day: eachDayOfInterval,
month: eachMonthOfInterval,
year: eachYearOfInterval
};

const collectionUrl = `${process.env.API_STAC_ENDPOINT}/collections`;

export function useStacCollectionSearch({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion app/scripts/components/analysis/results/timeseries-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface TimeseriesDataUnit {
percentile_98: number;
}

interface TimeseriesDataResult {
export interface TimeseriesDataResult {
isPeriodic: boolean;
timeDensity: TimeDensity;
domain: string[];
Expand Down

0 comments on commit 441f059

Please sign in to comment.