From 09b043532bc538cceb79b452073611c3ec9c861e Mon Sep 17 00:00:00 2001 From: Christian Ledermann Date: Thu, 12 Oct 2023 21:10:57 +0100 Subject: [PATCH] raise dimension error --- pygeoif/geometry.py | 8 +++----- tests/test_point.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pygeoif/geometry.py b/pygeoif/geometry.py index bbf7ff13..5e238dbd 100644 --- a/pygeoif/geometry.py +++ b/pygeoif/geometry.py @@ -251,11 +251,9 @@ def y(self) -> float: @property def z(self) -> Optional[float]: """Return z coordinate.""" - return ( - self._coordinates[2] # type: ignore[misc] - if len(self._coordinates) == 3 - else None - ) + if self.has_z: + return self._coordinates[2] # type: ignore [misc] + raise DimensionError(f"The {self!r} geometry does not have z values") @property def coords(self) -> Tuple[PointType]: diff --git a/tests/test_point.py b/tests/test_point.py index 3bca9685..9a82b282 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -5,6 +5,7 @@ import pytest from pygeoif import geometry +from pygeoif.exceptions import DimensionError def test_empty() -> None: @@ -52,11 +53,19 @@ def test_bounds3d() -> None: def test_xy() -> None: point = geometry.Point(1.0, 0.0) - assert point.z is None assert point.x == 1 assert point.y == 0 +def test_xy_raises_error_accessing_z() -> None: + point = geometry.Point(1, 0) + + with pytest.raises( + DimensionError, match=r"^The Point\(1, 0\) geometry does not have z values$" + ): + point.z + + def test_xyz() -> None: point = geometry.Point(1.0, 0.0, 2.0)