Skip to content

Commit

Permalink
Raise ValueError if (g, h, i) != (0.0, 0.0, 1.0) (#117)
Browse files Browse the repository at this point in the history
* Raise ValueError if (g, h, i) != (0.0, 0.0, 1.0)

* Update change log

* Reformat

* Take docstring suggestions from #118
  • Loading branch information
sgillies authored Jan 23, 2025
1 parent bdb7661 commit 57c1220
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
5 changes: 3 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ CHANGES
3.0a1 (TBD)
-----------

- Affine raises ValueError if initialized with values for g, h, and i that are
not 0.0, 0.0, and 1.0, respectively (#117).
- Python version support was changed to 3.9+ (#110).
- Switch from namedtuple to attrs for implementation of the Affine class and
use functools.cached_property(), which absolutely requires Python 3.8+
(#111).
- Source was moved to a single-module affine.py in the src directory (#112,
#117).
- Source was moved to a single-module affine.py in the src directory (#112).
- Add numpy __array__ interface (#108).

2.4.0 (2023-01-19)
Expand Down
46 changes: 31 additions & 15 deletions src/affine.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Affine:
Parameters
----------
a, b, c, d, e, f : float
Coefficients of an augmented affine transformation matrix
a, b, c, d, e, f, [g, h, i] : float
Coefficients of the 3 x 3 augmented affine transformation matrix.
| x' | | a b c | | x |
| y' | = | d e f | | y |
Expand All @@ -95,12 +95,12 @@ class Affine:
Attributes
----------
a, b, c, d, e, f, g, h, i : float
The coefficients of the 3x3 augmented affine transformation
matrix
The coefficients of the 3 x 3 augmented affine transformation
matrix::
| x' | | a b c | | x |
| y' | = | d e f | | y |
| 1 | | g h i | | 1 |
| x' | | a b c | | x |
| y' | = | d e f | | y |
| 1 | | g h i | | 1 |
`g`, `h`, and `i` are always 0, 0, and 1.
Expand Down Expand Up @@ -131,10 +131,33 @@ class Affine:
d: float = field(converter=float)
e: float = field(converter=float)
f: float = field(converter=float)

# The class has 3 attributes that don't have to be specified: g, h,
# and i. If they are, the given value has to be the same as the
# default value. This allows a new instances to be created from the
# tuple form of another, like Affine(*Affine.identity()).

g: float = field(default=0.0, converter=float)

@g.validator
def _check_g(self, attribute, value):
if value != 0.0:
raise ValueError("g must be equal to 0.0")

h: float = field(default=0.0, converter=float)

@h.validator
def _check_h(self, attribute, value):
if value != 0.0:
raise ValueError("h must be equal to 0.0")

i: float = field(default=1.0, converter=float)

@i.validator
def _check_i(self, attribute, value):
if value != 1.0:
raise ValueError("i must be equal to 1.0")

@classmethod
def from_gdal(cls, c: float, a: float, b: float, f: float, d: float, e: float):
"""Use same coefficient order as GDAL's GetGeoTransform().
Expand Down Expand Up @@ -287,14 +310,7 @@ def __array__(self, dtype=None, copy=None):

if copy is False:
raise ValueError("`copy=False` isn't supported. A copy is always created.")
return np.array(
[
[self.a, self.b, self.c],
[self.d, self.e, self.f],
[0.0, 0.0, 1.0],
],
dtype=dtype or float,
)
return np.array(self._astuple, dtype=(dtype or float)).reshape((3, 3))

def __str__(self) -> str:
"""Concise string representation."""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,18 @@ def __rmul__(self, other):
return other * (1, 2)

assert Affine.identity() * TextPoint() == (1, 2)


def test_init_invalid_g():
with pytest.raises(ValueError, match="g must"):
Affine(0, 0, 0, 0, 0, 0, 1)


def test_init_invalid_h():
with pytest.raises(ValueError, match="h must"):
Affine(0, 0, 0, 0, 0, 0, 0, 1)


def test_init_invalid_i():
with pytest.raises(ValueError, match="i must"):
Affine(0, 0, 0, 0, 0, 0, 0, 0, 0)

0 comments on commit 57c1220

Please sign in to comment.