Skip to content

Commit

Permalink
Move method
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Dec 14, 2024
1 parent 7ad6017 commit d5a8800
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
35 changes: 25 additions & 10 deletions mikeio/spatial/_geometry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod

from dataclasses import dataclass
from typing import Any
from typing import Any, Sequence

from mikecore.Projections import MapProjection

Expand All @@ -15,15 +15,6 @@ class BoundingBox:
right: float
top: float

def overlaps(self, other: "BoundingBox") -> bool:
"""Check if two bounding boxes overlap."""
return not (
self.left > other.right
or self.bottom > other.top
or self.right < other.left
or self.top < other.bottom
)

def __post_init__(self) -> None:
if self.left > self.right:
raise ValueError(
Expand All @@ -35,6 +26,30 @@ def __post_init__(self) -> None:
f"Invalid y axis, bottom: {self.bottom} must be smaller than top: {self.top}"
)

def overlaps(self, other: "BoundingBox") -> bool:
"""Check if two bounding boxes overlap."""
return not (
self.left > other.right
or self.bottom > other.top
or self.right < other.left
or self.top < other.bottom
)

@staticmethod
def parse(
values: "BoundingBox" | Sequence[float],
) -> "BoundingBox":
match values:
case BoundingBox():
bbox = values
case left, bottom, right, top:
bbox = BoundingBox(left, bottom, right, top)
case _:
raise ValueError(
"values must be a bounding box of coordinates e.g. (-10.0, 10.0 20.0, 30.0)"
)
return bbox


class _Geometry(ABC):
def __init__(self, projection: str = "LONG/LAT") -> None:
Expand Down
19 changes: 2 additions & 17 deletions mikeio/spatial/_grid_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def _create_in_bbox(
in which case the value will be inferred
"""
box = self._parse_bbox(bbox)
box = BoundingBox.parse(bbox)

xr = box.right - box.left # dx too large
yr = box.top - box.bottom # dy too large
Expand All @@ -577,21 +577,6 @@ def _create_in_bbox(
"y", box.bottom, box.top, dy, ny
)

@staticmethod
def _parse_bbox(
values: BoundingBox | Sequence[float],
) -> BoundingBox:
match values:
case BoundingBox():
bbox = values
case left, bottom, right, top:
bbox = BoundingBox(left, bottom, right, top)
case _:
raise ValueError(
"area most be a bounding box of coordinates e.g. area=(-10.0, 10.0 20.0, 30.0)"
)
return bbox

@staticmethod
def _create_in_bbox_1d(
axname: str,
Expand Down Expand Up @@ -850,7 +835,7 @@ def find_index(
if coords is not None:
return self._xy_to_index(coords)
elif area is not None:
bbox = self._parse_bbox(area)
bbox = BoundingBox.parse(area)
return self._bbox_to_index(bbox)
else:
raise ValueError("Provide x,y or coords")
Expand Down

0 comments on commit d5a8800

Please sign in to comment.