Exemplo n.º 1
0
 def test_rgba_colormap_to_rgba(self):
     colormap = {
         0: (0, 0, 0, 0),
         1: (0, 255, 0, 255),
         2: (255, 0, 0, 255),
         3: (0, 0, 255, 255)
     }
     self.assertEqual(colormap, colormap_to_rgba(colormap))
Exemplo n.º 2
0
 def test_rgba_colormap_to_rgba(self):
     colormap = {
         0: (0, 0, 0, 0),
         1: (0, 255, 0, 255),
         2: (255, 0, 0, 255),
         3: (0, 0, 255, 255)
     }
     self.assertEqual(colormap, colormap_to_rgba(colormap))
Exemplo n.º 3
0
    def get_colormap(self, layer=None):
        """
        Returns colormap from request and layer, looking for a colormap in the
        request or session, a custom legend name to construct the legend or the
        default colormap from the layer legend.
        """
        if 'colormap' in self.request.GET:
            colormap = colormap_to_rgba(
                json.loads(self.request.GET['colormap']))
            # Ensure colormap range is in float format.
            if 'range' in colormap:
                colormap['range'] = (float(colormap['range'][0]),
                                     float(colormap['range'][1]))
        elif 'legend' in self.request.GET:
            store = self.request.GET.get('store', 'database')
            if store == 'session':
                colormap = get_session_colormap(self.request.session,
                                                self.request.GET['legend'])
            else:
                legend_input = self.request.GET['legend']
                try:
                    legend_input = int(legend_input)
                except ValueError:
                    pass

                # Try to get legend by id, name or from input layer
                if isinstance(legend_input, int):
                    legend = get_object_or_404(Legend, id=legend_input)
                else:
                    legend = Legend.objects.filter(
                        title__iexact=legend_input).first()
                colormap = legend.colormap

        elif layer and hasattr(layer.legend, 'colormap'):
            colormap = layer.legend.colormap
        else:
            # Use a continous grayscale color scheme.
            colormap = {
                'continuous': True,
                'from': (0, 0, 0),
                'to': (255, 255, 255),
            }

        # Filter by custom entries if requested
        if colormap and 'entries' in self.request.GET:
            entries = self.request.GET['entries'].split(',')
            colormap = {
                k: v
                for (k, v) in colormap.items() if str(k) in entries
            }

        # Add layer level value range to colormap if it was not provided manually.
        if layer and colormap and 'continuous' in colormap and 'range' not in colormap:
            meta = layer.rasterlayerbandmetadata_set.first()
            if meta is not None:
                colormap['range'] = (meta.min, meta.max)

        return colormap
Exemplo n.º 4
0
 def test_hex_colormap_to_rgba(self):
     colormap = {0: "#000000", 1: "#00FF00", 2: "#FF0000", 3: "#0000FF"}
     converted_colormap = colormap_to_rgba(colormap)
     self.assertEqual(
         converted_colormap, {
             0: (0, 0, 0, 255),
             1: (0, 255, 0, 255),
             2: (255, 0, 0, 255),
             3: (0, 0, 255, 255)
         })
Exemplo n.º 5
0
def set_session_colormap(session, key, colormap):
    """
    Store the colormap in the user session.
    """
    raster_legends = session.get('raster_legends', {})
    raster_legends[key] = {
        "colormap": colormap_to_rgba(colormap),
    }

    session['raster_legends'] = raster_legends
Exemplo n.º 6
0
def set_session_colormap(session, key, colormap):
    """
    Store the colormap in the user session.
    """
    raster_legends = session.get('raster_legends', {})
    raster_legends[key] = {
        "colormap": colormap_to_rgba(colormap),
    }

    session['raster_legends'] = raster_legends
Exemplo n.º 7
0
 def test_hex_colormap_to_rgba(self):
     colormap = {
         0: "#000000",
         1: "#00FF00",
         2: "#FF0000",
         3: "#0000FF"
     }
     converted_colormap = colormap_to_rgba(colormap)
     self.assertEqual(converted_colormap, {
         0: (0, 0, 0, 255),
         1: (0, 255, 0, 255),
         2: (255, 0, 0, 255),
         3: (0, 0, 255, 255)
     })
Exemplo n.º 8
0
 def test_empty_colormap_to_rgba(self):
     colormap = {}
     self.assertEqual(colormap, colormap_to_rgba(colormap))
Exemplo n.º 9
0
    def get_colormap(self, layer=None):
        """
        Returns colormap from request and layer, looking for a colormap in the
        request or session, a custom legend name to construct the legend or the
        default colormap from the layer legend.
        """
        if 'colormap' in self.request.GET:
            colormap = self.request.GET['colormap']
            colormap = colormap_to_rgba(json.loads(colormap))
        elif 'legend' in self.request.GET:
            store = self.request.GET.get('store', 'database')
            if store == 'session':
                colormap = get_session_colormap(self.request.session,
                                                self.request.GET['legend'])
            else:
                legend_input = self.request.GET['legend']
                try:
                    legend_input = int(legend_input)
                except ValueError:
                    pass

                # Try to get legend by id, name or from input layer
                if isinstance(legend_input, int):
                    legend = get_object_or_404(Legend, id=legend_input)
                else:
                    legend = Legend.objects.filter(
                        title__iexact=legend_input).first()
                colormap = legend.colormap

        elif layer and hasattr(layer.legend, 'colormap'):
            colormap = layer.legend.colormap
        elif layer:
            # Construct a grayscale colormap from layer metadata
            meta = layer.rasterlayerbandmetadata_set.first()

            # Return if no metadata can be found to construct the colormap
            if meta is None:
                return

            # Compute bin width for a linear scaling
            diff = (meta.max - meta.min) / DEFAULT_LEGEND_BREAKS

            # Create colormap with seven breaks
            colormap = {}
            for i in range(DEFAULT_LEGEND_BREAKS):
                if i == 0:
                    expression = '({0} <= x) & (x <= {1})'
                else:
                    expression = '({0} < x) & (x <= {1})'
                expression = expression.format(meta.min + diff * i,
                                               meta.min + diff * (i + 1))
                colormap[expression] = [(255 / (DEFAULT_LEGEND_BREAKS - 1)) * i
                                        ] * 3 + [
                                            255,
                                        ]
        else:
            return

        # Filter by custom entries if requested
        if 'entries' in self.request.GET:
            entries = self.request.GET['entries'].split(',')
            colormap = {
                k: v
                for (k, v) in colormap.items() if str(k) in entries
            }

        return colormap
Exemplo n.º 10
0
 def test_empty_colormap_to_rgba(self):
     colormap = {}
     self.assertEqual(colormap, colormap_to_rgba(colormap))