Skip to content

Commit

Permalink
Dataclasses instead of namedtuples
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Dec 14, 2024
1 parent 8f6a8f6 commit df6740b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
12 changes: 10 additions & 2 deletions mikeio/spatial/_FM_geometry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from collections import namedtuple
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
from typing import (
Expand All @@ -12,6 +12,7 @@


import numpy as np
from numpy.typing import NDArray
from mikecore.DfsuFile import DfsuFileType
from mikecore.eum import eumQuantity
from mikecore.MeshBuilder import MeshBuilder
Expand All @@ -38,6 +39,14 @@
from matplotlib.axes import Axes


@dataclass
class Polyline:
n_nodes: int
nodes: NDArray
xy: NDArray
area: float


class _GeometryFMPlotter:
"""Plot GeometryFM.
Expand Down Expand Up @@ -914,7 +923,6 @@ def _get_boundary_polylines(self) -> BoundaryPolylines:

poly_lines_int = []
poly_lines_ext = []
Polyline = namedtuple("Polyline", ["n_nodes", "nodes", "xy", "area"])

for polyline in polylines:
xy = self.node_coordinates[polyline, :2]
Expand Down
14 changes: 8 additions & 6 deletions mikeio/spatial/_FM_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Literal, Sequence
from matplotlib.axes import Axes
from matplotlib.cm import ScalarMappable
Expand All @@ -8,18 +8,20 @@
from matplotlib.tri import Triangulation
import numpy as np
from scipy.sparse import csr_matrix
from collections import namedtuple

from ._utils import _relative_cumulative_distance


MESH_COL = "0.95"
MESH_COL_DARK = "0.6"

BoundaryPolylines = namedtuple(
"BoundaryPolylines",
["n_exteriors", "exteriors", "n_interiors", "interiors"],
)

@dataclass
class BoundaryPolylines:
n_exteriors: int
exteriors: list
n_interiors: int
interiors: list


def _plot_map(
Expand Down
32 changes: 30 additions & 2 deletions mikeio/spatial/_geometry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
from abc import ABC, abstractmethod

from collections import namedtuple
from dataclasses import dataclass
from typing import Any

from mikecore.Projections import MapProjection

BoundingBox = namedtuple("BoundingBox", ["left", "bottom", "right", "top"])

@dataclass
class BoundingBox:
"""Bounding box for spatial data."""

left: float
bottom: float
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(
f"Invalid x axis, left: {self.left} must be smaller than right: {self.right}"
)

if self.bottom > self.top:
raise ValueError(
f"Invalid y axis, bottom: {self.bottom} must be smaller than top: {self.top}"
)


class _Geometry(ABC):
Expand Down

0 comments on commit df6740b

Please sign in to comment.