Issue rendering singleband image with colormap via stac endpoint #235
-
In particular, if I follow the rasterio example and create a "colormap.tif". Loading the image directly using the cog endpoint works just fine. However, if I create a STAC item and use the stac endpoints, it appears to ignore the internal colormap. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Thanks @aliasmrchips for the question. when you are using the titiler/titiler/endpoints/factory.py Lines 320 to 322 in a95073d ☝️ in this, if the For If the assets in your items do not need to be merged (which is what we do when multiple bands are stored as multiple assets), I would advise to switch to the Another solution would be to register a custom colormap (if the colormap in your data never change) and to use it to colorize your result
Note: I'll update the docs to clearly explain how to create/register a custom colormap |
Beta Was this translation helpful? Give feedback.
-
OR you could create your own import attr
from rasterio.io import DatasetReader
from rio_tiler.io.stac import fetch, _to_pystac_item
from rio_tiler.io import COGReader
import pystac
@attr.s
class CustomSTACReader(COGReader):
"""Custom COG Reader with GCPS support."""
# This will keep the STAC item info within the instance
item: pystac.Item = attr.ib(default=None, init=False)
def __attrs_post_init__(self):
"""Define _kwargs, open dataset and get info."""
# get STAC item URL and asset name
asset = self.filepath.split(":")[-1]
stac_url = self.filepath.replace(f":{asset}", "")
# Fetch the STAC item
self.item = pystac.Item.from_dict(fetch(stac_url), stac_url)
# Get asset url from the STAC Item
self.filepath = self.item.assets[asset].get_absolute_href()
super().__attrs_post_init__()
with CustomSTACReader("https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json:pan") as cog:
print(cog.bounds)
>>> (-111.87793996076493, 60.48627186654449, -109.94924666908423, 61.42036313093244) then just create a custom tiler using the tiler factory from titiler.endpoints.factory import TilerFactory
stac = TileFactory(reader=CustomSTACReader) |
Beta Was this translation helpful? Give feedback.
-
FYI, in 0.2.0 we added custom colormap in the query parameter
|
Beta Was this translation helpful? Give feedback.
Thanks @aliasmrchips for the question.
You are right, the STACReader (used in the /stac) endpoint is loosing the information about the internal colormap of the item. This is by design and sadly there isn't an easy way of fixing this.
when you are using the
/cog
endpoint, you are using https://github.com/cogeotiff/rio-tiler/blob/master/rio_tiler/io/cogeo.py#L28 andtitiler/titiler/endpoints/factory.py
Lines 320 to 322 in a95073d
☝️ in this, if the
COGReader
instance has a colormap variable, it will be used by default, and that's what you are getting.For
/stac
, we are using a specific read…