Skip to content

Commit

Permalink
Improve interoperability with shapely: (#52)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
loicdtx authored and sgillies committed Nov 5, 2019
1 parent 634e96b commit e90718c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions affine/__init__.py
Expand Up @@ -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'"""
Expand Down
9 changes: 7 additions & 2 deletions affine/tests/test_transform.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e90718c

Please sign in to comment.