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

Error when trying to modify a layer's setting with it's own palette #334

Closed
lopezvoliver opened this issue Mar 3, 2021 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@lopezvoliver
Copy link
Member

Environment Information

  • geemap version: 0.8.11 (pyh44b312d_0 conda-forge)
  • Python version: 3.9.2
  • Operating System: Linux 4.15.0-136-generic x86_64 x86_64 x86_64 GNU/Linux

Description

I processed some data, then added two layers to a geemap.Map with a custom palette. I clicked on the settings button on one layer and got an error. I expected to change some settings (e.g. add the colorbar to the map).

What I Did

# simple demo to get max NDVI layer (no atm correction)
import ee
import datetime
import math

ee.Initialize()

date_str='2017-04-01'  
lonlat=[38.530867, 30.236383] 
date = ee.Date(date_str)
geom = ee.Geometry.Point(lonlat[0], lonlat[1]) # lon/lat

# Demo: only 10 days - few images 
S2_2017 =  ee.ImageCollection('COPERNICUS/S2').filterBounds(geom).filterDate(date,date.advance(10,'day')).sort('system:time_start')    

def compute_ndvi(image):
    toa = image.divide(10000.)
    ndvi = toa.normalizedDifference(['B8','B4'])
    return ndvi

# map the compute_ndvi function to the collection:
ndvi_collection = S2_2017.map(compute_ndvi)

# Reduce the collection to get maximum and stdDev:
max_ndvi=ndvi_collection.reduce(ee.Reducer.max())
std_ndvi=ndvi_collection.reduce(ee.Reducer.stdDev())
# and add them to a geemap
import geemap
Map=geemap.Map(center=[lonlat[1], lonlat[0]], zoom=10) #lat/lon

ndvi_viz = {
                'min':0,
                'max':1,
                'palette':["#d7191c","fdae61","#ffffc0","#a6d96a","#1a9641"]
                }
Map.addLayer(std_ndvi, ndvi_viz, 'Standard deviation NDVI', True, 1)
Map.addLayer(max_ndvi, ndvi_viz, 'Max NDVI', True, 1)

Map

Here is the error that I got:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/.conda/envs/gee-ksa/lib/python3.9/site-packages/ipywidgets/widgets/widget.py in _handle_msg(self, msg)
    674                 if 'buffer_paths' in data:
    675                     _put_buffers(state, data['buffer_paths'], msg['buffers'])
--> 676                 self.set_state(state)
    677 
    678         # Handle a state request.

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/ipywidgets/widgets/widget.py in set_state(self, sync_data)
    543                     from_json = self.trait_metadata(name, 'from_json',
    544                                                     self._trait_from_json)
--> 545                     self.set_trait(name, from_json(sync_data[name], self))
    546 
    547     def send(self, content, buffers=None):

~/.conda/envs/gee-ksa/lib/python3.9/contextlib.py in __exit__(self, type, value, traceback)
    122         if type is None:
    123             try:
--> 124                 next(self.gen)
    125             except StopIteration:
    126                 return False

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/traitlets/traitlets.py in hold_trait_notifications(self)
   1212                 for changes in cache.values():
   1213                     for change in changes:
-> 1214                         self.notify_change(change)
   1215 
   1216     def _notify_trait(self, name, old_value, new_value):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/ipywidgets/widgets/widget.py in notify_change(self, change)
    604                 # Send new state to front-end
    605                 self.send_state(key=name)
--> 606         super(Widget, self).notify_change(change)
    607 
    608     def __repr__(self):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/traitlets/traitlets.py in notify_change(self, change)
   1225     def notify_change(self, change):
   1226         """Notify observers of a change event"""
-> 1227         return self._notify_observers(change)
   1228 
   1229     def _notify_observers(self, event):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/traitlets/traitlets.py in _notify_observers(self, event)
   1262                 c = getattr(self, c.name)
   1263 
-> 1264             c(event)
   1265 
   1266     def _add_notifiers(self, handler, name, type):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/geemap/geemap.py in layer_vis_on_click(change)
    955                                 if self.vis_widget is not None:
    956                                     self.vis_widget = None
--> 957                                 self.vis_widget = self.create_vis_widget(layer_dict)
    958                                 if self.vis_control in self.controls:
    959                                     self.remove_control(self.vis_control)

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/geemap/geemap.py in create_vis_widget(self, layer_dict)
   3681                 if len(palette.value) > 0 and "," in palette.value:
   3682                     colors = ["#" + color.strip() for color in palette.value.split(",")]
-> 3683                     colorbar = cmap.LinearColormap(
   3684                         colors=colors,
   3685                         vmin=value_range.value[0],

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/branca/colormap.py in __init__(self, colors, index, vmin, vmax, caption)
    194         else:
    195             self.index = list(index)
--> 196         self.colors = [_parse_color(x) for x in colors]
    197 
    198     def rgba_floats_tuple(self, x):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/branca/colormap.py in <listcomp>(.0)
    194         else:
    195             self.index = list(index)
--> 196         self.colors = [_parse_color(x) for x in colors]
    197 
    198     def rgba_floats_tuple(self, x):

~/.conda/envs/gee-ksa/lib/python3.9/site-packages/branca/colormap.py in _parse_color(x)
     43         cname = _cnames.get(x.lower(), None)
     44         if cname is None:
---> 45             raise ValueError('Unknown color {!r}.'.format(cname))
     46         color_tuple = _parse_hex(cname)
     47     else:

ValueError: Unknown color None.

Here are two screenshots of the map:

image

image

@lopezvoliver lopezvoliver added the bug Something isn't working label Mar 3, 2021
@lopezvoliver
Copy link
Member Author

Note that when not using a custom palette, the error doesn't happen:

image

After modifying the layer settings:

image

giswqs added a commit that referenced this issue Mar 3, 2021
@giswqs
Copy link
Member

giswqs commented Mar 3, 2021

The "#" in your hex color code caused the problem. I just committed code to fix the bug. Update the package using geemap.update_package() and try again.

image

@giswqs giswqs closed this as completed Mar 3, 2021
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