Skip to content

Commit a12c0e4

Browse files
committed
abundance feature layer
1 parent 2ac1fc3 commit a12c0e4

File tree

5 files changed

+73
-103
lines changed

5 files changed

+73
-103
lines changed

app/hooks/map/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ export function usePUGridLayer({
658658
puIncludedValue,
659659
puExcludedValue,
660660
puAvailableValue,
661+
selectedFeatures = [],
661662
preHighlightFeatures = [],
662663
postHighlightFeatures = [],
663664
runId,
@@ -764,6 +765,55 @@ export function usePUGridLayer({
764765
],
765766
},
766767
})),
768+
// features abundance
769+
...selectedFeatures.map((featureId) => {
770+
const {
771+
visibility = true,
772+
opacity = 1,
773+
amountRange = { min: 50000, max: 1000000 },
774+
} = restLayerSettings[featureId] || {};
775+
776+
return {
777+
type: 'fill',
778+
'source-layer': 'layer0',
779+
layout: {
780+
visibility: getLayerVisibility(visibility),
781+
},
782+
filter: ['all', ['in', featureId, ['get', 'featureList']]],
783+
paint: {
784+
'fill-outline-color': 'yellow',
785+
'fill-color': [
786+
'let',
787+
'amount',
788+
[
789+
'to-number',
790+
[
791+
'let',
792+
'idx',
793+
['index-of', featureId, ['get', 'featureList']],
794+
[
795+
'slice',
796+
['get', 'featureList'],
797+
['+', ['index-of', ':', ['get', 'featureList'], ['var', 'idx']], 1],
798+
['index-of', ';', ['get', 'featureList'], ['var', 'idx']],
799+
],
800+
],
801+
],
802+
[
803+
'interpolate',
804+
['linear'],
805+
['var', 'amount'],
806+
amountRange.min,
807+
'white', // ! use COLORS.abundance.default instead when is available
808+
amountRange.max,
809+
'green',
810+
// color, // ! enable the color variable when we receive it
811+
],
812+
],
813+
'fill-opacity': opacity,
814+
},
815+
};
816+
}),
767817
]
768818
: []),
769819

app/hooks/map/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export interface UsePUGridLayer {
123123
puExcludedValue?: string[];
124124
puAvailableValue?: string[];
125125
runId?: number;
126-
features?: string[];
126+
features?: Feature['id'][];
127+
selectedFeatures?: Feature['id'][];
127128
preHighlightFeatures?: Array<string>;
128129
postHighlightFeatures?: Array<string>;
129130
cost?: {
@@ -172,6 +173,10 @@ export interface UsePUGridLayer {
172173
[key: string]: {
173174
opacity?: number;
174175
visibility?: boolean;
176+
amountRange?: {
177+
min: number;
178+
max: number;
179+
};
175180
};
176181
};
177182
};

app/layout/projects/show/map/legend/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const useFeatureAbundanceLegend = () => {
169169

170170
dispatch(
171171
setLayerSettings({
172-
id: `feature-abundance-${featureId}`,
172+
id: featureId,
173173
settings: {
174174
visibility: !layerSettings[featureId]?.visibility,
175175
amountRange,

app/layout/scenarios/edit/map/component.tsx

Lines changed: 15 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@ import { getScenarioEditSlice } from 'store/slices/scenarios/edit';
77

88
import PluginMapboxGl from '@vizzuality/layer-manager-plugin-mapboxgl';
99
import { LayerManager, Layer } from '@vizzuality/layer-manager-react';
10-
import { sortBy } from 'lodash';
1110
import { FiLayers } from 'react-icons/fi';
1211

1312
import { useAccessToken } from 'hooks/auth';
1413
import { useSelectedFeatures, useTargetedFeatures } from 'hooks/features';
1514
import { useAllGapAnalysis } from 'hooks/gap-analysis';
1615
import {
17-
// usePUGridPreviewLayer,
18-
// useAdminPreviewLayer,
1916
useWDPAPreviewLayer,
2017
usePUGridLayer,
2118
useFeaturePreviewLayers,
22-
// useLegend,
2319
useBBOX,
2420
useTargetedPreviewLayers,
2521
} from 'hooks/map';
2622
import { useProject } from 'hooks/projects';
27-
import { useCostSurfaceRange, useScenario, useScenarioPU } from 'hooks/scenarios';
23+
import { useScenario, useScenarioPU } from 'hooks/scenarios';
2824
import { useBestSolution } from 'hooks/solutions';
2925
import { useWDPACategories } from 'hooks/wdpa';
3026

@@ -130,59 +126,11 @@ export const ScenariosEditMap = (): JSX.Element => {
130126
const { data: scenarioData } = useScenario(sid);
131127

132128
const { data: selectedFeaturesData } = useSelectedFeatures(sid, {});
133-
134129
const { data: targetedFeaturesData } = useTargetedFeatures(sid, {});
135-
136-
const previewFeatureIsSelected = useMemo(() => {
137-
if (tab === TABS['scenario-features']) {
138-
return (
139-
(selectedFeaturesData || []).filter(({ id }) => selectedFeatures.includes(id)).length > 0
140-
);
141-
}
142-
143-
if (tab === TABS['scenario-features-targets-spf']) {
144-
return (
145-
(targetedFeaturesData || []).filter(({ id }) => selectedFeatures.includes(id)).length > 0
146-
);
147-
}
148-
149-
return false;
150-
}, [tab, selectedFeaturesData, targetedFeaturesData, selectedFeatures]);
151-
152-
const selectedPreviewFeatures = useMemo(() => {
153-
if (tab === TABS['scenario-features']) {
154-
return (selectedFeaturesData || [])
155-
.filter(({ id }) => selectedFeatures.includes(id))
156-
.map(({ name, id }) => ({ name, id }))
157-
.sort((a, b) => {
158-
const aIndex = selectedFeatures.indexOf(a.id as string);
159-
const bIndex = selectedFeatures.indexOf(b.id as string);
160-
return aIndex - bIndex;
161-
});
162-
}
163-
164-
if (tab === TABS['scenario-features-targets-spf']) {
165-
return (targetedFeaturesData || [])
166-
.filter(({ id }) => selectedFeatures.includes(id))
167-
.map(({ name, id }) => ({ name, id }))
168-
.sort((a, b) => {
169-
const aIndex = selectedFeatures.indexOf(a.id as string);
170-
const bIndex = selectedFeatures.indexOf(b.id as string);
171-
return aIndex - bIndex;
172-
});
173-
}
174-
175-
return [];
176-
}, [tab, selectedFeaturesData, targetedFeaturesData, selectedFeatures]);
177-
178-
const { data: costSurfaceRangeData } = useCostSurfaceRange(sid);
179-
180130
const { data: allGapAnalysisData } = useAllGapAnalysis(sid, {
181131
enabled: !!sid,
182132
});
183-
184133
const { data: projectData } = useProject(pid);
185-
186134
const { data: protectedAreasData } = useWDPACategories({
187135
adminAreaId:
188136
projectData?.adminAreaLevel2Id || projectData?.adminAreaLevel1I || projectData?.countryId,
@@ -285,55 +233,35 @@ export const ScenariosEditMap = (): JSX.Element => {
285233
puIncludedValue: [...puIncludedValue, ...puTmpIncludedValue],
286234
puExcludedValue: [...puExcludedValue, ...puTmpExcludedValue],
287235
puAvailableValue: [...puAvailableValue, ...puTmpAvailableValue],
288-
// features: [TABS['scenario-target-achievement'], TABS['scenario-gap-analysis']].includes(tab)
289-
// ? []
290-
// : featuresIds,
236+
features: [TABS['scenario-features'], TABS['scenario-features-targets-spf']].includes(tab)
237+
? []
238+
: featuresIds,
239+
selectedFeatures,
291240
preHighlightFeatures,
292241
postHighlightFeatures: postHighlightedFeaturesIds,
293-
// cost: costSurfaceRangeData,
294242
runId: selectedSolution?.runId || bestSolution?.runId,
295243
settings: {
296-
// pugrid: layerSettings.pugrid,
297-
// 'wdpa-percentage': layerSettings['wdpa-percentage'],
298-
// features: layerSettings.features,
299-
// 'cost-surface': layerSettings[selectedCostSurface],
300-
// 'lock-in': layerSettings['lock-in'],
301-
// 'lock-out': layerSettings['lock-out'],
302-
// 'lock-available': layerSettings['lock-available'],
303-
// frequency: layerSettings.frequency,
304-
// solution: layerSettings.solution,
244+
pugrid: layerSettings.pugrid,
245+
'wdpa-percentage': layerSettings['wdpa-percentage'],
246+
'features-highlight': layerSettings['features-highlight'],
247+
cost: layerSettings.cost,
248+
'lock-in': layerSettings['lock-in'],
249+
'lock-out': layerSettings['lock-out'],
250+
'lock-available': layerSettings['lock-available'],
251+
frequency: layerSettings.frequency,
252+
solution: layerSettings.solution,
305253
...layerSettings,
306254
},
307255
},
308256
});
309257

310258
const LAYERS = [
311-
// PUGridPreviewLayer,
312-
// AdminPreviewLayer,
313259
PUGridLayer,
314260
WDPApreviewLayer,
315261
...FeaturePreviewLayers,
316262
...TargetedPreviewLayers,
317263
].filter((l) => !!l);
318264

319-
// const LEGEND = useLegend({
320-
// layers,
321-
// options: {
322-
// wdpaIucnCategories: protectedAreas,
323-
// wdpaThreshold:
324-
// tab === TABS['scenario-protected-areas'] ? wdpaThreshold : scenarioData?.wdpaThreshold,
325-
// cost: costSurfaceRangeData,
326-
// items: selectedPreviewFeatures,
327-
// puAction,
328-
// puIncludedValue: [...puIncludedValue, ...puTmpIncludedValue],
329-
// puExcludedValue: [...puExcludedValue, ...puTmpExcludedValue],
330-
// puAvailableValue: [...puAvailableValue, ...puTmpAvailableValue],
331-
// runId: selectedSolution?.runId || bestSolution?.runId,
332-
// numberOfRuns: scenarioData?.numberOfRuns || 0,
333-
// layerSettings,
334-
// },
335-
// });
336-
337265
useEffect(() => {
338266
setBounds({
339267
bbox: BBOX,
@@ -491,7 +419,7 @@ export const ScenariosEditMap = (): JSX.Element => {
491419
);
492420

493421
const handleTransformRequest = useCallback(
494-
(url) => {
422+
(url: string) => {
495423
if (url.startsWith(process.env.NEXT_PUBLIC_API_URL)) {
496424
return {
497425
url,
@@ -520,19 +448,6 @@ export const ScenariosEditMap = (): JSX.Element => {
520448
[setLayerSettings, dispatch]
521449
);
522450

523-
const onChangeVisibility = useCallback(
524-
(id) => {
525-
const { visibility = true } = layerSettings[id] || {};
526-
dispatch(
527-
setLayerSettings({
528-
id,
529-
settings: { visibility: !visibility },
530-
})
531-
);
532-
},
533-
[setLayerSettings, dispatch, layerSettings]
534-
);
535-
536451
const renderLegendItems = ({
537452
type,
538453
intersections,

app/layout/scenarios/edit/map/legend/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export const useFeatureAbundanceLegend = () => {
154154

155155
dispatch(
156156
setLayerSettings({
157-
id: `feature-abundance-${featureId}`,
157+
id: featureId,
158158
settings: {
159159
visibility: !layerSettings[featureId]?.visibility,
160160
amountRange,

0 commit comments

Comments
 (0)