Skip to content

Colormap failing to serialize for tile endpointsΒ #231

@banesullivan

Description

@banesullivan

It looks like some colormaps can easily become too large when serialized to pass through as a URL parameter. Take the following example originally reported in opengeos/leafmap#966

Create the sample data

import numpy as np
import rasterio

l = list(range(0,80,1))
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)

profile = {
    'driver': 'GTiff',
    'height': nparr.shape[0],
    'width': nparr.shape[1],
    'count': 1,  # Number of bands
    'dtype': nparr.dtype,
    'crs': 'EPSG:32633',
    'transform': GT33,
}

with rasterio.open('output.tif', 'w', **profile) as dst:
    dst.write(nparr, 1)  # Write data to the first band

Attempt plotting

Create the colormap

import matplotlib.colors as mcolors
import matplotlib.pyplot as plt

colors = ['green', 'yellow', 'orange', 'red']
cmap = mcolors.LinearSegmentedColormap.from_list('custom_cmap', colors)
cmap.set_under('grey')
cmap.set_over('grey')

download

Attempt to use with localtileserver (not that the above/below colors are unused:

import localtileserver as lts
client = lts.TileClient('output.tif')
client.tile(10, 543, 279, nodata=65535, colormap=cmap, vmin=0, vmax=10)

download

and if I try to use on a map:

m = client.get_leaflet_map()
m.zoom = 11
m.add(lts.get_leaflet_tile_layer(client, nodata=65535, colormap=cmap, vmin=0, vmax=10))
m

then I see browser network errors: 414 Request-URI Too Large

Summary

  1. above/below colors on a matplotlib LinearSegmentedColormap are not used
  2. serializing the LinearSegmentedColormap is too large for the URL parameters and silently fails

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions