Skip to content

rio_tiler.colormap

rio_tiler.colormap

rio-tiler colormap functions and classes.

ColorMaps

Default Colormaps holder.

Attributes:

  • data (dict) –

    colormaps. Defaults to rio_tiler.colormap.DEFAULTS_CMAPS.

get

get(name: str) -> ColorMapType

Fetch a colormap.

Parameters:

  • name (str) –

    colormap name.

Returns dict: colormap dictionary.

list

list() -> List[str]

List registered Colormaps.

Returns list: list of colormap names.

register

register(custom_cmap: Dict[str, Union[str, Path, ColorMapType]], overwrite: bool = False) -> ColorMaps

Register a custom colormap.

Parameters:

  • custom_cmap (dict) –

    custom colormap(s) to register.

  • overwrite (bool, default: False ) –

    Overwrite existing colormap with same key. Defaults to False.

Examples:

>>> cmap = cmap.register({"acmap": {0: (0, 0, 0, 0), ...}})
>>> cmap = cmap.register({"acmap": "acmap.npy"})

_remove_value

_remove_value(cmap: GDALColorMapType, idx: Sequence[int]) -> None

Remove value from a colormap dict.

_update_alpha

_update_alpha(cmap: GDALColorMapType, idx: Sequence[int], alpha: int = 0) -> None

Update the alpha value of a colormap index.

_update_cmap

_update_cmap(cmap: GDALColorMapType, values: GDALColorMapType) -> None

Update a colormap dict.

apply_cmap

apply_cmap(data: ndarray, colormap: ColorMapType) -> DataMaskType

Apply colormap on data.

Parameters:

  • data (ndarray) –

    1D image array to translate to RGB.

  • colormap (dict or sequence) –

    GDAL RGBA Color Table dictionary or sequence (for intervals).

Returns:

  • tuple ( DataMaskType ) –

    Data (numpy.ndarray) and Mask (numpy.ndarray) values.

Raises:

  • InvalidFormat

    If data is not a 1 band dataset (1, col, row).

apply_discrete_cmap

apply_discrete_cmap(data: ndarray, colormap: Union[GDALColorMapType, DiscreteColorMapType]) -> DataMaskType

Apply discrete colormap.

Parameters:

  • data (ndarray) –

    1D image array to translate to RGB.

  • colormap (GDALColorMapType or DiscreteColorMapType) –

    Discrete ColorMap dictionary.

Returns:

  • tuple ( DataMaskType ) –

    Data (numpy.ndarray) and Alpha band (numpy.ndarray).

Examples:

>>> data = numpy.random.randint(0, 3, size=(1, 256, 256))
    cmap = {
        0: (0, 0, 0, 0),
        1: (255, 255, 255, 255),
        2: (255, 0, 0, 255),
        3: (255, 255, 0, 255),
    }
    data, mask = apply_discrete_cmap(data, cmap)
    assert data.shape == (3, 256, 256)

apply_intervals_cmap

apply_intervals_cmap(data: ndarray, colormap: IntervalColorMapType) -> DataMaskType

Apply intervals colormap.

Parameters:

  • data (ndarray) –

    1D image array to translate to RGB.

  • colormap (IntervalColorMapType) –

    Sequence of intervals and color in form of [([min, max], [r, g, b, a]), ...].

Returns:

  • tuple ( DataMaskType ) –

    Data (numpy.ndarray) and Alpha band (numpy.ndarray).

Examples:

>>> data = numpy.random.randint(0, 3, size=(1, 256, 256))
    cmap = [
        ((0, 1), (0, 0, 0, 0)),
        ((1, 2), (255, 255, 255, 255)),
        ((2, 3), (255, 0, 0, 255)),
        ((3, 4), (255, 255, 0, 255)),
    ]
data, mask = apply_intervals_cmap(data, cmap)
assert data.shape == (3, 256, 256)

make_lut

make_lut(colormap: GDALColorMapType) -> ndarray

Create a lookup table numpy.ndarray from a GDAL RGBA Color Table dictionary.

Parameters:

  • colormap (dict) –

    GDAL RGBA Color Table dictionary.

Returns:

  • ndarray

    numpy.ndarray: colormap lookup table.

parse_color

parse_color(rgba: Union[Sequence[int], str]) -> Tuple[int, int, int, int]

Parse RGB/RGBA color and return valid rio-tiler compatible RGBA colormap entry.

Parameters:

  • rgba (str or list of int) –

    HEX encoded or list RGB or RGBA colors.

Returns:

Examples:

>>> parse_color("#FFF")
(255, 255, 255, 255)
>>> parse_color("#FF0000FF")
(255, 0, 0, 255)
>>> parse_color("#FF0000")
(255, 0, 0, 255)
>>> parse_color([255, 255, 255])
(255, 255, 255, 255)