Skip to content

Commit

Permalink
MNT: Fix or skip issues raised by pyright. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Feb 9, 2024
1 parent 577a8cb commit c9c6cb8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
34 changes: 16 additions & 18 deletions pygeoutils/geotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class Coordinates:
@staticmethod
def __box_geo(bounds: tuple[float, float, float, float] | None) -> Polygon:
"""Get EPSG:4326 CRS."""
wgs84_bounds = pyproj.CRS(
4326
).area_of_use.bounds # pyright: ignore[reportOptionalMemberAccess]
wgs84_bounds = pyproj.CRS(4326).area_of_use.bounds # pyright: ignore[reportOptionalMemberAccess]
if bounds is None:
return shapely.box(*wgs84_bounds)

Expand All @@ -115,7 +113,7 @@ def __box_geo(bounds: tuple[float, float, float, float] | None) -> Polygon:
@staticmethod
def __validate(pts: gpd.GeoSeries, bbox: Polygon) -> gpd.GeoSeries:
"""Create a ``geopandas.GeoSeries`` from valid coords within a bounding box."""
return pts[pts.sindex.query(bbox)].sort_index()
return pts[pts.sindex.query(bbox)].sort_index() # pyright: ignore[reportReturnType]

def __post_init__(self) -> None:
"""Normalize the longitude value within [-180, 180)."""
Expand Down Expand Up @@ -144,7 +142,7 @@ def geometry_reproject(geom: GEOM, in_crs: CRSTYPE, out_crs: CRSTYPE) -> GEOM:
Parameters
----------
geom : list or tuple or any shapely.geometry
geom : list or tuple or any shapely.GeometryType
Input geometry could be a list of coordinates such as ``[(x1, y1), ...]``,
a bounding box like so ``(xmin, ymin, xmax, ymax)``, or any valid ``shapely``'s
geometry such as ``Polygon``, ``MultiPolygon``, etc..
Expand Down Expand Up @@ -192,24 +190,24 @@ def geometry_reproject(geom: GEOM, in_crs: CRSTYPE, out_crs: CRSTYPE) -> GEOM:
if len(geom) > 4:
raise TypeError
if pyproj.CRS(in_crs) == pyproj.CRS(out_crs):
bbox = shapely.box(*geom)
bbox = shapely.box(*geom) # pyright: ignore[reportArgumentType]
else:
bbox = cast("Polygon", ops.transform(project, shapely.box(*geom)))
return tuple(float(p) for p in bbox.bounds)
bbox = cast("Polygon", ops.transform(project, shapely.box(*geom))) # pyright: ignore[reportArgumentType]
return tuple(float(p) for p in bbox.bounds) # pyright: ignore[reportReturnType]

with contextlib.suppress(TypeError, AttributeError, ValueError):
if pyproj.CRS(in_crs) == pyproj.CRS(out_crs):
point = Point(geom)
else:
point = cast("Point", ops.transform(project, Point(geom)))
return [(float(point.x), float(point.y))]
return [(float(point.x), float(point.y))] # pyright: ignore[reportReturnType]

with contextlib.suppress(TypeError, AttributeError, ValueError):
if pyproj.CRS(in_crs) == pyproj.CRS(out_crs):
mp = MultiPoint(geom)
else:
mp = cast("MultiPoint", ops.transform(project, MultiPoint(geom)))
return [(float(p.x), float(p.y)) for p in mp.geoms]
return [(float(p.x), float(p.y)) for p in mp.geoms] # pyright: ignore[reportReturnType]

