gee-community / geemap

A Python package for interactive geospatial analysis and visualization with Google Earth Engine.
https://geemap.org
MIT License
3.47k stars 1.09k forks source link

ValueError: Unknown color None. when trying to use a coloramp #586

Closed serbinsh closed 3 years ago

serbinsh commented 3 years ago

Environment Information

Description

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-8010224dd26d> in <module>()
      4 vmax = visPara['max']
      5 
----> 6 Map.add_colorbar(visPara, label="Elevation (m a.s.l)", orientation="vertical", layer_name="Arctic DEM")
      7 # Map.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, caption="m a.s.l")

3 frames
/usr/local/lib/python3.7/dist-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.

What I Did

For either my geemap code or even this example (https://colab.research.google.com/github/fsn1995/GIS-at-deep-purple/blob/main/02%20gee%20map%20greenland.ipynb#scrollTo=RJdNLlhQjajw) I cant get the colorbars to display (see error above). On the other hand using the same version of geemap etc on my own computer in a Jupyter notebook running in conda it works. It seems to be some issue when I try to run the exact same code in google collab; or that's a red herring and I am doing something else wrong

Here is the code in that example that fails

greenlandmask = ee.Image('OSU/GIMP/2000_ICE_OCEAN_MASK') \
                   .select('ice_mask').eq(1); #'ice_mask', 'ocean_mask'
arcticDEM = ee.Image('UMN/PGC/ArcticDEM/V3/2m_mosaic')

arcticDEMgreenland = arcticDEM.updateMask(greenlandmask)

palette = cm.get_palette('terrain', n_class=10)

visPara = {'min': 0,  'max': 2500.0, 'palette': ['0d13d8', '60e1ff', 'ffffff']}
visPara
# visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM')
Map.setCenter(-41.0, 74.0, 3)

#add colorbar
colors = visPara['palette']
vmin = visPara['min']
vmax = visPara['max']

Map.add_colorbar(visPara, label="Elevation (m a.s.l)", orientation="vertical", layer_name="Arctic DEM")
# Map.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, caption="m a.s.l")

or

greenlandmask = ee.Image('OSU/GIMP/2000_ICE_OCEAN_MASK') \
                   .select('ice_mask').eq(1); #'ice_mask', 'ocean_mask'
arcticDEM = ee.Image('UMN/PGC/ArcticDEM/V3/2m_mosaic')

arcticDEMgreenland = arcticDEM.updateMask(greenlandmask)

palette = cm.get_palette('terrain', n_class=10)

# visPara = {'min': 0,  'max': 2500.0, 'palette': ['0d13d8', '60e1ff', 'ffffff']}
visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM terrain')
Map.setCenter(-41.0, 74.0, 3)
Map.add_colorbar(visPara, label="Elevation (m)", discrete=False, orientation="vertical", layer_name="Arctic DEM terrain")

or in my code (again works on my computer in Jupyter)

### Get GEE imagery

# DEM
elev_dataset = ee.Image('USGS/NED')
ned_elevation = elev_dataset.select('elevation')
#colors = cmr.take_cmap_colors('viridis', 5, return_fmt='hex')
#colors = cmr.take_cmap_colors('viridis', None, cmap_range=(0.2, 0.8), return_fmt='hex')
#dem_palette = cmr.take_cmap_colors(mpl.cm.get_cmap('Spectral', 30).reversed(),30, return_fmt='hex')
dem_palette = cm.get_palette('Spectral_r', n_class=30)
demViz = {'min': 0.0, 'max': 2000.0, 'palette': dem_palette, 'opacity': 1}

# LandFire
lf = ee.ImageCollection('LANDFIRE/Vegetation/EVH/v1_4_0');
lf_evh = lf.select('EVH')
#evh_palette = cmr.take_cmap_colors(mpl.cm.get_cmap('Spectral', 30).reversed(),30, return_fmt='hex')
evh_palette = cm.get_palette('Spectral_r', n_class=30)
evhViz = {'min': 0.0, 'max': 30.0, 'palette': evh_palette, 'opacity': 1}

# Global Forest Canopy Height (2005)
gfch = ee.Image('NASA/JPL/global_forest_canopy_height_2005');
forestCanopyHeight = gfch.select('1');
#gfch_palette = cmr.take_cmap_colors(mpl.cm.get_cmap('Spectral', 30).reversed(),30, return_fmt='hex')
gfch_palette = cm.get_palette('Spectral_r', n_class=30)
gfchViz = {'min': 0.0, 'max': 40.0, 'palette': gfch_palette, 'opacity': 1}

### Plot the elevation data

# Prints the elevation of Mount Everest.
xy = ee.Geometry.Point(sitecoords[:2])
site_elev = ned_elevation.sample(xy, 30).first().get('elevation').getInfo()
print('Selected site elevation (m):', site_elev)

# display NED data
# https://geemap.org/notebooks/14_legends/
# https://geemap.org/notebooks/49_colorbar/
# https://colab.research.google.com/github/fsn1995/GIS-at-deep-purple/blob/main/02%20gee%20map%20greenland.ipynb#scrollTo=uwLvDKHDlN0l
#colors = demViz['palette']
#vmin = demViz['min']
#vmax = demViz['max']

srtm = geemap.Map(center=[33.315809,-85.198609], zoom=6)
srtm.addLayer(ned_elevation, demViz, 'Elevation above sea level');
srtm.addLayer(xy, {'color': 'black', 'strokeWidth': 1}, 'Selected Site')
#srtm.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, label="Elevation (m)", 
#                         orientation="vertical", layer_name="SRTM DEM")
srtm.add_colorbar(demViz, label="Elevation (m)", layer_name="SRTM DEM")
states = ee.FeatureCollection('TIGER/2018/States')
statesImage = ee.Image().paint(states, 0, 2)
srtm.addLayer(statesImage, {'palette': 'black'}, 'US States')
srtm.addLayerControl()
srtm

here is the script on GitHub https://github.com/serbinsh/amf3_seus/blob/main/python/amf3_radar_blockage_demo_gee.ipynb

Any ideas? I was using a different colormap system (cmasher) that also worked for me but not on collab. I switched to the geemap version based on that geemap lib example in the hopes it would fix this "none" issue but it doesn't seem to have fixed it. I wonder if its a geemap versioning issue?

serbinsh commented 3 years ago

It should make a map that looks like this Screen Shot 2021-07-16 at 12 28 51 PM

serbinsh commented 3 years ago

On my computer here are my versions python 3.9

earthengine-api 0.1.273 folium 0.12.1 geemap 0.8.18

serbinsh commented 3 years ago

https://colab.research.google.com/github/fsn1995/GIS-at-deep-purple/blob/main/02%20gee%20map%20greenland.ipynb

Sorry for the spam, here is a little more info that may help

palette = cm.get_palette('terrain', n_class=10)
palette

['333399',
 '0d7fe5',
 '00be90',
 '55dd77',
 'c6f48e',
 'e3db8a',
 'aa926b',
 '8e6e67',
 'c6b6b3',
 'ffffff']

visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
visPara

{'max': 2500.0,
 'min': 0,
 'palette': ['333399',
  '0d7fe5',
  '00be90',
  '55dd77',
  'c6f48e',
  'e3db8a',
  'aa926b',
  '8e6e67',
  'c6b6b3',
  'ffffff']}

but again when trying to use that in add_colorbar

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-8771062d12f8> in <module>()
     10 Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM terrain')
     11 Map.setCenter(-41.0, 74.0, 3);
---> 12 Map.add_colorbar(visPara, label="Elevation (m)", discrete=False, orientation="vertical", layer_name="Arctic DEM terrain")

3 frames
/usr/local/lib/python3.7/dist-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.
serbinsh commented 3 years ago

I also wondered if it was a mismatch on backend with folium so I tried 49_colorbar.ipynb and Map.add_colorbar_branca

but then I get

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-6baa67c548ac> in <module>()
     13 vmax = vis_params['max']
     14 
---> 15 Map.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, layer_name="SRTM DEM")
     16 
     17 # nlcd_2016 = ee.Image('USGS/NLCD/NLCD2016').select('landcover')

