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

style_callback param for add_geojson() not working? #119

Closed
deeplook opened this issue Oct 3, 2021 · 8 comments
Closed

style_callback param for add_geojson() not working? #119

deeplook opened this issue Oct 3, 2021 · 8 comments
Labels
bug Something isn't working

Comments

@deeplook
Copy link
Collaborator

deeplook commented Oct 3, 2021

Environment Information

  • leafmap version: 0.5.0
  • Python version: 3.9
  • Operating System: Linux/macOS

Description

I want to use the style_callback parameter for map.add_geojson(), but the chosen style which sets only the color seems not to be respected. I think the style dicts are the same for ipyleaflet and leafmap, at least I could not find any contradictory information. See below.

import requests
data = requests.get((
    "https://raw.githubusercontent.com/telegeography/www.submarinecablemap.com"
    "/master/web/public/api/v3/cable/cable-geo.json"
)).json()
callback = lambda feat: {"color": feat["properties"]["color"]}
import leafmap
m = leafmap.Map(center=[0, 0], zoom=2)
m.add_geojson(data, style_callback=callback)
m.layout.height = "100px"
m

Screen Shot 2021-10-03 at 11 12 53

import ipyleaflet
m = ipyleaflet.Map(center=[0, 0], zoom=2)
m += ipyleaflet.GeoJSON(data=data, style_callback=callback)
m.layout.height = "100px"
m

Screen Shot 2021-10-03 at 11 14 14

@deeplook deeplook added the bug Something isn't working label Oct 3, 2021
giswqs added a commit that referenced this issue Oct 3, 2021
@giswqs
Copy link
Member

giswqs commented Oct 3, 2021

Thanks for reporting. This bug has been fixed. It was caused by the default style argument that overwrites the style_callback argument.

import leafmap
m = leafmap.Map(center=[0, 0], zoom=2, height="400px")
in_geojson = 'https://raw.githubusercontent.com/telegeography/www.submarinecablemap.com/master/web/public/api/v3/cable/cable-geo.json'
callback = lambda feat: {"color": feat["properties"]["color"]}
m.add_geojson(in_geojson, layer_name="Cable lines", style_callback=callback)
m

image

@giswqs giswqs closed this as completed Oct 3, 2021
@deeplook
Copy link
Collaborator Author

deeplook commented Oct 3, 2021

Thank you!! BTW, I see the same issue with geemap 0.9.2, where it seems to work for fillColor, but not color. Shall I open a ticket there, too?

@giswqs
Copy link
Member

giswqs commented Oct 3, 2021

Yes, please. Thanks.

@deeplook
Copy link
Collaborator Author

I've tested this in 0.6.0 with m.add_gdf() now which seems to behave like m.add_geojson() used to, showig only black cables for this snippet. Is there a relationship to jupyter-widgets/ipyleaflet#785 where style_callback is not yet supported for geo dataframes?

import random
import leafmap
import geopandas as gpd

m = leafmap.Map(center=[20, 0], zoom=1)
gdf = gpd.read_file('https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson')
m.add_gdf(gdf, 
    style_callback=lambda feat: {"color": random.choice(["red", "green", "black"])}
)
m

@deeplook deeplook reopened this Dec 10, 2021
@giswqs
Copy link
Member

giswqs commented Dec 10, 2021

The function has a fill_colors parameter for setting random filled colors. It works for polygons but not polylines. I will look into it.

import random
import leafmap
import geopandas as gpd

m = leafmap.Map(center=[20, 0], zoom=1)
gdf = gpd.read_file('https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson')
m.add_gdf(gdf, 
    fill_colors = ["red", "green", "black"])
m

@deeplook
Copy link
Collaborator Author

Thanks! I'm using the random colors only to keep the snippet short, but I'm more interested in a general callback that has access to the individual geo dataframe rows like the GeoJSON variant can access single features.

giswqs added a commit that referenced this issue Dec 13, 2021
@giswqs
Copy link
Member

giswqs commented Dec 13, 2021

@deeplook This bug has been fixed. Please update the package and have a try.

image

@giswqs giswqs closed this as completed Dec 13, 2021
@deeplook
Copy link
Collaborator Author

Awesome, thanks! In the meantime I've found an easy workaround, at least in cases like this:

import random
import leafmap
import geopandas as gpd

m = leafmap.Map(center=[20, 0], zoom=1)
gdf = gpd.read_file('https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable-geo.geojson')
m.add_geojson(gdf.geometry.__geo_interface__,  # convert to GeoJSON!
    style_callback=lambda feat: {"color": random.choice(["red", "green", "black"])}
)
m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants