Skip to content
Draft
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
44 changes: 42 additions & 2 deletions backend/configs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,50 @@
"soil_eros_water_infiltration": "600 * 300 / ( (epoch(interval(beer1)) + epoch(interval(beer2)) + epoch(interval(beer3)) + epoch(interval(beer4)) + epoch(interval(beer5)) + epoch(interval(beer6)) + epoch(interval(beer7)) + epoch(interval(beer8)) + epoch(interval(beer9)) + epoch(interval(beer10)) ) / 10 * pi() * 9^2) if beer1 != '' else 0",
"soil_fauna_density": "sum(tsbf_001.tsbf_no) / tsbf_samp",
"soil_fauna_diversity": "list_unique(list(nullif(tsbf_001.concat_ws('-', tsbf_tax1, tsbf_tax2, tsbf_tax3), '')))",
"soil_fauna_abundance": "list_aggregate(list(tsbf_001.concat_ws('-', tsbf_tax1, tsbf_tax2, tsbf_tax3)), 'histogram')",
"soil_fauna_abundance": "list_aggregate(list(nullif(tsbf_001.concat_ws('-', tsbf_tax1, tsbf_tax2, tsbf_tax3), '')), 'histogram')",
"soil_fauna_abundance_pop": "list_concat(list(tsbf_001.concat_ws(':', concat_ws('-',tsbf_tax1, tsbf_tax2, tsbf_tax3), tsbf_no) if tsbf_001.tsbf_no > 0))",
"soil_fauna_total_pop": "int(sum(tsbf_001.tsbf_no))",
"soil_surface_fauna_density": "(sum(barba_001.barbA_no) + sum(barbb_001.barbB_no) + sum(barbc_001.barbC_no) + sum(barbd_001.barbD_no)) / barb_samp",
"soil_surface_fauna_diversity": "list_unique(list_concat(list(nullif(barba_001.concat_ws('-', barbA_tax1, barbA_tax2, barbA_tax3), '')), list(nullif(barbb_001.concat_ws('-', barbB_tax1, barbB_tax2, barbB_tax3), '')), list(nullif(barbc_001.concat_ws('-', barbC_tax1, barbC_tax2, barbC_tax3), '')), list(nullif(barbd_001.concat_ws('-', barbD_tax1, barbD_tax2, barbD_tax3), ''))))",
"soil_surface_fauna_abundance": "list_aggregate(list_concat(list(barba_001.concat_ws('-', barbA_tax1, barbA_tax2, barbA_tax3)), list(barbb_001.concat_ws('-', barbB_tax1, barbB_tax2, barbB_tax3)), list(barbc_001.concat_ws('-', barbC_tax1, barbC_tax2, barbC_tax3)), list(barbd_001.concat_ws('-', barbD_tax1, barbD_tax2, barbD_tax3))), 'histogram')"
"soil_surface_fauna_abundance": "list_aggregate(list_concat(list(nullif(barba_001.concat_ws('-', barbA_tax1, barbA_tax2, barbA_tax3), '')), list(nullif(barbb_001.concat_ws('-', barbB_tax1, barbB_tax2, barbB_tax3), '')), list(nullif(barbc_001.concat_ws('-', barbC_tax1, barbC_tax2, barbC_tax3), '')), list(nullif(barbd_001.concat_ws('-', barbD_tax1, barbD_tax2, barbD_tax3), ''))), 'histogram')",
"soil_surface_fauna_abundance_pop": "list_concat(list(barba_001.concat_ws(':', concat_ws('-',barbA_tax1, barbA_tax2, barbA_tax3), barbA_no) if barba_001.barbA_no > 0), list(barbb_001.concat_ws(':', concat_ws('-',barbB_tax1, barbB_tax2, barbB_tax3), barbB_no) if barbb_001.barbB_no > 0), list(barbc_001.concat_ws(':', concat_ws('-',barbC_tax1, barbC_tax2, barbC_tax3), barbC_no) if barbc_001.barbC_no > 0), list(barbd_001.concat_ws(':', concat_ws('-',barbD_tax1, barbD_tax2, barbD_tax3), barbD_no) if barbd_001.barbD_no > 0))",
"soil_surface_fauna_total_pop": "int(sum(barba_001.barbA_no) + sum(barbb_001.barbB_no) + sum(barbc_001.barbC_no) + sum(barbd_001.barbD_no))"
},
"popup": {
"trigger": "click"
}
},
{
"id": "inventaire_bio",
"type": "datapackage",
"path": "../catalog/inventaire_bio",
"resource": "inv_bio",
"layerType": "symbol",
"cluster": {
"radius": 50,
"maxZoom": 50,
"steps": [10, 20],
"colors": ["#99D6C2", "#009966", "#006B47"]
},
"columns": {
"geom": "start_point.merge().centroid()",
"id": "_id",
"conf": "conf",
"proj": "proj",
"ecos": "ecos",
"region": "loc1",
"forest": "loc2",
"start_date": "coh",
"taxon": "'N/A'",
"type": "meth",
"samp_area": "inv_samp",
"samp_unit": "inv_unit",
"density": "int(sum(inv_001.no)) / inv_samp",
"dens_unit": "dens_unit",
"richness": "list_unique(list(nullif(inv_001.concat_ws('-', tax1, tax2, tax3), '')))",
"taxons_relative_abundance": "list_aggregate(list(nullif(inv_001.concat_ws('-', tax1, tax2, tax3), '')), 'histogram')",
"pop_by_taxon": "list_concat(list(inv_001.concat_ws(':', concat_ws('-',tax1, tax2, tax3), no) if inv_001.no > 0))",
"total_pop": "int(sum(inv_001.no))"
},
"popup": {
"trigger": "click"
Expand Down
2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
asgiref==3.8.1
coordo @ git+https://github.com/dataforgoodfr/Coordonnees.git@0.6.0#subdirectory=coordo-py
coordo @ git+https://github.com/dataforgoodfr/Coordonnees.git@0.6.2#subdirectory=coordo-py
Django>=4.2.27
djangorestframework==3.16.0
djangorestframework_simplejwt==5.5.1
Expand Down
12 changes: 0 additions & 12 deletions webapp/src/app/providers/map-provider-all4trees.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { renderAnchor, renderLayerRow } from "@features/controls/layer-control";

import { API_URL } from "@shared/api/client";
import {
type Category,
MapContext,
} from "@shared/contexts/map-context-all4trees";
import { useLocalStorage } from "@shared/hooks/use-local-storage";
Expand All @@ -30,7 +29,6 @@ type MapProviderAll4TreesProps = {
export function MapProviderAll4Trees({ children }: MapProviderAll4TreesProps) {
const [isReady, setIsReady] = useState(false);
const mapApiRef = useRef<ReturnType<typeof createMap> | null>(null);
const [forests, setForests] = useState<Category[]>([]);
const [mapSettings, setMapSettings] = useLocalStorage<MapSettings>(
"d4g:map-settings:all4trees",
DEFAULT_MAP_SETTINGS,
Expand All @@ -51,14 +49,6 @@ export function MapProviderAll4Trees({ children }: MapProviderAll4TreesProps) {

const handleReady = () => {
setIsReady(true);
// biome-ignore lint/suspicious/noExplicitAny : <no types from the lib coordo>
const metadata: any = mapApiRef.current?.getLayerMetadata("inventaire");
const forestField = metadata?.schema?.fields?.find(
(f: { name: string }) => f.name === "for",
);
if (forestField?.categories) {
setForests(forestField.categories);
}

// On first mount, sync the map state with the local storage state of "categories-filters"
syncInitialCategoriesFilters({
Expand Down Expand Up @@ -100,12 +90,10 @@ export function MapProviderAll4Trees({ children }: MapProviderAll4TreesProps) {
<MapContext
value={{
categoriesFilters,
forests,
isReady,
mapApiRef,
mapContainerRef,
setCategoriesFilters,
setForests,
setIsReady,
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export const useCategoriesConfig = (): {

// ----- Actions -----

const actionInventary: CategoryGroupItem = {
const actionInventory: CategoryGroupItem = {
icon: (
<TreePineIcon
className="text-inventaire"
size={ICON_SIZE}
/>
),
identifier: CATEGORY_IDENTIFIERS.ACTION_INVENTARY,
label: t("filters.categories.actions.forestInventary"),
identifier: CATEGORY_IDENTIFIERS.ACTION_INVENTORY,
label: t("filters.categories.actions.forestInventory"),
};

const actionDiversity: CategoryGroupItem = {
Expand Down Expand Up @@ -111,7 +111,7 @@ export const useCategoriesConfig = (): {
};

return {
actions: [actionInventary, actionDiversity, actionSocioEco],
actions: [actionInventory, actionDiversity, actionSocioEco],
data: [dataGround, dataSatellite, dataModel],
system: [
systemMangroveHigh,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const useCategoriesFilters = () => {
const [categoriesFilters, setCategoriesFilters] =
useLocalStorage<CategoriesFiltersState>("categories-filters", {
[CATEGORY_IDENTIFIERS.ACTION_DIVERSITY]: true,
[CATEGORY_IDENTIFIERS.ACTION_INVENTARY]: true,
[CATEGORY_IDENTIFIERS.ACTION_INVENTORY]: true,
[CATEGORY_IDENTIFIERS.ACTION_BIO]: true,
[CATEGORY_IDENTIFIERS.ACTION_SOCIO]: true,
[CATEGORY_IDENTIFIERS.DATA_GROUND]: true,
[CATEGORY_IDENTIFIERS.DATA_MODEL]: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useTranslation } from "@shared/i18n";
import type { LayerMetadata } from "@shared/lib/coordo";
import { findCategoricalLabel, precise } from "@shared/lib/utils";
import { findCategoricalLabel } from "@shared/lib/utils";
import type { ChartConfig } from "@shared/ui/chart";

import type { ChartComponentType } from "../components/chart-component";
import { PieChartCategorical } from "../components/pie-chart-categorical";

type PieChartProps = {
data: [string, number][];
data: Record<string, number>;
metadata: LayerMetadata;
};

Expand All @@ -16,19 +16,18 @@ export const ChartRelativeAbundance: ChartComponentType<PieChartProps> = ({
metadata,
}) => {
const { t } = useTranslation("all4trees");
const smallCategoriesSum = Number(
precise(
data
.filter(([_, value]) => value < 5)
.reduce((acc, [_, value]) => acc + value, 0),
),
);
const chartData = data
.filter(([name, value]) => name !== "0" && (data.length < 6 || value >= 5))
.map((element, index) => ({
const smallCategoriesSum = Object.values(data)
.filter((value) => value < 5)
.reduce((acc, value) => acc + value, 0);
const chartData = Object.entries(data)
.filter(
([name, value]) =>
name !== "0" && (Object.keys(data).length < 6 || value >= 5),
)
.map(([name, value], index) => ({
fill: `var(--chart-${(index % 5) + 1})`,
name: element[0],
value: element[1],
name,
value,
}));

let chartConfig: ChartConfig = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {

import { useTranslation } from "@shared/i18n";

import { SUNBURST_LAYOUT } from "../config";
import { buildNodeColors, buildSunburstNodes } from "../lib/sunburst";
import type { PieChartProps, SunburstTrace } from "../types";
import { SUNBURST_LAYOUT } from "../soil/config";
import { buildNodeColors, buildSunburstNodes } from "./lib/sunburst";
import type { PieChartProps, SunburstTrace } from "../soil/types";

export const ChartTaxonAbundance: ChartComponentType<PieChartProps> = ({
data,
metadata,
dataType,
dataType = null,
}) => {
const { t } = useTranslation(["common", "all4trees"]);
const dataEntries = Object.entries(data);
Expand All @@ -29,11 +29,16 @@ export const ChartTaxonAbundance: ChartComponentType<PieChartProps> = ({
);
const nodes = buildSunburstNodes(filteredDataEntries, metadata, dataType);
const nodeColors = buildNodeColors(nodes);
const hoverText = nodes.map(
(node) => `${node.label}<br>${node.value.toFixed(2)} %`,
);

sunburstData = [
{
branchvalues: "total",
extendsunburstcolorway: true,
hoverinfo: "text",
hovertext: hoverText,
ids: nodes.map((n) => n.id),
labels: nodes.map((n) => n.label),
leaf: { opacity: 0.4 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { LayerMetadata } from "@shared/lib/coordo";
import { getChartPalette } from "@shared/lib/palette";

import type { SunburstNode } from "../types";
import type { SunburstNode } from "../../soil/types";
import { formatTaxonLevelLabel } from "./taxon";

export function buildSunburstNodes(
dataEntries: [string, number][],
metadata: LayerMetadata,
dataType: "tsbf" | "barbA",
dataType: "tsbf" | "barbA" | null,
): SunburstNode[] {
const nodes: SunburstNode[] = [];
const seen = new Set<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import type { LayerMetadata } from "@shared/lib/coordo";
import { findCategoricalLabel } from "@shared/lib/utils";

export function getDataTypePrefix(dataType: "tsbf" | "barbA" | null): string {
return dataType ? `${dataType}_` : '';
}

export function getTaxonLabels(
element: string,
metadata: LayerMetadata,
dataType: "tsbf" | "barbA",
dataType: "tsbf" | "barbA" | null,
): [string, string, string] {
const [taxon1, taxon2, taxon3] = element.split("-");
const taxon1Label =
findCategoricalLabel(metadata, `${dataType}_tax1`, taxon1) || taxon1;
findCategoricalLabel(metadata, `${getDataTypePrefix(dataType)}tax1`, taxon1) || taxon1;
const taxon2Label =
findCategoricalLabel(metadata, `${dataType}_tax2`, taxon2) || taxon2;
findCategoricalLabel(metadata, `${getDataTypePrefix(dataType)}tax2`, taxon2) || taxon2;
const taxon3Label =
findCategoricalLabel(metadata, `${dataType}_tax3`, taxon3) || taxon3;
findCategoricalLabel(metadata, `${getDataTypePrefix(dataType)}tax3`, taxon3) || taxon3;
return [taxon1Label, taxon2Label, taxon3Label];
}

export function formatTaxonLevelLabel(
element: string,
metadata: LayerMetadata,
dataType: "tsbf" | "barbA",
dataType: "tsbf" | "barbA" | null,
): string {
const [taxon1Label, taxon2Label, taxon3Label] = getTaxonLabels(
element,
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/features/charts/soil/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { LayerMetadata } from "@shared/lib/coordo";
export type PieChartProps = {
data: Record<string, number>;
metadata: LayerMetadata;
dataType: "tsbf" | "barbA";
dataType?: "tsbf" | "barbA";
};

export type SunburstNode = {
Expand All @@ -17,6 +17,8 @@ export type SunburstTrace = {
type: "sunburst";
branchvalues: "total";
extendsunburstcolorway: boolean;
hoverinfo?: string;
hovertext?: string[];
ids: string[];
labels: string[];
parents: string[];
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/features/controls/layer-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function useLayerConfig(layerId: string) {
/>
),
};
case LAYERS.INVENTARY:
case LAYERS.INVENTORY_FOR:
return {
label: t("filters.categories.actions.forestInventary") as string,
label: t("filters.categories.actions.forestInventory") as string,
renderIcon: (checked?: boolean) => (
<TreePineIcon
className={checked ? "text-inventaire" : "text-muted"}
Expand Down
49 changes: 49 additions & 0 deletions webapp/src/features/indicators/bio-inventory/format-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useTranslation } from "react-i18next";

import {
formatTaxonAbundance,
preciseNumericIndicators,
} from "@features/indicators/utils";

import type { NumericKeys } from "@shared/types";

import type { BioInventoryData } from "@features/popup/bio-inventory";
import { findCategoricalLabel } from "@shared/lib/utils";
import type { LayerMetadata } from "@shared/lib/coordo";

const indicatorKeys: NumericKeys<BioInventoryData>[] = [
"samp_area",
"density",
"richness",
"total_pop",
];

/**
* Return data in a convenient way for UI rendering, handling units and fixing
*/
export const useFormatBioInventoryData = (data: BioInventoryData, metadata: LayerMetadata) => {
const { t } = useTranslation("common");

const safeData = preciseNumericIndicators<BioInventoryData>(
data,
indicatorKeys,
t("dataManagement.noData"),
);

safeData.taxons_relative_abundance = formatTaxonAbundance(
safeData.pop_by_taxon,
safeData.total_pop,
);

return {
taxon: 'Lémuriens', // findCategoricalLabel(metadata, "tax", safeData.taxon) || safeData.taxon
type: findCategoricalLabel(metadata, "meth", safeData.type.toString()) || safeData.type,
area: `${safeData.samp_area} ${findCategoricalLabel(metadata, "inv_unit", safeData.samp_unit) || t("dataManagement.noUnit")}`,
density: `${safeData.density} ${safeData.dens_unit || t("dataManagement.noUnit")}`,
richness: safeData.richness,
relative_abundance: safeData.taxons_relative_abundance,
total_population: safeData.total_pop,
};
};

export type FormattedData = ReturnType<typeof useFormatBioInventoryData>;
1 change: 1 addition & 0 deletions webapp/src/features/indicators/bio-inventory/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useBioInventoryIndicatorElements as useBiodiversityIndicatorElements } from "./use-bioinventory-indicator-elements";
Loading