AttributeError: 'Map' object has no attribute 'add_colorbar_branca'
giswqs commented 3 years ago

When creating colorbars, ipyleaflet can use either matplotlib or branca, while folium can only use branca. Map.add_colorbar() have different parameters between ipyleaflet and folium. ipyleaflet's Map.add_colorbar_branca() and folium's Map.add_colorbar() use the same function parameters. Check the link below to see function parameters.

For ipyleaflet

import ee
import geemap
import geemap.colormaps as cm

Map = geemap.Map()

greenlandmask = ee.Image('OSU/GIMP/2000_ICE_OCEAN_MASK') \
                   .select('ice_mask').eq(1); #'ice_mask', 'ocean_mask'
arcticDEM = ee.Image('UMN/PGC/ArcticDEM/V3/2m_mosaic')

arcticDEMgreenland = arcticDEM.updateMask(greenlandmask)

palette = cm.get_palette('terrain', n_class=10)

visPara = {'min': 0,  'max': 2500.0, 'palette': ['0d13d8', '60e1ff', 'ffffff']}
visPara
# visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM')
Map.setCenter(-41.0, 74.0, 3)

#add colorbar
colors = visPara['palette']
vmin = visPara['min']
vmax = visPara['max']

Map.add_colorbar(visPara, label="Elevation (m a.s.l)", orientation="vertical", layer_name="Arctic DEM")
# Map.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, caption="m a.s.l")
Map

image

For folium

import ee
import geemap.foliumap as geemap
import geemap.colormaps as cm

Map = geemap.Map()
greenlandmask = ee.Image('OSU/GIMP/2000_ICE_OCEAN_MASK') \
                   .select('ice_mask').eq(1); #'ice_mask', 'ocean_mask'
arcticDEM = ee.Image('UMN/PGC/ArcticDEM/V3/2m_mosaic')

arcticDEMgreenland = arcticDEM.updateMask(greenlandmask)

palette = cm.get_palette('terrain', n_class=10)

visPara = {'min': 0,  'max': 2500.0, 'palette': ['0d13d8', '60e1ff', 'ffffff']}
visPara
# visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM')
Map.setCenter(-41.0, 74.0, 3)

#add colorbar
colors = visPara['palette']
vmin = visPara['min']
vmax = visPara['max']

Map.add_colorbar(colors=colors, vmin=vmin, vmax=vmax, caption="m a.s.l")
Map

image

giswqs commented 3 years ago

Run geemap.update_package() and restart the kernel. Now the code below should work on both local Jupyter and Google Colab.

import ee
import geemap
import geemap.colormaps as cm

Map = geemap.Map()

greenlandmask = ee.Image('OSU/GIMP/2000_ICE_OCEAN_MASK') \
                   .select('ice_mask').eq(1); #'ice_mask', 'ocean_mask'
arcticDEM = ee.Image('UMN/PGC/ArcticDEM/V3/2m_mosaic')

arcticDEMgreenland = arcticDEM.updateMask(greenlandmask)

palette = cm.get_palette('terrain', n_class=10)

visPara = {'min': 0,  'max': 2500.0, 'palette': ['0d13d8', '60e1ff', 'ffffff']}
visPara
# visPara = {'min': 0,  'max': 2500.0, 'palette': palette}
Map.addLayer(arcticDEMgreenland, visPara, 'Arctic DEM')
Map.setCenter(-41.0, 74.0, 3)

#add colorbar
colors = visPara['palette']
vmin = visPara['min']
vmax = visPara['max']

# Map.add_colorbar(visPara, label="Elevation (m a.s.l)", orientation="vertical", layer_name="Arctic DEM")
Map.add_colorbar_branca(colors=colors, vmin=vmin, vmax=vmax, caption="m a.s.l")
Map
serbinsh commented 3 years ago

@giswqs Excellent, thank you!! and thank you for the detailed reply. I definitely think I was getting tripped up on which backend and options I was using. This is really helpful, thank you. I am going to give it a try now