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
hanbyul-here committed Sep 25, 2023
1 parent f378f37 commit dfac5f0
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ 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';
import { TimeseriesDataResult } from '../results/timeseries-data';

interface UseStacSearchProps {
start?: Date;
end?: Date;
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 @@ -53,23 +59,29 @@ export function useStacCollectionSearch({
const numberOfItems = getNumberOfItemsWithinTimeRange(
start,
end,
l.collection
l
);
return { ...l, numberOfItems };
}
);

console.log(selectableDatasetLayersWithNumberOfItems);

return {
selectableDatasetLayers: selectableDatasetLayers,
selectableDatasetLayers: selectableDatasetLayersWithNumberOfItems,
stacSearchStatus: result.status,
readyToLoadDatasets
};
}

function getNumberOfItemsWithinTimeRange(start, end, collection) {
const isPeriodic = 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) {
Expand Down Expand Up @@ -117,7 +129,7 @@ function getInTemporalAndSpatialExtent(collectionData, aoi, timeRange) {
matchingCollectionIds.includes(l.stacCol)
);

const filteredDataetsWithCollections: TimeseriesDataResult[] =
const filteredDatasetsWithCollections: TimeseriesDataResult[] =
filteredDatasets.map((l) => {
const collection = collectionData.find((c) => c.id === l.stacCol);
return {
Expand All @@ -129,5 +141,5 @@ function getInTemporalAndSpatialExtent(collectionData, aoi, timeRange) {
};
});

return filteredDataetsWithCollections;
return filteredDatasetsWithCollections;
}

0 comments on commit dfac5f0

Please sign in to comment.