Skip to content

Commit

Permalink
Finalize linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Nov 30, 2024
1 parent 8e0a45f commit c72943c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
42 changes: 22 additions & 20 deletions geoutils/pointcloud/pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -74,7 +73,7 @@ def _load_laspy_metadata(
# return


class PointCloud(gu.Vector):
class PointCloud(gu.Vector): # type: ignore[misc]
"""
The georeferenced point cloud.
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion geoutils/vector/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import pathlib
import warnings
from collections import abc
from os import PathLike
from typing import (
Expand Down
4 changes: 1 addition & 3 deletions tests/test_pointcloud/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit c72943c

Please sign in to comment.