Skip to content

Commit 2be9862

Browse files
committed
Unit tests - add 'images_almost_equal(im1, im2, tol)` + update reprojection_test.
1 parent 9954669 commit 2be9862

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

test/python_tests/reprojection_test.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import mapnik
33
import pytest
4-
from .utilities import execution_path
4+
from .utilities import execution_path, images_almost_equal
55

66
@pytest.fixture(scope="module")
77
def setup():
@@ -25,13 +25,13 @@ def test_zoom_all_will_work_with_max_extent():
2525
m.zoom_all()
2626
# note - fixAspectRatio is being called, then re-clipping to maxextent
2727
# which makes this hard to predict
28-
# assert m.envelope() ==merc_bounds
28+
#assert m.envelope() == merc_bounds
2929

30-
#m = mapnik.Map(512,512)
31-
# mapnik.load_map(m,'../data/good_maps/wgs842merc_reprojection.xml')
32-
#merc_bounds = mapnik.Box2d(-20037508.34,-20037508.34,20037508.34,20037508.34)
33-
# m.zoom_to_box(merc_bounds)
34-
# assert m.envelope() ==merc_bounds
30+
m = mapnik.Map(512,512)
31+
mapnik.load_map(m,'../data/good_maps/wgs842merc_reprojection.xml')
32+
merc_bounds = mapnik.Box2d(-20037508.34,-20037508.34,20037508.34,20037508.34)
33+
m.zoom_to_box(merc_bounds)
34+
assert m.envelope() == merc_bounds
3535

3636
def test_visual_zoom_all_rendering1():
3737
m = mapnik.Map(512, 512)
@@ -45,21 +45,16 @@ def test_visual_zoom_all_rendering1():
4545
expected = 'images/support/mapnik-wgs842merc-reprojection-render.png'
4646
im.save(actual, 'png32')
4747
expected_im = mapnik.Image.open(expected)
48-
assert im.tostring('png32') == expected_im.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual,
49-
'test/python_tests/' + expected)
48+
images_almost_equal(im, expected_im)
5049

5150
def test_visual_zoom_all_rendering2():
5251
m = mapnik.Map(512, 512)
5352
mapnik.load_map(m, '../data/good_maps/merc2wgs84_reprojection.xml')
5453
m.zoom_all()
5554
im = mapnik.Image(512, 512)
5655
mapnik.render(m, im)
57-
actual = '/tmp/mapnik-merc2wgs84-reprojection-render.png'
58-
expected = 'images/support/mapnik-merc2wgs84-reprojection-render.png'
59-
im.save(actual, 'png32')
60-
expected_im = mapnik.Image.open(expected)
61-
assert im.tostring('png32') == expected_im.tostring('png32'),'failed comparing actual (%s) and expected (%s)' % (actual,
62-
'test/python_tests/' + expected)
56+
expected_im = mapnik.Image.open('images/support/mapnik-merc2wgs84-reprojection-render.png')
57+
images_almost_equal(im, expected_im)
6358

6459
# maximum-extent read from map.xml
6560
def test_visual_zoom_all_rendering3():
@@ -68,12 +63,9 @@ def test_visual_zoom_all_rendering3():
6863
m.zoom_all()
6964
im = mapnik.Image(512, 512)
7065
mapnik.render(m, im)
71-
actual = '/tmp/mapnik-merc2merc-reprojection-render1.png'
7266
expected = 'images/support/mapnik-merc2merc-reprojection-render1.png'
73-
im.save(actual, 'png32')
7467
expected_im = mapnik.Image.open(expected)
75-
assert im.tostring('png32') == expected_im.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual,
76-
'test/python_tests/' + expected)
68+
images_almost_equal(im, expected_im)
7769

7870
# no maximum-extent
7971
def test_visual_zoom_all_rendering4():
@@ -83,8 +75,6 @@ def test_visual_zoom_all_rendering4():
8375
m.zoom_all()
8476
im = mapnik.Image(512, 512)
8577
mapnik.render(m, im)
86-
actual = '/tmp/mapnik-merc2merc-reprojection-render2.png'
8778
expected = 'images/support/mapnik-merc2merc-reprojection-render2.png'
88-
im.save(actual, 'png32')
8979
expected_im = mapnik.Image.open(expected)
90-
assert im.tostring('png32') == expected_im.tostring('png32'),'failed comparing actual (%s) and expected (%s)' % (actual, 'test/python_tests/' + expected)
80+
images_almost_equal(im, expected_im)

test/python_tests/utilities.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,15 @@ def assert_box2d_almost_equal(a, b, msg=None):
9797
assert a.maxx == pytest.approx(b.maxx, abs=1e-2), msg
9898
assert a.miny == pytest.approx(b.miny, abs=1e-2), msg
9999
assert a.maxy == pytest.approx(b.maxy, abs=1e-2), msg
100+
101+
102+
def images_almost_equal(image1, image2, tolerance = 1):
103+
def rgba(p):
104+
return p & 0xff,(p >> 8) & 0xff,(p >> 16) & 0xff, p >> 24
105+
assert image1.width() == image2.width()
106+
assert image1.height() == image2.height()
107+
for x in range(image1.width()):
108+
for y in range(image1.height()):
109+
p1 = image1.get_pixel(x, y)
110+
p2 = image2.get_pixel(x, y)
111+
assert rgba(p1) == pytest.approx(rgba(p2), abs = tolerance)

0 commit comments

Comments
 (0)