From 8e30aec44cb3304a4fcc9f1f2e29091f0edf0c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Fri, 24 May 2024 14:31:33 +0200 Subject: [PATCH] Avoid error when there are no legend images --- tilecloud_chain/controller.py | 41 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tilecloud_chain/controller.py b/tilecloud_chain/controller.py index 3b85eac7f..3f28049e8 100644 --- a/tilecloud_chain/controller.py +++ b/tilecloud_chain/controller.py @@ -364,26 +364,27 @@ def _generate_legend_images(gene: TileGeneration) -> None: response.content, exc_info=True, ) - width = max(i.size[0] for i in legends) - height = sum(i.size[1] for i in legends) - image = Image.new("RGBA", (width, height)) - y = 0 - for i in legends: - image.paste(i, (0, y)) - y += i.size[1] - string_io = BytesIO() - image.save(string_io, FORMAT_BY_CONTENT_TYPE[layer["legend_mime"]]) - result = string_io.getvalue() - new_hash = sha1(result).hexdigest() # nosec - if new_hash != previous_hash: - previous_hash = new_hash - _send( - result, - f"1.0.0/{layer_name}/{layer['wmts_style']}/" - f"legend{zoom}.{layer['legend_extension']}", - layer["legend_mime"], - cache, - ) + if legends: + width = max(i.size[0] for i in legends) + height = sum(i.size[1] for i in legends) + image = Image.new("RGBA", (width, height)) + y = 0 + for i in legends: + image.paste(i, (0, y)) + y += i.size[1] + string_io = BytesIO() + image.save(string_io, FORMAT_BY_CONTENT_TYPE[layer["legend_mime"]]) + result = string_io.getvalue() + new_hash = sha1(result).hexdigest() # nosec + if new_hash != previous_hash: + previous_hash = new_hash + _send( + result, + f"1.0.0/{layer_name}/{layer['wmts_style']}/" + f"legend{zoom}.{layer['legend_extension']}", + layer["legend_mime"], + cache, + ) def _get_resource(resource: str) -> bytes: