Skip to content

how custom discreate colormap work with expression  #719

@satas12825

Description

@satas12825

I'm attempting to create a custom colormap for my NDVI raster, but I think I might have misunderstood something, as the results aren't quite what I expected. Could you please help me figure out what might be going wrong?

first i try
ndvi_cmap = {
-1: (0, 0, 255, 255), # Blue for non-vegetation
-0.5: (0, 255, 255, 255), # Cyan for barren land
0: (255, 255, 0, 255), # Yellow for sparse vegetation
0.2: (200, 255, 0, 255), # Light green for low vegetation
0.4: (100, 255, 0, 255), # Green for moderate vegetation
0.6: (0, 255, 0, 255), # Dark green for dense vegetation
1: (0, 128, 0, 255) # Very dark green for very dense vegetation
}

then try to rescale value to 0-255
ndvi_cmap = {
0: (0, 0, 255, 255), # Blue for non-vegetation (e.g., water)
63: (0, 255, 255, 255), # Cyan for barren land or built-up areas
127: (255, 255, 0, 255), # Yellow for sparse vegetation
153: (200, 255, 0, 255), # Light green for low vegetation
178: (100, 255, 0, 255), # Green for moderate vegetation
204: (0, 255, 0, 255), # Dark green for dense vegetation
255: (0, 128, 0, 255) # Very dark green for very dense vegetation
}

and then use colormap like this
expression="(b3-b1)/(b1+b3)"
colormap = cmap
# Register the custom NDVI colormap
colormap = colormap.register({"ndvi_cmap": ndvi_cmap})

        cm = colormap.get('ndvi_cmap')
        with Reader(url) as cog:
            img = cog.tile(x, y, z,tilesize=512,expression=expression)
            # img.rescale(in_range=((-1,1,))
            img.rescale(in_range=((-1, 1),),)
        content = img.render(colormap=cm,img_format="PNG")
        return content   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions