From e90718c6464b1efd7d6aaf1ebc1e86b85032a2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dutrieux?= Date: Tue, 5 Nov 2019 15:38:48 +0100 Subject: [PATCH] Improve interoperability with shapely: (#52) - Add a new .to_shapely method to the Affine class that returns a tuple in the order expected by shapely's affinity module - Write simple unit test for the new method --- affine/__init__.py | 10 ++++++++++ affine/tests/test_transform.py | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/affine/__init__.py b/affine/__init__.py index 4982216..b784900 100644 --- a/affine/__init__.py +++ b/affine/__init__.py @@ -311,6 +311,16 @@ def to_gdal(self): """ return (self.c, self.a, self.b, self.f, self.d, self.e) + def to_shapely(self): + """Return an affine transformation matrix compatible with shapely + + Shapely's affinity module expects an affine transformation matrix + in (a,b,d,e,xoff,yoff) order. + + :rtype: tuple + """ + return (self.a, self.b, self.d, self.e, self.xoff, self.yoff) + @property def xoff(self): """Alias for 'c'""" diff --git a/affine/tests/test_transform.py b/affine/tests/test_transform.py index be79168..aed6238 100644 --- a/affine/tests/test_transform.py +++ b/affine/tests/test_transform.py @@ -128,8 +128,8 @@ def test_permutation_constructor(self): (0, 1, 0, 1, 0, 0, 0, 0, 1) - assert (perm*perm).is_identity - + assert (perm*perm).is_identity + def test_translation_constructor(self): trans = Affine.translation(2, -5) assert isinstance(trans, Affine) @@ -485,6 +485,11 @@ def test_gdal(): assert t.to_gdal() == (-237481.5, 425.0, 0.0, 237536.4, 0.0, -425.0) +def test_shapely(): + t = Affine(425.0, 0.0, -237481.5, 0.0, -425.0, 237536.4) + assert t.to_shapely() == (425.0, 0.0, 0.0, -425, -237481.5, 237536.4) + + def test_imul_number(): t = Affine(1, 2, 3, 4, 5, 6) try: