Can add_raster work with a matplotlib.colors.LinearSegmentedColormap? #966
-
Has anyone succeeded in feeding a matplotlib.colors.LinearSegmentedColormap to add_raster(), or know that it's not supported? I need to display a raster image of continuous data (in a numpy array) as reclassified into danger levels green-yellow-orange-red. The colours vary largely in value interval size, so an even-spaced matplotlib.colors.ListedColormap won't do. Unfortunately the two types have rather different formulation, and add_raster fails to produce the colours I expect. There is no crash or error. Of course I could resample the numpy array, but I am letting the user browse through a long time series of maps, so would prefer not. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Try updating Localtileserver to the latest version. It should work. See banesullivan/localtileserver#223 (comment) |
Beta Was this translation helpful? Give feedback.
-
Or try add_cog_layer if the raster is available through http https://geog-312.gishub.org/book/geospatial/leafmap.html#using-a-custom-colormap |
Beta Was this translation helpful? Give feedback.
-
You're close! What I recommend is starting simple with a plot in something like This is what I have: import os, numpy as np
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
# Create the raster to plot:
l = list(range(0,80,1)) # Values to be classified into green, yellow, orange, red danger level
l.extend([65535]*10) # 65535 means missing data
l.extend([65533]*10) # 65533 is another special value
nparr = np.array(l, dtype=np.float32)
nparr[nparr<65000] /= 10
nparr = nparr.reshape([10,10])
GT33 = (1000.0,0.0,300000.0,0.0,-1000.0,7000000.0)
nparr
# So what we have here are valid clim ~0-10, 65535 is NaN, and otherwise outside of bounds = grey
colors = ['green', 'yellow', 'orange', 'red']
cmap = mcolors.LinearSegmentedColormap.from_list('custom_cmap', colors)
cmap.set_under('grey')
cmap.set_over('grey')
cmap.set_bad((0, 0, 0, 0)) # set no data as transparent
im = nparr.copy()
im[np.where(im == 65535)] = np.nan # this is what `nodata` does in localtileserver
plt.imshow(im, cmap=cmap, clim=(0,10))
plt.colorbar() Now we have a colormap built properly. We should be able to pass this directly to localtileserver for geospatial visualization too. However, this colormap isn't serializing nicely for localtilerver and its unable to display. I'm going to open an issue in localtileserver for this and see what I can do to resolve it. |
Beta Was this translation helpful? Give feedback.
-
I found a workaround. Writing a colormap to the image in the memory. Localtileserver will automatically use the colormap in the image to render. See the example: https://leafmap.org/notebooks/103_raster_colormap/ |
Beta Was this translation helpful? Give feedback.
You're close! What I recommend is starting simple with a plot in something like
matplotlib
. This will help you properly create the colormap without navigating other bugs or oddities of the more complicated geospatial stack like localtilserver.This is what I have: