Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use collections endpoint instead of search for analysis #666

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 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 { 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 @@ -102,7 +102,7 @@

const findParentDataset = (layerId: string) => {
const parentDataset = Object.values(datasets).find((dataset) =>
(dataset as VedaDatum<DatasetData>).data.layers.find(

Check warning on line 105 in app/scripts/components/analysis/define/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Use a ! assertion to more succinctly remove null and undefined from the type
(l) => l.id === layerId
)
);
Expand All @@ -112,7 +112,7 @@
export const allAvailableDatasetsLayers: DatasetLayer[] = Object.values(
datasets
)
.map((dataset) => (dataset as VedaDatum<DatasetData>).data.layers)

Check warning on line 115 in app/scripts/components/analysis/define/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Use a ! assertion to more succinctly remove null and undefined from the type
.flat()
.filter((d) => d.type !== 'vector');

Expand Down Expand Up @@ -167,7 +167,7 @@
const onDatasetLayerChange = useCallback(
(e) => {
const id = e.target.id;
let newDatasetsLayers = [...(datasetsLayers || [])];

Check warning on line 170 in app/scripts/components/analysis/define/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
if (e.target.checked) {
const newDatasetLayer = allAvailableDatasetsLayers.find(
(l) => l.id === id
Expand All @@ -184,7 +184,11 @@
);

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 All @@ -193,14 +197,14 @@
const selectableDatasetLayersIds = selectableDatasetLayers.map(
(layer) => layer.id
);
const cleanedDatasetsLayers = datasetsLayers?.filter((l) =>

Check warning on line 200 in app/scripts/components/analysis/define/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
selectableDatasetLayersIds.includes(l.id)
);

setAnalysisParam('datasetsLayers', cleanedDatasetsLayers);
// Only update when stac search gets updated to avoid triggering an infinite
// read/set state loop
}, [selectableDatasetLayers, setAnalysisParam]);

Check warning on line 207 in app/scripts/components/analysis/define/index.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'datasetsLayers'. Either include it or remove the dependency array

const showTip = !readyToLoadDatasets || !datasetsLayers?.length;

Expand Down Expand Up @@ -367,7 +371,9 @@
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.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
"@tippyjs/react": "^4.2.6",
"@turf/area": "^6.5.0",
"@turf/bbox": "^6.5.0",
"@turf/bbox-polygon": "^6.5.0",
"@turf/boolean-intersects": "^6.5.0",
"@turf/centroid": "^6.5.0",
"@turf/simplify": "^6.5.0",
"@turf/union": "^6.5.0",
Expand Down
92 changes: 89 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3058,14 +3058,49 @@
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/bbox@^6.5.0":
"@turf/bbox-polygon@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fbbox-polygon/-/bbox-polygon-6.5.0.tgz#f18128b012eedfa860a521d8f2b3779cc0801032"
integrity sha512-+/r0NyL1lOG3zKZmmf6L8ommU07HliP4dgYToMoTxqzsWzyLjaj/OzgQ8rBmv703WJX+aS6yCmLuIhYqyufyuw==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/bbox@*", "@turf/bbox@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fbbox/-/bbox-6.5.0.tgz#bec30a744019eae420dac9ea46fb75caa44d8dc5"
integrity sha512-RBbLaao5hXTYyyg577iuMtDB8ehxMlUqHEJiMs8jT1GHkFhr6sYre3lmLsPeYEi/ZKj5TP5tt7fkzNdJ4GIVyw==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/boolean-disjoint@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fboolean-disjoint/-/boolean-disjoint-6.5.0.tgz#e291d8f8f8cce7f7bb3c11e23059156a49afc5e4"
integrity sha512-rZ2ozlrRLIAGo2bjQ/ZUu4oZ/+ZjGvLkN5CKXSKBcu6xFO6k2bgqeM8a1836tAW+Pqp/ZFsTA5fZHsJZvP2D5g==
dependencies:
"@turf/boolean-point-in-polygon" "^6.5.0"
"@turf/helpers" "^6.5.0"
"@turf/line-intersect" "^6.5.0"
"@turf/meta" "^6.5.0"
"@turf/polygon-to-line" "^6.5.0"

"@turf/boolean-intersects@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fboolean-intersects/-/boolean-intersects-6.5.0.tgz#df2b831ea31a4574af6b2fefe391f097a926b9d6"
integrity sha512-nIxkizjRdjKCYFQMnml6cjPsDOBCThrt+nkqtSEcxkKMhAQj5OO7o2CecioNTaX8EayqwMGVKcsz27oP4mKPTw==
dependencies:
"@turf/boolean-disjoint" "^6.5.0"
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/boolean-point-in-polygon@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fboolean-point-in-polygon/-/boolean-point-in-polygon-6.5.0.tgz#6d2e9c89de4cd2e4365004c1e51490b7795a63cf"
integrity sha512-DtSuVFB26SI+hj0SjrvXowGTUCHlgevPAIsukssW6BG5MlNSBQAo70wpICBNJL6RjukXg8d2eXaAWuD/CqL00A==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"

"@turf/centroid@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fcentroid/-/centroid-6.5.0.tgz#ecaa365412e5a4d595bb448e7dcdacfb49eb0009"
Expand All @@ -3089,7 +3124,7 @@
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/helpers@^6.5.0":
"@turf/helpers@6.x", "@turf/helpers@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fhelpers/-/helpers-6.5.0.tgz#f79af094bd6b8ce7ed2bd3e089a8493ee6cae82e"
integrity sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==
Expand All @@ -3101,13 +3136,41 @@
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/meta@^6.5.0":
"@turf/line-intersect@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fline-intersect/-/line-intersect-6.5.0.tgz#dea48348b30c093715d2195d2dd7524aee4cf020"
integrity sha512-CS6R1tZvVQD390G9Ea4pmpM6mJGPWoL82jD46y0q1KSor9s6HupMIo1kY4Ny+AEYQl9jd21V3Scz20eldpbTVA==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"
"@turf/line-segment" "^6.5.0"
"@turf/meta" "^6.5.0"
geojson-rbush "3.x"

"@turf/line-segment@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fline-segment/-/line-segment-6.5.0.tgz#ee73f3ffcb7c956203b64ed966d96af380a4dd65"
integrity sha512-jI625Ho4jSuJESNq66Mmi290ZJ5pPZiQZruPVpmHkUw257Pew0alMmb6YrqYNnLUuiVVONxAAKXUVeeUGtycfw==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/[email protected]", "@turf/meta@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fmeta/-/meta-6.5.0.tgz#b725c3653c9f432133eaa04d3421f7e51e0418ca"
integrity sha512-RrArvtsV0vdsCBegoBtOalgdSOfkBrTJ07VkpiCnq/491W67hnMWmDu7e6Ztw0C3WldRYTXkg3SumfdzZxLBHA==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/polygon-to-line@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fpolygon-to-line/-/polygon-to-line-6.5.0.tgz#4dc86db66168b32bb83ce448cf966208a447d952"
integrity sha512-5p4n/ij97EIttAq+ewSnKt0ruvuM+LIDzuczSzuHTpq4oS7Oq8yqg5TQ4nzMVuK41r/tALCk7nAoBuw3Su4Gcw==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"

"@turf/simplify@^6.5.0":
version "6.5.0"
resolved "http://verdaccio.ds.io:4873/@turf%2fsimplify/-/simplify-6.5.0.tgz#ec435460bde0985b781618b05d97146c32c8bc16"
Expand Down Expand Up @@ -3421,6 +3484,11 @@
resolved "http://verdaccio.ds.io:4873/@types%2fgeojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249"
integrity sha512-Nmh0K3iWQJzniTuPRcJn5hxXkfB1T1pgB89SBig5PlJQU5yocazeu4jATJlaA0GYFKWMqDdvYemoSnF2pXgLVA==

"@types/[email protected]":
version "7946.0.8"
resolved "http://verdaccio.ds.io:4873/@types%2fgeojson/-/geojson-7946.0.8.tgz#30744afdb385e2945e22f3b033f897f76b1f12ca"
integrity sha512-1rkryxURpr6aWP7R786/UQOkJ3PcpQiWkAXBmdWc7ryFWqN6a4xfK7BtjXvFBKO9LjQ+MWQSWxYeZX1OApnArA==

"@types/graceful-fs@^4.1.3":
version "4.1.5"
resolved "http://verdaccio.ds.io:4873/@types%2fgraceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down Expand Up @@ -6560,6 +6628,17 @@ geojson-flatten@^1.0.4:
resolved "http://verdaccio.ds.io:4873/geojson-flatten/-/geojson-flatten-1.1.1.tgz#601aae07ba6406281ebca683573dcda69eba04c7"
integrity sha512-k/6BCd0qAt7vdqdM1LkLfAy72EsLDy0laNwX0x2h49vfYCiQkRc4PSra8DNEdJ10EKRpwEvDXMb0dBknTJuWpQ==

[email protected]:
version "3.2.0"
resolved "http://verdaccio.ds.io:4873/geojson-rbush/-/geojson-rbush-3.2.0.tgz#8b543cf0d56f99b78faf1da52bb66acad6dfc290"
integrity sha512-oVltQTXolxvsz1sZnutlSuLDEcQAKYC/uXt9zDzJJ6bu0W+baTI8LZBaTup5afzibEH4N3jlq2p+a152wlBJ7w==
dependencies:
"@turf/bbox" "*"
"@turf/helpers" "6.x"
"@turf/meta" "6.x"
"@types/geojson" "7946.0.8"
rbush "^3.0.1"

geojson-validation@^1.0.2:
version "1.0.2"
resolved "http://verdaccio.ds.io:4873/geojson-validation/-/geojson-validation-1.0.2.tgz#5c11a83afbec9a1cb9d76c73d47843dbd154d3ff"
Expand Down Expand Up @@ -10509,6 +10588,13 @@ raf@^3.1.0:
dependencies:
performance-now "^2.1.0"

rbush@^3.0.1:
version "3.0.1"
resolved "http://verdaccio.ds.io:4873/rbush/-/rbush-3.0.1.tgz#5fafa8a79b3b9afdfe5008403a720cc1de882ecf"
integrity sha512-XRaVO0YecOpEuIvbhbpTrZgoiI6xBlz6hnlr6EHhd+0x9ase6EmeN+hdwwUaJvLcsFFQ8iWVF1GAK1yB0BWi0w==
dependencies:
quickselect "^2.0.0"

react-calendar@^3.3.0:
version "3.7.0"
resolved "http://verdaccio.ds.io:4873/react-calendar/-/react-calendar-3.7.0.tgz#951d56e91afb33b1c1e019cb790349fbffcc6894"
Expand Down
Loading