Skip to content

Commit

Permalink
Merge branch 'main' of github.com:NASA-IMPACT/veda-ui into feature/li…
Browse files Browse the repository at this point in the history
…mit-stats
  • Loading branch information
nerik committed Sep 25, 2023
2 parents 92f0574 + 77f3946 commit db3fd08
Show file tree
Hide file tree
Showing 18 changed files with 520 additions and 339 deletions.
14 changes: 10 additions & 4 deletions app/scripts/components/analysis/define/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { datasets, DatasetLayer, VedaDatum, DatasetData } from 'veda';
import { useAnalysisParams } from '../results/use-analysis-params';
import AoiSelector from './aoi-selector';
import PageHeroActions from './page-hero-actions';
import { useStacSearch } from './use-stac-search';
import { useStacCollectionSearch } from './use-stac-collection-search';
import { variableGlsp } from '$styles/variable-utils';

import { PageMainContent } from '$styles/page';
Expand Down Expand Up @@ -114,7 +114,7 @@ export const allAvailableDatasetsLayers: DatasetLayer[] = Object.values(
)
.map((dataset) => (dataset as VedaDatum<DatasetData>).data.layers)
.flat()
.filter((d) => d.type !== 'vector');
.filter((d) => d.type !== 'vector' && !d.analysis?.exclude);

export default function Analysis() {
const { params, setAnalysisParam } = useAnalysisParams();
Expand Down Expand Up @@ -184,7 +184,11 @@ export default function Analysis() {
);

const { selectableDatasetLayers, stacSearchStatus, readyToLoadDatasets } =
useStacSearch({ start, end, aoi: aoiDrawState.featureCollection });
useStacCollectionSearch({
start,
end,
aoi: aoiDrawState.featureCollection
});

// Update datasetsLayers when stac search is refreshed in case some
// datasetsLayers are not available anymore
Expand Down Expand Up @@ -367,7 +371,9 @@ export default function Analysis() {
false
}
>
<Overline>From: {findParentDataset(datasetLayer.id)?.name}</Overline>
<Overline>
From: {findParentDataset(datasetLayer.id)?.name}
</Overline>
{datasetLayer.name}
</FormCheckableCustom>
))}
Expand Down
101 changes: 101 additions & 0 deletions app/scripts/components/analysis/define/use-stac-collection-search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { useMemo } from 'react';
import { FeatureCollection, Polygon } from 'geojson';
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 { allAvailableDatasetsLayers } from '.';

import { utcString2userTzDate } from '$utils/date';

interface UseStacSearchProps {
start?: Date;
end?: Date;
aoi?: FeatureCollection<Polygon> | null;
}

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

export function useStacCollectionSearch({
start,
end,
aoi
}: UseStacSearchProps) {
const readyToLoadDatasets = !!(start && end && aoi);

const result = useQuery({
queryKey: ['stacCollection'],
queryFn: async ({ signal }) => {
const collectionResponse = await axios.get(collectionUrl, {
signal
});
return collectionResponse.data.collections;
},
enabled: readyToLoadDatasets
});

const selectableDatasetLayers = useMemo(() => {
try {
return getInTemporalAndSpatialExtent(result.data, aoi, {
start,
end
});
} catch (e) {
return [];
}
}, [result.data, aoi, start, end]);

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

function getInTemporalAndSpatialExtent(collectionData, aoi, timeRange) {
const matchingCollectionIds = collectionData.reduce((acc, col) => {
const id = col.id;

// Is is a dataset defined in the app?
// If not, skip other calculations.
const isAppDataset = allAvailableDatasetsLayers.some(
(l) => l.stacCol === id
);

if (
!isAppDataset ||
!col.extent.spatial.bbox ||
!col.extent.temporal.interval
) {
return acc;
}

const bbox = col.extent.spatial.bbox[0];
const start = utcString2userTzDate(col.extent.temporal.interval[0][0]);
const end = utcString2userTzDate(col.extent.temporal.interval[0][1]);

const isInAOI = aoi.features.some((feature) =>
booleanIntersects(feature, bboxPolygon(bbox))
);

const isInTOI = areIntervalsOverlapping(
{ start: new Date(timeRange.start), end: new Date(timeRange.end) },
{
start: new Date(start),
end: new Date(end)
}
);

if (isInAOI && isInTOI) {
return [...acc, id];
} else {
return acc;
}
}, []);

return allAvailableDatasetsLayers.filter((l) =>
matchingCollectionIds.includes(l.stacCol)
);
}
94 changes: 0 additions & 94 deletions app/scripts/components/analysis/define/use-stac-search.ts

This file was deleted.

Loading

0 comments on commit db3fd08

Please sign in to comment.