Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Dec 14, 2024
1 parent df6740b commit 7ad6017
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions mikeio/spatial/_grid_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,18 @@ def _create_in_bbox(

@staticmethod
def _parse_bbox(
bbox: BoundingBox | Sequence[float],
values: BoundingBox | Sequence[float],
) -> BoundingBox:
if isinstance(bbox, BoundingBox):
return bbox
elif isinstance(bbox, Sequence) and len(bbox) == 4:
return BoundingBox(*bbox)
else:
raise ValueError("bbox must be a BoundingBox or a tuple of length 4")
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(
Expand Down Expand Up @@ -846,7 +850,8 @@ def find_index(
if coords is not None:
return self._xy_to_index(coords)
elif area is not None:
return self._bbox_to_index(area)
bbox = self._parse_bbox(area)
return self._bbox_to_index(bbox)
else:
raise ValueError("Provide x,y or coords")

Expand All @@ -869,19 +874,8 @@ def _xy_to_index(self, xy: np.ndarray) -> tuple[np.ndarray, np.ndarray]:

return ii, jj

def _bbox_to_index(
self, values: BoundingBox | tuple[float, float, float, float]
) -> tuple[range, range]:
def _bbox_to_index(self, bbox: BoundingBox) -> tuple[range, range]:
"""Find subarea within this geometry."""
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)"
)
if not bbox.overlaps(self.bbox):
raise ValueError("area is outside grid")

Expand Down

0 comments on commit 7ad6017

Please sign in to comment.