gee-community/geemap

improve error message when validating vis_params

rodrigo-j-goncalves opened this issue · 2 comments

Environment Information


          Date : Thu Oct 12 17:37:37 2023 CEST
            OS : Linux
        CPU(s) : 24
       Machine : x86_64
  Architecture : 64bit
           RAM : 62.5 GiB
   Environment : Python
   File system : ext4

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]

        geemap : 0.27.1
            ee : 0.1.370
    ipyleaflet : 0.17.4
        folium : 0.14.0
    jupyterlab : 4.0.7
      notebook : Module not found
     ipyevents : 2.0.1
     geopandas : 0.14.0
localtileserver : 0.7.1

Description

I'm adding a layer of a single-band raster file.
This includes the following code:


1. Map = geemap.Map()
2. Map.add_raster(filename, cmap='terrain', layer_name="DEM")
3. vis_params = {'min': 0, 'max': 4000, 'palette': 'terrain'}
4. Map.add_colorbar(vis_params, label='Elevation (m)')
5. Map

However, since I want to automate this task, I want to use the raster max and min values instead of coding the numbers by hand. Thus, I used variables rastermin and rastermax I got from the raster statistics:

3. vis_params = {'min': rastermin, 'max': rastermax, 'palette': 'terrain'}

But I get an error: TypeError: The provided min value must be scalar type.
However, the variables are in fact scalar. When inspecting the code a little deeper I note that map_widgets.py only asks if the max and min variables are either int or float:

83   vmin = vis_params.get("min", kwargs.pop("vmin", 0))
84   if type(vmin) not in (int, float):
85        raise TypeError("The provided min value must be scalar type.")

My min and max variables are scalar, just a different type of float:

type(rastermin)
numpy.float32

And to confirm that this is the problem, this works fine:
vis_params = {'min': float(rastermin), 'max': float(rastermax), 'palette': 'terrain'}

Conclusion

I see two alternatives

  1. Change the error message:
    To match the behavior of the code and change the error message to
    TypeError: The provided min/max values can only be 'int' or 'float'.

  2. Allow other types:
    Allow float32 (and possible other numpy types?). Maybe this could be done by forcing the conversion to int or float as I did above.



Acknowledgement

Thanks a lot for this great library!

Good suggestion! I would encourage you to submit a pull request so that you can become a contributor to the project.

Fix in #1782