From c72943c3634b70741ba97b49e9b6d79fd5f25bd3 Mon Sep 17 00:00:00 2001 From: Romain Hugonnet Date: Sat, 30 Nov 2024 14:30:17 -0900 Subject: [PATCH] Finalize linting --- geoutils/pointcloud/pointcloud.py | 42 +++++++++++++----------- geoutils/vector/vector.py | 1 - tests/test_pointcloud/test_pointcloud.py | 4 +-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/geoutils/pointcloud/pointcloud.py b/geoutils/pointcloud/pointcloud.py index 6e786d43..7591a705 100644 --- a/geoutils/pointcloud/pointcloud.py +++ b/geoutils/pointcloud/pointcloud.py @@ -17,7 +17,6 @@ import geoutils as gu from geoutils._typing import ArrayLike, NDArrayNum, Number from geoutils.interface.gridding import _grid_pointcloud -from geoutils.interface.raster_point import _raster_to_pointcloud from geoutils.raster.sampling import subsample_array try: @@ -74,7 +73,7 @@ def _load_laspy_metadata( # return -class PointCloud(gu.Vector): +class PointCloud(gu.Vector): # type: ignore[misc] """ The georeferenced point cloud. @@ -99,7 +98,7 @@ class PointCloud(gu.Vector): def __init__( self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry, - data_column: str | None = "z", + data_column: str = "z", ): """ Instantiate a point cloud from either a data column name and a vector (filename, GeoPandas dataframe or series, @@ -112,10 +111,10 @@ def __init__( self._ds: gpd.GeoDataFrame | None = None self._name: str | None = None self._crs: CRS | None = None - self._bounds: BoundingBox | None = None - self._data_column: str | None = None - self._nb_points: int | None = None - self._columns: pd.Index | None = None + self._bounds: BoundingBox + self._data_column: str + self._nb_points: int + self._columns: pd.Index # If PointCloud is passed, simply point back to PointCloud if isinstance(filename_or_dataset, PointCloud): @@ -129,8 +128,9 @@ def __init__( ".laz", ]: # Load only metadata, and not the data - crs, nb_points, bounds, columns = _load_laspy_metadata(filename_or_dataset) - self._name = filename_or_dataset + fn = filename_or_dataset if isinstance(filename_or_dataset, str) else filename_or_dataset.name + crs, nb_points, bounds, columns = _load_laspy_metadata(fn) + self._name = fn self._crs = crs self._nb_points = nb_points self._columns = columns @@ -238,7 +238,7 @@ def nb_points(self) -> int: else: return self._nb_points - def load(self, columns: Literal["all", "main"] | list[str] = "main"): + def load(self, columns: Literal["all", "main"] | list[str] = "main") -> None: """ Load point cloud from disk (only supported for LAS files). @@ -254,15 +254,17 @@ def load(self, columns: Literal["all", "main"] | list[str] = "main"): ) if columns == "all": - columns = self.all_columns + columns_to_load = self.all_columns elif columns == "main": - columns = [self.data_column] + columns_to_load = [self.data_column] + else: + columns_to_load = columns - ds = _load_laspy_data(filename=self.name, columns=columns) + ds = _load_laspy_data(filename=self.name, columns=columns_to_load) self._ds = ds @classmethod - def from_array(cls, array: NDArrayNum, crs: CRS, data_column: str | None = "z") -> PointCloud: + def from_array(cls, array: NDArrayNum, crs: CRS, data_column: str = "z") -> PointCloud: """Create point cloud from a 3 x N or N x 3 array of X coordinate, Y coordinates and Z values.""" # Check shape @@ -284,14 +286,14 @@ def from_array(cls, array: NDArrayNum, crs: CRS, data_column: str | None = "z") @classmethod def from_tuples( - cls, tuples_xyz: Iterable[tuple[Number, Number, Number]], crs: CRS, data_column: str | None = "z" + cls, tuples_xyz: Iterable[tuple[Number, Number, Number]], crs: CRS, data_column: str = "z" ) -> PointCloud: """Create point cloud from an iterable of 3-tuples (X coordinate, Y coordinate, Z value).""" return cls.from_array(np.array(tuples_xyz), crs=crs, data_column=data_column) @classmethod - def from_xyz(cls, x: ArrayLike, y: ArrayLike, z: ArrayLike, crs: CRS, data_column: str | None = "z") -> PointCloud: + def from_xyz(cls, x: ArrayLike, y: ArrayLike, z: ArrayLike, crs: CRS, data_column: str = "z") -> PointCloud: """Create point cloud from three 1D array-like coordinates for X/Y/Z.""" return cls.from_array(np.stack((x, y, z)), crs=crs, data_column=data_column) @@ -311,7 +313,7 @@ def to_xyz(self) -> tuple[NDArrayNum, NDArrayNum, NDArrayNum]: return self.geometry.x.values, self.geometry.y.values, self.ds[self.data_column].values - def pointcloud_equal(self, other: PointCloud, **kwargs: Any): + def pointcloud_equal(self, other: PointCloud, **kwargs: Any) -> bool: """ Check if two point clouds are equal. @@ -332,7 +334,7 @@ def pointcloud_equal(self, other: PointCloud, **kwargs: Any): def grid( self, ref: gu.Raster | None, - grid_coords: tuple[np.ndarray, np.ndarray] | None, + grid_coords: tuple[NDArrayNum, NDArrayNum] | None, resampling: Literal["nearest", "linear", "cubic"], dist_nodata_pixel: float = 1.0, ) -> gu.Raster: @@ -360,11 +362,11 @@ def grid( def subsample(self, subsample: float | int, random_state: int | np.random.Generator | None = None) -> PointCloud: - subsample = subsample_array( + indices = subsample_array( array=self.ds[self.data_column].values, subsample=subsample, return_indices=True, random_state=random_state ) - return PointCloud(self.ds[subsample]) + return PointCloud(self.ds[indices]) # @classmethod # def from_raster(cls, raster: gu.Raster) -> PointCloud: diff --git a/geoutils/vector/vector.py b/geoutils/vector/vector.py index bfaf4567..d79917db 100644 --- a/geoutils/vector/vector.py +++ b/geoutils/vector/vector.py @@ -5,7 +5,6 @@ from __future__ import annotations import pathlib -import warnings from collections import abc from os import PathLike from typing import ( diff --git a/tests/test_pointcloud/test_pointcloud.py b/tests/test_pointcloud/test_pointcloud.py index cd7cae3d..dadca55b 100644 --- a/tests/test_pointcloud/test_pointcloud.py +++ b/tests/test_pointcloud/test_pointcloud.py @@ -6,8 +6,6 @@ import numpy as np import pytest from geopandas.testing import assert_geodataframe_equal -from pyproj import CRS -from rasterio.coords import BoundingBox from shapely import Polygon from geoutils import PointCloud @@ -169,7 +167,7 @@ def test_from_array(self) -> None: pc_from_arr = PointCloud.from_array(array=self.arr_points.T, crs=4326, data_column="b1") assert pc_from_arr.pointcloud_equal(pc1) - def test_from_array__errors(self): + def test_from_array__errors(self) -> None: """Test errors raised during creation with array.""" array = np.ones((4, 5))