folium icon indicating copy to clipboard operation
folium copied to clipboard

ImageOverlay arg colormap can't take a branca.colormap.LinearColormap() as input

Open Joueswant opened this issue 5 years ago • 2 comments

Hi guys, I am not pretty sure if that's the best place to ask, since I am not sure I found a bug or it is just me that I am a blockhead. Point is, I have a raster to plot in Folium, and I want to define my own color map (based on std. deviation, like in QGis). The raster is just a np.array. Folium can plot the map as long as I use a matplotlib cmap, but when I create my own with Branca module, it fails...

#Custom cmap
cmap = branca.colormap.LinearColormap(cmap_colors, vmin=vmin, vmax=vmax).to_step(cmap_levels)
...

#Folium 
folium.raster_layers.ImageOverlay(weigths_diff[0].data.astype('float'), bounds = bbox, name = 'First Scenario', opacity = .5, colormap = lambda x: cmap(x)).add_to(map_layer)

The error I get looks like the following:

./folium/utilities.py in image_to_url(image, colormap, origin)
    136         url = 'data:image/{};base64,{}'.format(fileformat, b64encoded)
    137     elif 'ndarray' in image.__class__.__name__:
--> 138         img = write_png(image, origin=origin, colormap=colormap)
    139         b64encoded = base64.b64encode(img).decode('utf-8')
    140         url = 'data:image/png;base64,{}'.format(b64encoded)

./folium/utilities.py in write_png(data, origin, colormap)
    199     if nblayers == 1:
    200         arr = np.array(list(map(colormap, arr.ravel())))
--> 201         nblayers = arr.shape[1]
    202         if nblayers not in [3, 4]:
    203             raise ValueError('colormap must provide colors of r'

IndexError: tuple index out of range

Using the lambda function I expect folium to plot the weights of the raster according to the cmap function.

Folium version: '0.10.1' Branca version: '0.4.0' Python: 3.7

P.S. I have also tried to input the the cmap() as input (like if it was a matplotlib cmap) didn't work either, Folium returns the same error as described above

#Folium 
folium.raster_layers.ImageOverlay(weigths_diff[0].data.astype('float'), bounds = bbox, name = 'First Scenario', opacity = .5, colormap = lambda x: cmap(x)).add_to(map_layer)

Joueswant avatar Mar 31 '20 21:03 Joueswant

I have been debbuging a bit your utilities.write_png() function:

    if nblayers == 1:
        arr = np.array(list(map(colormap, arr.ravel())))
        nblayers = arr.shape[1]

Why the index 1 of shape, if you ravel() the input weights you get a linear array (that's why the numpy IndexError), shouldn't it be reshaped before?

Joueswant avatar Mar 31 '20 22:03 Joueswant

I'm also getting this issue.

A simple colormap=matplotlib.colormap.<your_colormap> would work. But it's not as easily customisable.

I'm trying to set the min and max of the scale so that the nodata value (-9999.0) in my raster does not effect the rest of the scale. The above doesn't fix that without remaking the entire colourmap.

Just joining your issue thread for a solution.

Instatarama avatar May 17 '20 07:05 Instatarama

This has been addressed, see https://github.com/python-visualization/folium/issues/1571#issuecomment-1400081656

Conengmo avatar Jan 24 '23 17:01 Conengmo