/usr/lib/python3/dist-packages/affine/tests/test_transform.py is in python3-affine 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | #############################################################################
# Planar is Copyright (c) 2010 by Casey Duncan
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name(s) of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AS IS AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#############################################################################
"""Transform unit tests"""
from __future__ import division
import math
import unittest
from textwrap import dedent
from nose.tools import assert_equal, assert_almost_equal, raises
from affine import Affine, EPSILON, UndefinedRotationError
def seq_almost_equal(t1, t2, error=0.00001):
assert len(t1) == len(t2), "%r != %r" % (t1, t2)
for m1, m2 in zip(t1, t2):
assert abs(m1 - m2) <= error, "%r != %r" % (t1, t2)
class PyAffineTestCase(unittest.TestCase):
@raises(TypeError)
def test_zero_args(self):
Affine()
@raises(TypeError)
def test_wrong_arg_type(self):
Affine(None)
@raises(TypeError)
def test_args_too_few(self):
Affine(1, 2)
@raises(TypeError)
def test_args_too_many(self):
Affine(*range(10))
@raises(TypeError)
def test_args_members_wrong_type(self):
Affine(0, 2, 3, None, None, "")
def test_len(self):
t = Affine(1, 2, 3, 4, 5, 6)
assert_equal(len(t), 9)
def test_slice_last_row(self):
t = Affine(1, 2, 3, 4, 5, 6)
assert_equal(t[-3:], (0, 0, 1))
def test_members_are_floats(self):
t = Affine(1, 2, 3, 4, 5, 6)
for m in t:
assert isinstance(m, float), repr(m)
def test_getitem(self):
t = Affine(1, 2, 3, 4, 5, 6)
assert_equal(t[0], 1)
assert_equal(t[1], 2)
assert_equal(t[2], 3)
assert_equal(t[3], 4)
assert_equal(t[4], 5)
assert_equal(t[5], 6)
assert_equal(t[6], 0)
assert_equal(t[7], 0)
assert_equal(t[8], 1)
assert_equal(t[-1], 1)
@raises(TypeError)
def test_getitem_wrong_type(self):
t = Affine(1, 2, 3, 4, 5, 6)
t['foobar']
def test_str(self):
assert_equal(
str(Affine(1.111, 2.222, 3.333, -4.444, -5.555, 6.666)),
"| 1.11, 2.22, 3.33|\n|-4.44,-5.55, 6.67|\n| 0.00, 0.00, 1.00|")
def test_repr(self):
assert_equal(
repr(Affine(1.111, 2.222, 3.456, 4.444, 5.5, 6.25)),
("Affine(1.111, 2.222, 3.456,\n"
" 4.444, 5.5, 6.25)"))
def test_identity_constructor(self):
ident = Affine.identity()
assert isinstance(ident, Affine)
assert_equal(
tuple(ident),
(1, 0, 0,
0, 1, 0,
0, 0, 1))
assert ident.is_identity
def test_translation_constructor(self):
trans = Affine.translation(2, -5)
assert isinstance(trans, Affine)
assert_equal(
tuple(trans),
(1, 0, 2,
0, 1, -5,
0, 0, 1))
def test_scale_constructor(self):
scale = Affine.scale(5)
assert isinstance(scale, Affine)
assert_equal(
tuple(scale),
(5, 0, 0,
0, 5, 0,
0, 0, 1))
scale = Affine.scale(-1, 2)
assert_equal(
tuple(scale),
(-1, 0, 0,
0, 2, 0,
0, 0, 1))
assert_equal(tuple(Affine.scale(1)), tuple(Affine.identity()))
def test_shear_constructor(self):
shear = Affine.shear(30)
assert isinstance(shear, Affine)
mx = math.tan(math.radians(30))
seq_almost_equal(
tuple(shear),
(1, mx, 0,
0, 1, 0,
0, 0, 1))
shear = Affine.shear(-15, 60)
mx = math.tan(math.radians(-15))
my = math.tan(math.radians(60))
seq_almost_equal(
tuple(shear),
(1, mx, 0,
my, 1, 0,
0, 0, 1))
shear = Affine.shear(y_angle=45)
seq_almost_equal(
tuple(shear),
(1, 0, 0,
1, 1, 0,
0, 0, 1))
def test_rotation_constructor(self):
rot = Affine.rotation(60)
assert isinstance(rot, Affine)
r = math.radians(60)
s, c = math.sin(r), math.cos(r)
assert_equal(
tuple(rot),
(c, -s, 0,
s, c, 0,
0, 0, 1))
rot = Affine.rotation(337)
r = math.radians(337)
s, c = math.sin(r), math.cos(r)
seq_almost_equal(
tuple(rot),
(c, -s, 0,
s, c, 0,
0, 0, 1))
assert_equal(tuple(Affine.rotation(0)), tuple(Affine.identity()))
def test_rotation_constructor_quadrants(self):
assert_equal(
tuple(Affine.rotation(0)),
(1, 0, 0,
0, 1, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(90)),
(0, -1, 0,
1, 0, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(180)),
(-1, 0, 0,
0, -1, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(-180)),
(-1, 0, 0,
0, -1, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(270)),
(0, 1, 0,
-1, 0, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(-90)),
(0, 1, 0,
-1, 0, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(360)),
(1, 0, 0,
0, 1, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(450)),
(0, -1, 0,
1, 0, 0,
0, 0, 1))
assert_equal(
tuple(Affine.rotation(-450)),
(0, 1, 0,
-1, 0, 0,
0, 0, 1))
def test_rotation_constructor_with_pivot(self):
assert_equal(tuple(Affine.rotation(60)),
tuple(Affine.rotation(60, pivot=(0, 0))))
rot = Affine.rotation(27, pivot=(2, -4))
r = math.radians(27)
s, c = math.sin(r), math.cos(r)
assert_equal(
tuple(rot),
(c, -s, 2 - 2 * c - 4 * s,
s, c, -4 - 2 * s + 4 * c,
0, 0, 1))
assert_equal(tuple(Affine.rotation(0, (-3, 2))),
tuple(Affine.identity()))
@raises(TypeError)
def test_rotation_contructor_wrong_arg_types(self):
Affine.rotation(1, 1)
def test_determinant(self):
assert_equal(Affine.identity().determinant, 1)
assert_equal(Affine.scale(2).determinant, 4)
assert_equal(Affine.scale(0).determinant, 0)
assert_equal(Affine.scale(5, 1).determinant, 5)
assert_equal(Affine.scale(-1, 1).determinant, -1)
assert_equal(Affine.scale(-1, 0).determinant, 0)
assert_almost_equal(Affine.rotation(77).determinant, 1)
assert_almost_equal(Affine.translation(32, -47).determinant, 1)
def test_is_rectilinear(self):
assert Affine.identity().is_rectilinear
assert Affine.scale(2.5, 6.1).is_rectilinear
assert Affine.translation(4, -1).is_rectilinear
assert Affine.rotation(90).is_rectilinear
assert not Affine.shear(4, -1).is_rectilinear
assert not Affine.rotation(-26).is_rectilinear
def test_is_conformal(self):
assert Affine.identity().is_conformal
assert Affine.scale(2.5, 6.1).is_conformal
assert Affine.translation(4, -1).is_conformal
assert Affine.rotation(90).is_conformal
assert Affine.rotation(-26).is_conformal
assert not Affine.shear(4, -1).is_conformal
def test_is_orthonormal(self):
assert Affine.identity().is_orthonormal
assert Affine.translation(4, -1).is_orthonormal
assert Affine.rotation(90).is_orthonormal
assert Affine.rotation(-26).is_orthonormal
assert not Affine.scale(2.5, 6.1).is_orthonormal
assert not Affine.scale(.5, 2).is_orthonormal
assert not Affine.shear(4, -1).is_orthonormal
def test_is_degenerate(self):
assert not Affine.identity().is_degenerate
assert not Affine.translation(2, -1).is_degenerate
assert not Affine.shear(0, -22.5).is_degenerate
assert not Affine.rotation(88.7).is_degenerate
assert not Affine.scale(0.5).is_degenerate
assert Affine.scale(0).is_degenerate
assert Affine.scale(-10, 0).is_degenerate
assert Affine.scale(0, 300).is_degenerate
assert Affine.scale(0).is_degenerate
assert Affine.scale(0).is_degenerate
def test_column_vectors(self):
a, b, c = Affine(2, 3, 4, 5, 6, 7).column_vectors
assert isinstance(a, tuple)
assert isinstance(b, tuple)
assert isinstance(c, tuple)
assert_equal(a, (2, 5))
assert_equal(b, (3, 6))
assert_equal(c, (4, 7))
def test_almost_equals(self):
EPSILON = 1e-5
E = EPSILON * 0.5
t = Affine(1.0, E, 0, -E, 1.0 + E, E)
assert t.almost_equals(Affine.identity())
assert Affine.identity().almost_equals(t)
assert t.almost_equals(t)
t = Affine(1.0, 0, 0, -EPSILON, 1.0, 0)
assert not t.almost_equals(Affine.identity())
assert not Affine.identity().almost_equals(t)
assert t.almost_equals(t)
def test_almost_equals_2(self):
EPSILON = 1e-10
E = EPSILON * 0.5
t = Affine(1.0, E, 0, -E, 1.0 + E, E)
assert t.almost_equals(Affine.identity(), precision=EPSILON)
assert Affine.identity().almost_equals(t, precision=EPSILON)
assert t.almost_equals(t, precision=EPSILON)
t = Affine(1.0, 0, 0, -EPSILON, 1.0, 0)
assert not t.almost_equals(Affine.identity(), precision=EPSILON)
assert not Affine.identity().almost_equals(t, precision=EPSILON)
assert t.almost_equals(t, precision=EPSILON)
def test_equality(self):
t1 = Affine(1, 2, 3, 4, 5, 6)
t2 = Affine(6, 5, 4, 3, 2, 1)
t3 = Affine(1, 2, 3, 4, 5, 6)
assert t1 == t3
assert not t1 == t2
assert t2 == t2
assert not t1 != t3
assert not t2 != t2
assert t1 != t2
assert not t1 == 1
assert t1 != 1
@raises(TypeError)
def test_gt(self):
Affine(1, 2, 3, 4, 5, 6) > Affine(6, 5, 4, 3, 2, 1)
@raises(TypeError)
def test_lt(self):
Affine(1, 2, 3, 4, 5, 6) < Affine(6, 5, 4, 3, 2, 1)
@raises(TypeError)
def test_add(self):
Affine(1, 2, 3, 4, 5, 6) + Affine(6, 5, 4, 3, 2, 1)
@raises(TypeError)
def test_sub(self):
Affine(1, 2, 3, 4, 5, 6) - Affine(6, 5, 4, 3, 2, 1)
def test_mul_by_identity(self):
t = Affine(1, 2, 3, 4, 5, 6)
assert_equal(tuple(t * Affine.identity()), tuple(t))
def test_mul_transform(self):
t = Affine.rotation(5) * Affine.rotation(29)
assert isinstance(t, Affine)
seq_almost_equal(t, Affine.rotation(34))
t = Affine.scale(3, 5) * Affine.scale(2)
seq_almost_equal(t, Affine.scale(6, 10))
def test_itransform(self):
pts = [(4, 1), (-1, 0), (3, 2)]
r = Affine.scale(-2).itransform(pts)
assert r is None, r
assert_equal(pts, [(-8, -2), (2, 0), (-6, -4)])
@raises(TypeError)
def test_mul_wrong_type(self):
Affine(1, 2, 3, 4, 5, 6) * None
@raises(TypeError)
def test_mul_sequence_wrong_member_types(self):
class NotPtSeq:
@classmethod
def from_points(cls, points):
list(points)
def __iter__(self):
yield 0
Affine(1, 2, 3, 4, 5, 6) * NotPtSeq()
def test_imul_transform(self):
t = Affine.translation(3, 5)
t *= Affine.translation(-2, 3.5)
assert isinstance(t, Affine)
seq_almost_equal(t, Affine.translation(1, 8.5))
def test_inverse(self):
seq_almost_equal(~Affine.identity(), Affine.identity())
seq_almost_equal(
~Affine.translation(2, -3), Affine.translation(-2, 3))
seq_almost_equal(
~Affine.rotation(-33.3), Affine.rotation(33.3))
t = Affine(1, 2, 3, 4, 5, 6)
seq_almost_equal(~t * t, Affine.identity())
def test_cant_invert_degenerate(self):
from affine import TransformNotInvertibleError
t = Affine.scale(0)
self.assertRaises(TransformNotInvertibleError, lambda: ~t)
@raises(TypeError)
def test_bad_type_world(self):
from affine import loadsw
# wrong type, i.e don't use readlines()
loadsw(['1.0', '0.0', '0.0', '1.0', '0.0', '0.0'])
@raises(ValueError)
def test_bad_value_world(self):
from affine import loadsw
# wrong number of parameters
loadsw('1.0\n0.0\n0.0\n1.0\n0.0\n0.0\n0.0')
def test_simple_world(self):
from affine import loadsw, dumpsw
s = '1.0\n0.0\n0.0\n-1.0\n100.5\n199.5\n'
a = loadsw(s)
self.assertEqual(
a,
Affine(
1.0, 0.0, 100.0,
0.0, -1., 200.0))
self.assertEqual(dumpsw(a), s)
def test_real_world(self):
from affine import loadsw, dumpsw
s = dedent('''\
39.9317755024
30.0907511581
30.0907511576
-39.9317755019
2658137.2266720217
5990821.7039887439''') # no EOL
a1 = loadsw(s)
self.assertTrue(a1.almost_equals(
Affine(
39.931775502364644, 30.090751157602412, 2658102.2154086917,
30.090751157602412, -39.931775502364644, 5990826.624500916)))
a1out = dumpsw(a1)
self.assertTrue(isinstance(a1out, str))
a2 = loadsw(a1out)
self.assertTrue(a1.almost_equals(a2))
# We're using pytest for tests added after 1.0 and don't need unittest
# test case classes.
def test_gdal():
t = Affine.from_gdal(-237481.5, 425.0, 0.0, 237536.4, 0.0, -425.0)
assert t.c == t.xoff == -237481.5
assert t.a == 425.0
assert t.b == 0.0
assert t.f == t.yoff == 237536.4
assert t.d == 0.0
assert t.e == -425.0
assert tuple(t) == (425.0, 0.0, -237481.5, 0.0, -425.0, 237536.4, 0, 0, 1.0)
assert t.to_gdal() == (-237481.5, 425.0, 0.0, 237536.4, 0.0, -425.0)
def test_imul_number():
t = Affine(1, 2, 3, 4, 5, 6)
try:
t *= 2.0
except TypeError:
assert True
def test_mul_tuple():
t = Affine(1, 2, 3, 4, 5, 6)
t * (2.0, 2.0)
def test_rmul_tuple():
t = Affine(1, 2, 3, 4, 5, 6)
(2.0, 2.0) * t
def test_transform_precision():
t = Affine.rotation(45.0)
assert t.precision == EPSILON
t.precision = 1e-10
assert t.precision == 1e-10
assert Affine.rotation(0.0).precision == EPSILON
def test_associative():
point = (12, 5)
trans = Affine.translation(-10., -5.)
rot90 = Affine.rotation(90.)
result1 = rot90 * (trans * point)
result2 = (rot90 * trans) * point
seq_almost_equal(result1, (0., 2.))
seq_almost_equal(result1, result2)
def test_roundtrip():
point = (12, 5)
trans = Affine.translation(3, 4)
rot37 = Affine.rotation(37.)
point_prime = (trans * rot37) * point
roundtrip_point = ~(trans * rot37) * point_prime
seq_almost_equal(point, roundtrip_point)
def test_eccentricity():
assert_equal(Affine.identity().eccentricity, 0.0)
assert_equal(Affine.scale(2).eccentricity, 0.0)
#assert_equal(Affine.scale(0).eccentricity, ?)
assert_almost_equal(Affine.scale(2, 1).eccentricity, math.sqrt(3) / 2)
assert_almost_equal(Affine.scale(2, 3).eccentricity, math.sqrt(5) / 3)
assert_equal(Affine.scale(1, 0).eccentricity, 1.0)
assert_almost_equal(Affine.rotation(77).eccentricity, 0.0)
assert_almost_equal(Affine.translation(32, -47).eccentricity, 0.0)
assert_almost_equal(Affine.scale(-1, 1).eccentricity, 0.0)
def test_eccentricity_complex():
assert_almost_equal(
(Affine.scale(2, 3) * Affine.rotation(77)).eccentricity,
math.sqrt(5) / 3
)
assert_almost_equal(
(Affine.rotation(77) * Affine.scale(2, 3)).eccentricity,
math.sqrt(5) / 3
)
assert_almost_equal(
(Affine.translation(32, -47) * Affine.rotation(77) * Affine.scale(2, 3)).eccentricity,
math.sqrt(5) / 3
)
def test_rotation_angle():
assert_equal(Affine.identity().rotation_angle, 0.0)
assert_equal(Affine.scale(2).rotation_angle, 0.0)
assert_equal(Affine.scale(2, 1).rotation_angle, 0.0)
assert_almost_equal(Affine.translation(32, -47).rotation_angle, 0.0)
assert_almost_equal(Affine.rotation(30).rotation_angle, 30)
assert_almost_equal(Affine.rotation(-150).rotation_angle, -150)
@raises(UndefinedRotationError)
def test_rotation_improper():
Affine.scale(-1, 1).rotation_angle
if __name__ == '__main__':
unittest.main()
# vim: ai ts=4 sts=4 et sw=4 tw=78
|