gtypes = " ".join(
(
Expand Down Expand Up @@ -245,7 +243,7 @@ def geo2polygon(
if isinstance(geometry, (Polygon, MultiPolygon)):
geom = geometry
elif isinstance(geometry, (tuple, list)) and len(geometry) == 4:
geom = shapely.box(*geometry)
geom = shapely.box(*geometry) # pyright: ignore[reportArgumentType]
else:
raise InputTypeError("geometry", "(Multi)Polygon or tuple of length 4")

Expand Down Expand Up @@ -760,15 +758,15 @@ def break_lines(lines: GDFTYPE, points: gpd.GeoDataFrame, tol: float = 0.0) -> G

crs_proj = lines.crs
if tol > 0.0:
points = snap2nearest(lines, points, tol)
points = snap2nearest(lines, points, tol) # pyright: ignore[reportArgumentType]

mlines = lines.geom_type == "MultiLineString"
if mlines.any():
lines.loc[mlines, "geometry"] = lines.loc[mlines, "geometry"].apply(lambda g: list(g.geoms))
lines = lines.explode("geometry").set_crs(crs_proj)
lines = lines.explode("geometry").set_crs(crs_proj) # pyright: ignore[reportAssignmentType,reportArgumentType]

lines = lines.reset_index(drop=True)
points = points.reset_index(drop=True)
lines = lines.reset_index(drop=True) # pyright: ignore[reportAssignmentType]
points = points.reset_index(drop=True) # pyright: ignore[reportAssignmentType]
pts_idx, flw_idx = lines.sindex.query(points.geometry, predicate="intersects")
if len(pts_idx) == 0:
msg = "No intersection between lines and points"
Expand All @@ -786,7 +784,7 @@ def break_lines(lines: GDFTYPE, points: gpd.GeoDataFrame, tol: float = 0.0) -> G
crs=crs_proj,
index=idx,
)
return gpd.GeoDataFrame(
return gpd.GeoDataFrame( # pyright: ignore[reportReturnType]
lines.loc[idx].drop(columns="geometry"), geometry=broken_lines, crs=crs_proj
).to_crs(lines.crs)

Expand Down Expand Up @@ -817,7 +815,7 @@ def geometry_list(geometry: GEOM) -> list[Polygon] | list[Point] | list[LineStri
and len(geometry) == 4
and all(isinstance(i, (float, int, np.number)) for i in geometry)
):
return [shapely.box(*geometry)]
return [shapely.box(*geometry)] # pyright: ignore[reportArgumentType]

with contextlib.suppress(TypeError, AttributeError):
return list(MultiPoint(geometry).geoms)
Expand Down Expand Up @@ -898,7 +896,7 @@ def nested_polygons(gdf: gpd.GeoDataFrame | gpd.GeoSeries) -> dict[int | str, li


def coords_list(
coords: tuple[float, float] | list[tuple[float, float]] | FloatArray
coords: tuple[float, float] | list[tuple[float, float]] | FloatArray,
) -> list[tuple[float, float]]:
"""Convert a single coordinate or list of coordinates to a list of coordinates.
Expand Down
14 changes: 7 additions & 7 deletions pygeoutils/pygeoutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def xarray_geomask(
def _to_dataset(
resp: bytes,
var_name: str,
driver: str,
driver: str | None,
dtypes: dict[str, np.dtype], # pyright: ignore[reportMissingTypeArgument]
nodata_dict: dict[str, NUMBER],
nodata: NUMBER | None,
Expand Down Expand Up @@ -269,7 +269,7 @@ def gtiff2xarray(
var_name = {lyr: "_".join(lyr.split("_")[:-3]) for lyr in r_dict}

attrs = utils.get_gtiff_attrs(r_dict[key1], ds_dims, driver, nodata)
dtypes: dict[str, type] = {}
dtypes: dict[str, np.dtype] = {} # pyright: ignore[reportMissingTypeArgument]
nodata_dict: dict[str, NUMBER] = {}

ds = xr.merge(
Expand Down Expand Up @@ -297,7 +297,7 @@ def gtiff2xarray(
ds[v].attrs["nodatavals"] = (nodata_dict[v],)
ds[v] = ds[v].rio.write_nodata(nodata_dict[v])

ds = utils.xd_write_crs(ds)
ds = utils.xd_write_crs(ds) # pyright: ignore[reportArgumentType]
if geometry is not None:
if geo_crs is None:
raise MissingCRSError
Expand All @@ -311,11 +311,11 @@ def _path2str(path: Path | str) -> str:


@overload
def _path2str(path: list[Path | str]) -> list[str]:
def _path2str(path: list[Path] | list[str]) -> list[str]:
...


def _path2str(path: Path | str | list[Path | str]) -> str | list[str]:
def _path2str(path: Path | str | list[Path] | list[str]) -> str | list[str]:
if isinstance(path, (list, tuple)):
return [Path(p).resolve().as_posix() for p in path]
return Path(path).resolve().as_posix()
Expand Down Expand Up @@ -407,7 +407,7 @@ def xarray2geodf(
geojsons, values = zip(*shapes)
geojsons = cast("tuple[dict[str, Any], ...]", geojsons)
return gpd.GeoDataFrame(
data={str(da.name): np.array(values, dtype)},
{str(da.name): np.array(values, dtype)},
geometry=[sgeom.shape(g) for g in geojsons],
crs=crs,
)
Expand Down Expand Up @@ -470,7 +470,7 @@ def geodf2xarray(
out_shape=(height, width),
transform=affine,
dtype=dtype,
fill=fill,
fill=fill, # pyright: ignore[reportArgumentType]
),
coords={"x": np.linspace(west, east, width), "y": np.linspace(north, south, height)},
dims=("y", "x"),
Expand Down

0 comments on commit c9c6cb8

Please sign in to comment.