Advice on creating a photometric RGB COG from an xarray source

Hello all, I am considering using COGs as a data visualization tool in a web application and could use some advice. We’re currently using the ESRI JavaScript mapping API, which allows for COG layers, but only photometric RGB COGs. It doesn’t have the ability to render floating point data on the fly in the web browser. I know there is the amazing CarbonPlan CMIP demo web application that renders xarray data in real-time, but this requires a server side backend. I’m hoping for a solution that doesn’t require a backend, so photometric RGB COGs in a simple map viewer seems to fit the bill, but all the rendering options will be locked in.

So, the question is: what is the most efficient workflow to go from an xarray data source to an RGB COG? There is the rio.to_raster() method, but I need to apply a colormap and vmin/vmax range to render floating point values to RBG bands. Well, perhaps RGBA to account for missing values since the data are land only and the oceans should be transparent. I could potentially use xarray.apply_ufunc to do the color interpolation myself, but I was hoping there was already a library for this workflow.

Thanks

1 Like

I would use something like this:

import odc.geo.xr # enables .odc accessor

# assuming `xx` is data array
# xx: xarray.DataArray

xx.odc.colorize(cmap='viridis', robust=True).odc.write_cog("tt.tif")

robust=True mimics xarray plotting behavior by computing vmin/vmax using percentiles , but you can also supply fixed vmin=, vmax= also.

3 Likes