Skip to content

Commit

Permalink
ENH: Only accept polygon as input. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Jan 17, 2025
1 parent 8d5467c commit 746704d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/pynldas2/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

if TYPE_CHECKING:
from numpy.typing import NDArray
from shapely.geometry.base import BaseGeometry
from shapely import Polygon

CRSType = int | str | pyproj.CRS
PolyType = Polygon | MultiPolygon | tuple[float, float, float, float]
PolyType = Polygon | tuple[float, float, float, float]
Number = int | float | np.number

__all__ = [
Expand Down Expand Up @@ -73,7 +73,7 @@ def transform_coords(
return list(zip(x_proj, y_proj))


def _geo_transform(geom: BaseGeometry, in_crs: CRSType, out_crs: CRSType) -> BaseGeometry:
def _geo_transform(geom: Polygon, in_crs: CRSType, out_crs: CRSType) -> Polygon:
"""Transform a geometry from one CRS to another."""
project = TransformerFromCRS(in_crs, out_crs, always_xy=True).transform
return ops.transform(project, geom)
Expand All @@ -93,10 +93,10 @@ def validate_coords(


def to_geometry(
geometry: BaseGeometry | tuple[float, float, float, float],
geometry: Polygon | tuple[float, float, float, float],
geo_crs: CRSType | None = None,
crs: CRSType | None = None,
) -> BaseGeometry:
) -> Polygon:
"""Return a Shapely geometry and optionally transformed to a new CRS.
Parameters
Expand All @@ -110,7 +110,7 @@ def to_geometry(
Returns
-------
shapely.geometry.base.BaseGeometry
shapely.Polygon
A shapely geometry object.
"""
is_geom = np.atleast_1d(shapely.is_geometry(geometry))
Expand All @@ -130,7 +130,7 @@ def to_geometry(

def clip_dataset(
ds: xr.Dataset,
geometry: Polygon | MultiPolygon | tuple[float, float, float, float],
geometry: Polygon | tuple[float, float, float, float],
crs: CRSType,
) -> xr.Dataset:
"""Mask a ``xarray.Dataset`` based on a geometry."""
Expand All @@ -139,7 +139,7 @@ def clip_dataset(
geom = to_geometry(geometry, crs, ds.rio.crs)
try:
ds = ds.rio.clip_box(*geom.bounds, auto_expand=True)
if isinstance(geometry, (Polygon, MultiPolygon)):
if isinstance(geometry, Polygon):
ds = ds.rio.clip([geom], all_touched=True)
except OneDimensionalRaster:
ds = ds.rio.clip([geom], all_touched=True)
Expand Down
7 changes: 4 additions & 3 deletions src/pynldas2/pynldas2.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def _txt2da(


def get_bygeom(
geometry: Polygon | MultiPolygon | tuple[float, float, float, float],
geometry: Polygon | tuple[float, float, float, float],
start_date: str,
end_date: str,
geo_crs: CRSType = 4326,
Expand All @@ -453,8 +453,9 @@ def get_bygeom(
Parameters
----------
geometry : shapely.Polygon, shapely.MultiPolygon, or tuple of length 4
Input polygon or a bounding box like so (xmin, ymin, xmax, ymax).
geometry : Polygon or tuple
The geometry of the region of interest. It can be a shapely Polygon or a tuple
of length 4 representing the bounding box (minx, miny, maxx, maxy).
start_date : str
Start date of the data.
end_date : str
Expand Down

0 comments on commit 746704d

Please sign in to comment.