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

legend ignores transparent colors #360

Merged
merged 2 commits into from
Dec 17, 2024
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
34 changes: 16 additions & 18 deletions ecoscope/mapping/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import pandas as pd
import rasterio as rio
from ecoscope.base.utils import color_tuple_to_css
from ecoscope.base.utils import color_tuple_to_css, hex_to_rgba
from io import BytesIO
from typing import Dict, IO, List, Optional, TextIO, Union
from pathlib import Path
Expand Down Expand Up @@ -254,29 +254,27 @@ def add_legend(self, labels: list | pd.Series, colors: list | pd.Series, **kwarg
style: dict
Additional style params
"""
nans = None
if isinstance(labels, pd.Series):
if pd.api.types.is_numeric_dtype(labels):
nans = np.argwhere(np.isnan(labels))
labels = labels.unique().tolist()
labels = labels.tolist()
if isinstance(colors, pd.Series):
colors = colors.unique().tolist()
colors = colors.tolist()

labels = list(dict.fromkeys(labels))
colors = list(dict.fromkeys(colors))
if len(labels) != len(colors):
raise ValueError("Unique label and color values must be of equal number")

if nans is not None:
filtered_labels = []
filtered_colors = []
for index, value in enumerate(labels):
if index not in nans:
filtered_labels.append(labels[index])
filtered_colors.append(colors[index])
labels = filtered_labels
colors = filtered_colors

widget_labels = [str(label) for label in labels]
widget_colors = [color_tuple_to_css(color) if isinstance(color, tuple) else color for color in colors]
filtered_labels = []
filtered_colors = []
for index, value in enumerate(colors):
if isinstance(value, str):
value = hex_to_rgba(value)
if len(value) == 4 and value[3] > 0:
filtered_labels.append(labels[index])
filtered_colors.append(colors[index])

widget_labels = [str(label) for label in filtered_labels]
widget_colors = [color_tuple_to_css(color) if isinstance(color, tuple) else color for color in filtered_colors]

self.add_widget(LegendWidget(labels=widget_labels, colors=widget_colors, **kwargs))

Expand Down
7 changes: 3 additions & 4 deletions tests/test_ecomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import random
import ee
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
import ecoscope
Expand Down Expand Up @@ -89,16 +88,16 @@ def test_add_legend_series():
assert legend.colors == ["rgba(0, 0, 0, 1.0)", "rgba(255, 255, 255, 1.0)"]


def test_add_legend_series_with_nan():
def test_add_legend_series_with_transparent():
m = EcoMap(default_widgets=False)
m.add_legend(
labels=pd.Series([0, 1, np.nan, 5, np.nan]),
labels=pd.Series([1, 3, 0, 5, 0]),
colors=pd.Series([(0, 0, 0, 255), (255, 255, 255, 255), (0, 0, 0, 0), (100, 100, 100, 255), (0, 0, 0, 0)]),
)
assert len(m.deck_widgets) == 1
legend = m.deck_widgets[0]
assert isinstance(legend, LegendWidget)
assert legend.labels == ["0.0", "1.0", "5.0"]
assert legend.labels == ["1", "3", "5"]
assert legend.colors == ["rgba(0, 0, 0, 1.0)", "rgba(255, 255, 255, 1.0)", "rgba(100, 100, 100, 1.0)"]


Expand Down
Loading