Skip to content

Commit

Permalink
Handle serialization and deserialization of Colormap and lists of col…
Browse files Browse the repository at this point in the history
…ors.
  • Loading branch information
lopezvoliver committed Sep 12, 2024
1 parent df43e54 commit 0362bfb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
17 changes: 14 additions & 3 deletions localtileserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import pathlib
from typing import List, Optional, Union
from matplotlib.colors import Colormap

import rasterio
import requests
Expand Down Expand Up @@ -416,7 +417,7 @@ def create_url(self, path: str, client: bool = False):
def get_tile_url(
self,
indexes: Optional[List[int]] = None,
colormap: Optional[str] = None,
colormap: Optional[Union[str, Colormap]] = None,
vmin: Optional[Union[float, List[float]]] = None,
vmax: Optional[Union[float, List[float]]] = None,
nodata: Optional[Union[int, float]] = None,
Expand All @@ -441,13 +442,23 @@ def get_tile_url(
The value from the band to use to interpret as not valid data.
"""
import json
# First handle query parameters to check for errors
params = {}
if indexes is not None:
params["indexes"] = indexes
if colormap is not None:
# make sure palette is valid
palette_valid_or_raise(colormap)

if isinstance(colormap, Colormap):
colormap = json.dumps(
{k:tuple(v.tolist()) for k,v in enumerate(colormap(range(256),1,1))}
)
elif isinstance(colormap, list):
colormap = json.dumps(colormap)
else:
# make sure palette is valid
palette_valid_or_raise(colormap)

params["colormap"] = colormap
if vmin is not None:
if isinstance(vmin, Iterable) and not isinstance(indexes, Iterable):
Expand Down
16 changes: 15 additions & 1 deletion localtileserver/tiler/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,21 @@ def _render_image(
colormap: Optional[str] = None,
img_format: str = "PNG",
):
colormap = cmap.get(colormap) if colormap else None
import json
from matplotlib.colors import LinearSegmentedColormap

if colormap in cmap.list():
colormap = cmap.get(colormap)
else:
c = json.loads(colormap)
if isinstance(c, list):
c = LinearSegmentedColormap.from_list('', c, N=256)
colormap = {k:tuple(v) for k,v in enumerate(c(range(256),1,1))}
else:
colormap = {}
for key,value in c.items():
colormap[int(key)] = tuple(value)

if (
not colormap
and len(indexes) == 1
Expand Down
3 changes: 2 additions & 1 deletion localtileserver/widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import pathlib
from typing import List, Optional, Union
from matplotlib.colors import Colormap
import warnings

import rasterio
Expand All @@ -23,7 +24,7 @@ def get_leaflet_tile_layer(
port: Union[int, str] = "default",
debug: bool = False,
indexes: Optional[List[int]] = None,
colormap: Optional[str] = None,
colormap: Optional[Union[str, Colormap]] = None,
vmin: Optional[Union[float, List[float]]] = None,
vmax: Optional[Union[float, List[float]]] = None,
nodata: Optional[Union[int, float]] = None,
Expand Down

0 comments on commit 0362bfb

Please sign in to comment.