From ccc9e637f62e8f4ce7c52a1d7bd08cb8acaf724b Mon Sep 17 00:00:00 2001 From: Huite Bootsma Date: Thu, 8 Feb 2024 16:20:18 +0100 Subject: [PATCH] Do not require shapely. Fixes #172 --- xugrid/ugrid/burn.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/xugrid/ugrid/burn.py b/xugrid/ugrid/burn.py index 9e3f0c67b..76920e5ea 100644 --- a/xugrid/ugrid/burn.py +++ b/xugrid/ugrid/burn.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import List, Union import numba as nb @@ -16,17 +18,11 @@ try: import shapely + except ImportError: shapely = MissingOptionalModule("shapely") -POINT = shapely.GeometryType.POINT -LINESTRING = shapely.GeometryType.LINESTRING -LINEARRING = shapely.GeometryType.LINEARRING -POLYGON = shapely.GeometryType.POLYGON -GEOM_NAMES = {v: k for k, v in shapely.GeometryType.__members__.items()} - - @nb.njit(inline="always") def in_bounds(p: Point, a: Point, b: Point) -> bool: """ @@ -228,7 +224,7 @@ def _burn_lines( def burn_vector_geometry( gdf: "geopandas.GeoDataframe", # type: ignore # noqa like: Union["xugrid.Ugrid2d", "xugrid.UgridDataArray", "xugrid.UgridDataset"], - column: str = None, + column: str | None = None, fill: Union[int, float] = np.nan, all_touched: bool = False, ) -> None: @@ -244,7 +240,7 @@ def burn_vector_geometry( Polygons, points, and/or lines to be burned into the grid. like: UgridDataArray, UgridDataset, or Ugrid2d Grid to burn the vector data into. - column: str + column: str, optional Name of the geodataframe column of which to the values to burn into grid. fill: int, float, optional, default value ``np.nan``. @@ -259,6 +255,12 @@ def burn_vector_geometry( """ import geopandas as gpd + POINT = shapely.GeometryType.POINT + LINESTRING = shapely.GeometryType.LINESTRING + LINEARRING = shapely.GeometryType.LINEARRING + POLYGON = shapely.GeometryType.POLYGON + GEOM_NAMES = {v: k for k, v in shapely.GeometryType.__members__.items()} + if not isinstance(gdf, gpd.GeoDataFrame): raise TypeError(f"gdf must be GeoDataFrame, received: {type(like).__name__}") if isinstance(like, (xugrid.UgridDataArray, xugrid.UgridDataset)):