diff --git a/affine/__init__.py b/affine/__init__.py index 91f4b81..37d4433 100644 --- a/affine/__init__.py +++ b/affine/__init__.py @@ -299,6 +299,18 @@ def to_gdal(self): """ return (self.c, self.a, self.b, self.f, self.d, self.e) + def to_cairo(self): + """Return a transformation matrix compatible with PyCairo. + + PyCairo expects a 2x3 matrix in a column-major order. + Rearrange Affines (row-major) matrix to match. + + >>> cairo.Matrix(*affine.to_cairo()) + + :rtype: tuple + """ + return (self.a, self.d, self.b, self.e, self.c, self.f) + def to_shapely(self): """Return an affine transformation matrix compatible with shapely @@ -341,10 +353,10 @@ def _scaling(self): # The singular values are the square root of the eigenvalues # of the matrix times its transpose, M M* # Computing trace and determinant of M M* - trace = a ** 2 + b ** 2 + d ** 2 + e ** 2 + trace = a**2 + b**2 + d**2 + e**2 det = (a * e - b * d) ** 2 - delta = trace ** 2 / 4 - det + delta = trace**2 / 4 - det if delta < 1e-12: delta = 0 @@ -362,7 +374,7 @@ def eccentricity(self) -> float: Raises NotImplementedError for improper transformations. """ l1, l2 = self._scaling - return math.sqrt(l1 ** 2 - l2 ** 2) / l1 + return math.sqrt(l1**2 - l2**2) / l1 @property def rotation_angle(self) -> float: