geodesign / django-raster

Django-raster allows you to create tiled map services (TMS) and raster map algebra end points for web maps. It is Python-based, and requires GeoDjango with a PostGIS backend.
BSD 3-Clause "New" or "Revised" License
96 stars 39 forks source link

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

Open sebbelese opened 3 years ago

sebbelese commented 3 years ago

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 commented 2 years ago

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!