diff --git a/docs/notebooks/03_cog_stac.ipynb b/docs/notebooks/03_cog_stac.ipynb index 4ff6de6840..545ca2f742 100644 --- a/docs/notebooks/03_cog_stac.ipynb +++ b/docs/notebooks/03_cog_stac.ipynb @@ -227,6 +227,45 @@ "cell_type": "markdown", "id": "22", "metadata": {}, + "source": [ + "Use custom colormap." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "23", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://clarkcga-aquaculture.s3.amazonaws.com/data/el_salvador/cover/El_Salvador_Landcover_2022.tif\"\n", + "custom_cmap = {\n", + " \"0\": \"#000000\",\n", + " \"1\": \"#008040\",\n", + " \"2\": \"#ff0000\",\n", + " \"3\": \"#ffff00\",\n", + " \"4\": \"#8000ff\",\n", + " \"5\": \"#8080ff\",\n", + " \"6\": \"#00ff00\",\n", + " \"7\": \"#c0c0c0\",\n", + " \"8\": \"#16002d\",\n", + " \"9\": \"#ff80ff\",\n", + " \"10\": \"#b3ffb3\",\n", + " \"11\": \"#ff8080\",\n", + " \"12\": \"#ffffbf\",\n", + " \"13\": \"#000080\",\n", + " \"14\": \"#808000\",\n", + " \"15\": \"#00ffff\",\n", + "}\n", + "m = leafmap.Map()\n", + "m.add_cog_layer(url, colormap=custom_cmap, name=\"El_Salvador\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "24", + "metadata": {}, "source": [ "**Working with SpatioTemporal Asset Catalog (STAC)**\n", "\n", @@ -241,7 +280,7 @@ }, { "cell_type": "markdown", - "id": "23", + "id": "25", "metadata": {}, "source": [ "Create an interactive map." @@ -250,7 +289,7 @@ { "cell_type": "code", "execution_count": null, - "id": "24", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -260,7 +299,7 @@ { "cell_type": "code", "execution_count": null, - "id": "25", + "id": "27", "metadata": {}, "outputs": [], "source": [ @@ -269,7 +308,7 @@ }, { "cell_type": "markdown", - "id": "26", + "id": "28", "metadata": {}, "source": [ "Retrieve the bounding box coordinates of the STAC file." @@ -278,7 +317,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "29", "metadata": {}, "outputs": [], "source": [ @@ -287,7 +326,7 @@ }, { "cell_type": "markdown", - "id": "28", + "id": "30", "metadata": {}, "source": [ "Retrieve the centroid coordinates of the STAC file." @@ -296,7 +335,7 @@ { "cell_type": "code", "execution_count": null, - "id": "29", + "id": "31", "metadata": {}, "outputs": [], "source": [ @@ -305,7 +344,7 @@ }, { "cell_type": "markdown", - "id": "30", + "id": "32", "metadata": {}, "source": [ "Retrieve the band names of the STAC file." @@ -314,7 +353,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "33", "metadata": {}, "outputs": [], "source": [ @@ -323,7 +362,7 @@ }, { "cell_type": "markdown", - "id": "32", + "id": "34", "metadata": {}, "source": [ "Retrieve the tile layer URL of the STAC file." @@ -332,7 +371,7 @@ { "cell_type": "code", "execution_count": null, - "id": "33", + "id": "35", "metadata": {}, "outputs": [], "source": [ @@ -341,7 +380,7 @@ }, { "cell_type": "markdown", - "id": "34", + "id": "36", "metadata": {}, "source": [ "Add a STAC layer to the map." @@ -350,7 +389,7 @@ { "cell_type": "code", "execution_count": null, - "id": "35", + "id": "37", "metadata": {}, "outputs": [], "source": [ @@ -360,7 +399,7 @@ { "cell_type": "code", "execution_count": null, - "id": "36", + "id": "38", "metadata": {}, "outputs": [], "source": [ @@ -370,7 +409,7 @@ { "cell_type": "code", "execution_count": null, - "id": "37", + "id": "39", "metadata": {}, "outputs": [], "source": [ diff --git a/leafmap/stac.py b/leafmap/stac.py index 00b65965a2..3b9628ba6e 100644 --- a/leafmap/stac.py +++ b/leafmap/stac.py @@ -146,6 +146,7 @@ def cog_tile( Returns: tuple: Returns the COG Tile layer URL and bounds. """ + import json titiler_endpoint = check_titiler_endpoint(titiler_endpoint) @@ -180,7 +181,7 @@ def cog_tile( elif isinstance(kwargs["bidx"], int): kwargs["bidx"] = [kwargs["bidx"]] - if "rescale" not in kwargs: + if "rescale" not in kwargs and ("colormap" not in kwargs): stats = cog_stats(url, titiler_endpoint) if "message" not in stats: @@ -197,6 +198,9 @@ def cog_tile( except Exception as e: pass + if "colormap" in kwargs and isinstance(kwargs["colormap"], dict): + kwargs["colormap"] = json.dumps(kwargs["colormap"]) + TileMatrixSetId = "WebMercatorQuad" if "TileMatrixSetId" in kwargs.keys(): TileMatrixSetId = kwargs["TileMatrixSetId"]