localtileserver
localtileserver copied to clipboard
add support for custom matplotlib colormap
When displaying single band imagery specifically categorical maps, sometimes you need a very specific color classification with value-color pairs. In the current implementation, only a list of hex colors or a colormap name can be used. would you like to add support for customized matplotlib colormap?
Should be easy enough!
I believe a custom colormap could be converted to what is currently supported with this:
import matplotlib.colors as mcolors
cmap = ... # custom MPL colormap
color_list = [mcolors.rgb2hex(cmap(i)) for i in range(cmap.N)]
I will add support for any matplotlib.colors.Colormap
subclass
from ipyleaflet import Map
from localtileserver import examples
from localtileserver import TileClient, get_leaflet_tile_layer
import matplotlib
import matplotlib.colors as mcolors
b_client = examples.get_bahamas()
cmap = matplotlib.cm.get_cmap('jet', 5)
color_list = [mcolors.rgb2hex(cmap(i)) for i in range(cmap.N)]
# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(b_client, cmap=color_list)
# Create ipyleaflet map, add tile layer, and display
m = Map(center=b_client.center(), zoom=b_client.default_zoom)
m.add_layer(t)
m
sounds about right. So you "export" all the values contained in the matplotlib colormap. I guess that in my use case I'll need to be extra careful wih vmin and vmax to make sure that values are corresponding right ?
Yes, we have to âexportâ the colors like this and yes, youâll need to be concious of the vmin/vmax.
Iâm curious⌠how are you defining your colormaps? Do they have a range associated with them? If you could share, I may be able to implement something that would handle all this under the hood.
I need to find back a meaningful example, let me check in my files and I'll share something. In the meantime I've implemented your suggestion
@12rambau, following up on this: do you have an example you could share? I'd like to support this either here or directly in large-image
At this time, I don't think I'll be able to add support for this. I have completely redesigned localtileserver from the ground up to now entirely rely on rasterio and rio-tiler and it seems the infrastucture is best suited for named matplotlib colormaps.
PRs welcome in the future to implement this, but I don't have the bandwidth.
One solution could be to create your own package of colormaps and register them with MPL in the environment (like cmocean) then refer to them by name