Skip to content

Commit

Permalink
Merge pull request #2614 from camptocamp/backport/2613-to-1.22
Browse files Browse the repository at this point in the history
[Backport 1.22] Test generated legend images
  • Loading branch information
sbrunner authored Nov 29, 2024
2 parents c0ec5af + 1c37d43 commit b81bfae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tilecloud_chain/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def _generate_legend_images(gene: TileGeneration) -> None:
for zoom, resolution in enumerate(config.config["grids"][layer["grid"]]["resolutions"]):
legends = []
for wmslayer in layer["layers"].split(","):
response = session.get(
url = (
layer["url"]
+ "?"
+ urlencode(
Expand All @@ -391,6 +391,14 @@ def _generate_legend_images(gene: TileGeneration) -> None:
}
)
)
_LOGGER.debug(
"Get legend image for layer '%s'-'%s', resolution '%s': %s",
layer_name,
wmslayer,
resolution,
url,
)
response = session.get(url)
try:
legends.append(Image.open(BytesIO(response.content)))
except Exception: # pylint: disable=broad-exception-caught
Expand Down
24 changes: 24 additions & 0 deletions tilecloud_chain/tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import shutil

from PIL import Image

from tilecloud_chain import TileGeneration, controller
from tilecloud_chain.tests import CompareCase

Expand Down Expand Up @@ -1663,6 +1665,28 @@ def test_quote(self) -> None:
self.assertEqual(quote('ab"c'), "'ab\"c'")
self.assertEqual(quote(""), "''")

def test_generate_legend_images(self) -> None:
self.assert_tiles_generated(
cmd=".build/venv/bin/generate_controler -c tilegeneration/test-legends.yaml --generate-legend-images",
main_func=controller.main,
directory="/tmp/tiles/1.0.0/",
tiles_pattern="%s/default/legend%i.png",
tiles=[("point", 0), ("line", 0), ("line", 2), ("polygon", 0), ("all", 0), ("all", 2)],
)

im = Image.open("/tmp/tiles/1.0.0/point/default/legend0.png")
self.assertEqual(im.size, (64, 20))
im = Image.open("/tmp/tiles/1.0.0/line/default/legend0.png")
self.assertEqual(im.size, (71, 35))
im = Image.open("/tmp/tiles/1.0.0/line/default/legend2.png")
self.assertEqual(im.size, (71, 35))
im = Image.open("/tmp/tiles/1.0.0/polygon/default/legend0.png")
self.assertEqual(im.size, (81, 23))
im = Image.open("/tmp/tiles/1.0.0/all/default/legend0.png")
self.assertEqual(im.size, (81, 78))
im = Image.open("/tmp/tiles/1.0.0/all/default/legend2.png")
self.assertEqual(im.size, (81, 78))

def test_legends(self) -> None:
self.assert_tiles_generated(
cmd=".build/venv/bin/generate_controler -c tilegeneration/test-legends.yaml --legends",
Expand Down

0 comments on commit b81bfae

Please sign in to comment.