opendatacube / odc-geo

GeoBox and geometry utilities extracted from datacube-core
https://odc-geo.readthedocs.io/en/latest/
Apache License 2.0
83 stars 15 forks source link

Support passing in `cmap` and `norm` for the `odc.geo.xr.ODCExtension.explore` function #137

Open alexgleith opened 7 months ago

alexgleith commented 7 months ago

Hey folks

Not sure if this is expected to work, but I have a nice colourmap for a categorical plot, example here.

Code is something like this:

from matplotlib import colors

classes = [
    [1, "bare_land", "#968640"],
    [2, "forest", "#064a00"],
    [3, "crops", "#ffce33"],
    [4, "grassland", "#d7ffa0"],
    [5, "settlements", "#b3b2ae"],
    [6, "mangroves", "#07b28d"],
    [7, "water", "#71a8ff"],
    [8, "quarry", "#b03a2e"]
]

values_list = [c[0] for c in classes]
color_list = [c[2] for c in classes]

# Build a listed colormap.
c_map = colors.ListedColormap(color_list)
bounds = values_list + [9]
norm = colors.BoundaryNorm(bounds, c_map.N)

data["class"].isel(time=0).plot.imshow(cmap=c_map, norm=norm, size=10)

Would it be possible for the amazing explore() function to be able to render with these colourmaps? Currently it fails with TypeError: Object of type BoundaryNorm is not JSON serializable

Kirill888 commented 7 months ago

It's actually an issue with mapping function itself, it doesn't know of norm= so passes it on to folium/ipyleaflet, where it tries to push it to JavaScript, hence the error.

So these need to be augmented to understand norm= parameter:

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/_rgba.py#L174

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/_rgba.py#L204-L206

https://github.com/opendatacube/odc-geo/blob/1f8885490d8836f6dd6ad09640c639ee02c003f7/odc/geo/_map.py#L54-L65