Skip to content

Commit

Permalink
Handle colormaps instances
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Oct 4, 2024
1 parent 044540c commit d670afe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
6 changes: 4 additions & 2 deletions localtileserver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
from typing import List, Optional, Union

from matplotlib.colors import Colormap
from matplotlib.colors import Colormap, ListedColormap
import rasterio
import requests
from rio_tiler.io import Reader
Expand Down Expand Up @@ -448,7 +448,9 @@ def get_tile_url(
if indexes is not None:
params["indexes"] = indexes
if colormap is not None:
if isinstance(colormap, Colormap):
if isinstance(colormap, ListedColormap):
colormap = json.dumps([c for c in colormap.colors])
elif isinstance(colormap, Colormap):
colormap = json.dumps(
{k: tuple(v.tolist()) for k, v in enumerate(colormap(range(256), 1, 1))}
)
Expand Down
13 changes: 8 additions & 5 deletions localtileserver/tiler/handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Methods for working with images."""
import json
import pathlib
from typing import Dict, List, Optional, Tuple, Union

from matplotlib.colors import Colormap, LinearSegmentedColormap, ListedColormap
import numpy as np
import rasterio
from rasterio.enums import ColorInterp
Expand Down Expand Up @@ -150,13 +152,14 @@ def _render_image(
colormap: Optional[str] = None,
img_format: str = "PNG",
):
import json

from matplotlib.colors import LinearSegmentedColormap

if colormap in cmap.list():
colormap = cmap.get(colormap)
elif colormap:
elif isinstance(colormap, ListedColormap):
c = LinearSegmentedColormap.from_list("", colormap.colors, N=256)
colormap = {k: tuple(v) for k, v in enumerate(c(range(256), 1, 1))}
elif isinstance(colormap, Colormap):
colormap = {k: tuple(v) for k, v in enumerate(colormap(range(256), 1, 1))}
else:
c = json.loads(colormap)
if isinstance(c, list):
c = LinearSegmentedColormap.from_list("", c, N=256)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d670afe

Please sign in to comment.