|
19 | 19 | from emsarray import plot as _plot |
20 | 20 | from emsarray import utils |
21 | 21 | from emsarray.exceptions import InvalidGeometryWarning, NoSuchCoordinateError |
22 | | -from emsarray.operations import depth, point_extraction |
| 22 | +from emsarray.operations import depth, point_extraction, triangulate |
23 | 23 | from emsarray.operations.cache import hash_attributes, hash_int, hash_string |
24 | 24 | from emsarray.state import State |
25 | 25 | from emsarray.types import Bounds, DataArrayOrName, Pathish |
@@ -1949,6 +1949,51 @@ def hash_geometry(self, hash: "hashlib._Hash") -> None: |
1949 | 1949 | # Hash dataset attributes |
1950 | 1950 | hash_attributes(hash, data_array.attrs) |
1951 | 1951 |
|
| 1952 | + def make_triangulation(self) -> triangulate.Triangulation[GridKind]: |
| 1953 | + """ |
| 1954 | + Triangulates the polygons in the dataset. |
| 1955 | + Subclasses may have improved implementations. |
| 1956 | +
|
| 1957 | + This requires the dataset to have a grid with polygonal geometry. |
| 1958 | + If there is an additional grid corresponding to the vertices of the polygons |
| 1959 | + then subclasses should use this information to inform the triangulation. |
| 1960 | +
|
| 1961 | + Returns |
| 1962 | + ------- |
| 1963 | + tuple of vertices, triangles, and `cell_indexes` |
| 1964 | + A tuple of three numpy arrays is returned, |
| 1965 | + containing vertices, triangles, and cell indexes respectively. |
| 1966 | +
|
| 1967 | + `vertices` is a numpy array of shape (V, 2) |
| 1968 | + where V is the number of unique vertices in the dataset. |
| 1969 | + The vertex coordinates are in (x, y) or (lon, lat) order. |
| 1970 | + If the dataset has a vertex grid associated with the polygons |
| 1971 | + then this array replicates that data. |
| 1972 | +
|
| 1973 | + `triangles` is a numpy array of shape (T, 3) |
| 1974 | + where T is the number of triangles in the dataset. |
| 1975 | + Each triangle is a set of three vertex indexes. |
| 1976 | +
|
| 1977 | + `cell_indexes` is a numpy list of length T. |
| 1978 | + Each entry indicates which polygon from the dataset a triangle is a part of. |
| 1979 | +
|
| 1980 | + See also |
| 1981 | + -------- |
| 1982 | + :func:`~emsarray.operations.triangulate.triangulate` |
| 1983 | + """ |
| 1984 | + grid = self.default_grid |
| 1985 | + if not issubclass(grid.geometry_type, shapely.Polygon): |
| 1986 | + raise ValueError("Can not triangulate a dataset that does not have polygonal geometry") |
| 1987 | + polygons = grid.geometry |
| 1988 | + vertices = triangulate.find_unique_vertices(polygons) |
| 1989 | + polygon_vertex_indexes = triangulate.polygons_to_vertex_indexes(polygons, vertices) |
| 1990 | + vertex_coordinates, triangles, face_indexes = triangulate.triangulate(vertices, polygons, polygon_vertex_indexes) |
| 1991 | + return triangulate.Triangulation( |
| 1992 | + vertices=vertex_coordinates, |
| 1993 | + triangles=triangles, |
| 1994 | + face_indexes=face_indexes, |
| 1995 | + face_grid_kind=grid.grid_kind) |
| 1996 | + |
1952 | 1997 |
|
1953 | 1998 | type DimensionIndex[GridKind] = tuple[GridKind, *tuple[int, ...]] |
1954 | 1999 |
|
|
0 commit comments