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

BUG: gdf.explore() method fails when highlight_kwds contains custom style_function #3031

Open
1 task
BartoszBiernacki opened this issue Sep 28, 2023 · 2 comments · May be fixed by #3032
Open
1 task

BUG: gdf.explore() method fails when highlight_kwds contains custom style_function #3031

BartoszBiernacki opened this issue Sep 28, 2023 · 2 comments · May be fixed by #3032
Labels

Comments

@BartoszBiernacki
Copy link

BartoszBiernacki commented Sep 28, 2023

  • [Y] I have checked that this issue has not already been reported.

  • [Y] I have confirmed this bug exists on the latest version of geopandas.

  • (optional) I have confirmed this bug exists on the main branch of geopandas.


Code Sample, a copy-pastable example

import geopandas
import pandas as pd

dict_data = {
    'LATITUDE': {0: 47.001, 1: 47.002, 2: 47.003},
    'LONGITUDE': {0: 6.001, 1: 6.002, 2: 6.003},
}

df = pd.DataFrame(dict_data)

gdf = geopandas.GeoDataFrame(
    data=df,
    crs='EPSG:4326',
    geometry=geopandas.points_from_xy(df['LONGITUDE'], df['LATITUDE']),
)

gdf.explore(
    style_kwds={'radius': 20},
    highlight_kwds={'style_function': lambda x: {'fillColor': 'red'}},
)

Problem description

TypeError: Object of type function is not JSON serializable

Expected Output

Interactive map with 3 points which color changes to red if mouse is over them.

My solution

I looked into explore.py and at line 570 found this code

    if highlight:
        if "fillOpacity" not in highlight_kwds:
            highlight_kwds["fillOpacity"] = 0.75

        def _style_highlight(x):
            return {**highlight_kwds}

        highlight_function = _style_highlight
    else:
        highlight_function = None

After making it similar to how style_kwds is treated, eg.

    if highlight:
        if "fillOpacity" not in highlight_kwds:
            highlight_kwds["fillOpacity"] = 0.75
        if "style_function" in highlight_kwds:
            highlight_kwds_function = highlight_kwds["style_function"]
            if not callable(highlight_kwds_function):
                raise ValueError("'style_function' has to be a callable")
            highlight_kwds.pop("style_function")
        else:
            highlight_kwds_function = lambda x: {}
            
        def _style_highlight(x):
            return {**highlight_kwds, **highlight_kwds_function(x)}

        highlight_function = _style_highlight
    else:
        highlight_function = None

code from example above works as expected.

Output of geopandas.show_versions()

SYSTEM INFO

python : 3.11.5 | packaged by conda-forge | (main, Aug 27 2023, 03:23:48) [MSC v.1936 64 bit (AMD64)]
executable : C:\PROGRAMY\Anaconda\envs\geo_env\python.exe
machine : Windows-10-10.0.19045-SP0

GEOS, GDAL, PROJ INFO

GEOS : 3.12.0
GEOS lib : None
GDAL : 3.7.2
GDAL data dir: None
PROJ : 9.2.1
PROJ data dir: C:\PROGRAMY\Anaconda\envs\geo_env\Library\share\proj

PYTHON DEPENDENCIES

geopandas : 0.14.0
numpy : 1.26.0
pandas : 2.1.1
pyproj : 3.6.1
shapely : 2.0.1
fiona : 1.9.4
geoalchemy2: None
geopy : 2.4.0
matplotlib : 3.8.0
mapclassify: 2.5.0
pygeos : None
pyogrio : None
psycopg2 : None
pyarrow : 13.0.0
rtree : 1.0.1

@martinfleis
Copy link
Member

Thanks! Can you turn your solution into a pull request?

BartoszBiernacki added a commit to BartoszBiernacki/geopandas that referenced this issue Sep 28, 2023
BartoszBiernacki added a commit to BartoszBiernacki/geopandas that referenced this issue Sep 28, 2023
@BartoszBiernacki
Copy link
Author

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants