Skip to content

Commit

Permalink
Fix COG custom colormap bug (#868)
Browse files Browse the repository at this point in the history
* Fix COG custom colormap bug

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
giswqs and pre-commit-ci[bot] authored Aug 7, 2024
1 parent 8e34c51 commit 08e2475
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
69 changes: 54 additions & 15 deletions docs/notebooks/03_cog_stac.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -241,7 +280,7 @@
},
{
"cell_type": "markdown",
"id": "23",
"id": "25",
"metadata": {},
"source": [
"Create an interactive map."
Expand All @@ -250,7 +289,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "24",
"id": "26",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -260,7 +299,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "25",
"id": "27",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -269,7 +308,7 @@
},
{
"cell_type": "markdown",
"id": "26",
"id": "28",
"metadata": {},
"source": [
"Retrieve the bounding box coordinates of the STAC file."
Expand All @@ -278,7 +317,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "27",
"id": "29",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -287,7 +326,7 @@
},
{
"cell_type": "markdown",
"id": "28",
"id": "30",
"metadata": {},
"source": [
"Retrieve the centroid coordinates of the STAC file."
Expand All @@ -296,7 +335,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "29",
"id": "31",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -305,7 +344,7 @@
},
{
"cell_type": "markdown",
"id": "30",
"id": "32",
"metadata": {},
"source": [
"Retrieve the band names of the STAC file."
Expand All @@ -314,7 +353,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "31",
"id": "33",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -323,7 +362,7 @@
},
{
"cell_type": "markdown",
"id": "32",
"id": "34",
"metadata": {},
"source": [
"Retrieve the tile layer URL of the STAC file."
Expand All @@ -332,7 +371,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "33",
"id": "35",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -341,7 +380,7 @@
},
{
"cell_type": "markdown",
"id": "34",
"id": "36",
"metadata": {},
"source": [
"Add a STAC layer to the map."
Expand All @@ -350,7 +389,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "35",
"id": "37",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -360,7 +399,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "36",
"id": "38",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -370,7 +409,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "37",
"id": "39",
"metadata": {},
"outputs": [],
"source": [
Expand Down
6 changes: 5 additions & 1 deletion leafmap/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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"]
Expand Down

0 comments on commit 08e2475

Please sign in to comment.