Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous colormaps - error trying to convert 'continuous' expression code to color value #56

Open
sebbelese opened this issue Feb 19, 2021 · 1 comment

Comments

@sebbelese
Copy link

Hi,

I have an issue with the use of programmatically created legends, but I am not sure if this is a bug or a misuse of my part.

First, I create programmatically a grayscale legend:

        legendGrayscale = Legend.objects.filter(title='grayscale').first()
        if legendGrayscale is None:
            LegendEntry.objects.all().delete()
            LegendSemantics.objects.all().delete()
            Legend.objects.all().delete()
            legendGrayscale = Legend.objects.create(title='grayscale', description='White to black continuous')
            fromSemantics = LegendSemantics.objects.create(name='from')
            toSemantics = LegendSemantics.objects.create(name='to')
            continuousSemantics = LegendSemantics.objects.create(name='continuous')
            fromEntry = LegendEntry.objects.create(semantics=fromSemantics, legend=legendGrayscale, expression='from', color='#FFFFFF', code='#FFFFFF')
            toEntry = LegendEntry.objects.create(semantics=toSemantics, legend=legendGrayscale, expression='to', color='#000000', code='#000000')
            continuousEntry = LegendEntry.objects.create(semantics=continuousSemantics, legend=legendGrayscale, expression='continuous', code='True')

But when I try to render tiles, it complains that the colors cannot be converted from hex to rgb. This is due to the colormap function in models.py where it tries to convert the color associated with the continuous expression to a rgb color:

    @property
    def colormap(self):
        legend = json.loads(self.json)
        cmap = {}
        for leg in legend:
            cmap[leg['expression']] = hex_to_rgba(leg['color'])
        return cmap

My workaround was to update the colormap function, but I am not sure if this was the actual issue, or if my legend was not correctly created.

    @property
    def colormap(self):
        legend = json.loads(self.json)
        cmap = {}
        for leg in legend:
            if leg['expression'] == 'continuous':
                cmap[leg['expression']] = leg['code']
            else:
                cmap[leg['expression']] = hex_to_rgba(leg['color'])
        return cmap
@henhuy
Copy link

henhuy commented Sep 15, 2021

Same here!
Wanted to set legend json to continuous colormap like in example on https://django-raster.readthedocs.io/en/latest/tms.html#continuous-color-schemes - but as @sebbelese pointed out, implementation of colormap function in legend only supports discrete color maps.
It would be nice, if legend.colormap would make use of colormap_to_rgba!
For now, I will implement your solution @sebbelese - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants