class WMSLegendSource(LegendSource): def __init__(self, clients, legend_cache): self.clients = clients self.identifier = legend_identifier([c.identifier for c in self.clients]) self._cache = legend_cache self._size = None @property def size(self): if not self._size: legend = self.get_legend(LegendQuery(format='image/png', scale=None)) # TODO image size without as_image? self._size = legend.as_image().size return self._size def get_legend(self, query): legend = Legend(id=self.identifier, scale=query.scale) if not self._cache.load(legend): legends = [] error_occured = False for client in self.clients: try: legends.append(client.get_legend(query)) except HTTPClientError, e: error_occured = True log.error(e.args[0]) except SourceError, e: error_occured = True # TODO errors? log.error(e.args[0]) format = split_mime_type(query.format)[1] legend = Legend(source=concat_legends(legends, format=format), id=self.identifier, scale=query.scale) if not error_occured: self._cache.store(legend)
def get_legend(self, query): if self.static: # prevent caching of static legends for different scales legend = Legend(id=self.identifier, scale=None) else: legend = Legend(id=self.identifier, scale=query.scale) if not self._cache.load(legend): legends = [] error_occured = False for client in self.clients: try: legends.append(client.get_legend(query)) except HTTPClientError as e: error_occured = True log.error(e.args[0]) except SourceError as e: error_occured = True # TODO errors? log.error(e.args[0]) format = split_mime_type(query.format)[1] legend = Legend(source=concat_legends(legends, format=format), id=self.identifier, scale=query.scale) if not error_occured: self._cache.store(legend) return legend.source
def test_concatenation(self): legends = [] img_1 = Image.new(mode='RGBA', size=(30,10), color="red") img_2 = Image.new(mode='RGBA', size=(10,10), color="black") img_3 = Image.new(mode='RGBA', size=(50,80), color="blue") legends.append(ImageSource(img_1)) legends.append(ImageSource(img_2)) legends.append(ImageSource(img_3)) source = concat_legends(legends) src_img = source.as_image() assert src_img.getpixel((0,90)) == (255,0,0,255) assert src_img.getpixel((0,80)) == (0,0,0,255) assert src_img.getpixel((0,0)) == (0,0,255,255) assert src_img.getpixel((49,99)) == (255,255,255,0) assert is_png(source.as_buffer())
def legendgraphic(self, request): legends = [] self.check_legend_request(request) layer = request.params.layer if not self.layers[layer].has_legend: raise RequestError("layer %s has no legend graphic" % layer, request=request) legend = self.layers[layer].legend(request) [legends.append(i) for i in legend if i is not None] result = concat_legends(legends) if "format" in request.params: mimetype = request.params.format_mime_type else: mimetype = "image/png" img_opts = self.image_formats[request.params.format_mime_type] return Response(result.as_buffer(img_opts), mimetype=mimetype)
def legendgraphic(self, request): legends = [] self.check_legend_request(request) layer = request.params.layer if not self.layers[layer].has_legend: raise RequestError('layer %s has no legend graphic' % layer, request=request) legend = self.layers[layer].legend(request) [legends.append(i) for i in legend if i is not None] result = concat_legends(legends) if 'format' in request.params: mimetype = request.params.format_mime_type else: mimetype = 'image/png' img_opts = self.image_formats[request.params.format_mime_type] return Response(result.as_buffer(img_opts), mimetype=mimetype)