Skip to content

Commit

Permalink
raise dimension error
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Oct 12, 2023
1 parent 29fd591 commit 09b0435
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 3 additions & 5 deletions pygeoif/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from pygeoif import geometry
from pygeoif.exceptions import DimensionError


def test_empty() -> None:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 09b0435

Please sign in to comment.