Skip to content

Commit 8e0a45f

Browse files
committed
Partial linting
1 parent 492ba19 commit 8e0a45f

File tree

6 files changed

+118
-82
lines changed

6 files changed

+118
-82
lines changed

geoutils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
from geoutils import examples, projtools # noqa
66
from geoutils._config import config # noqa
7+
from geoutils.pointcloud import PointCloud # noqa
78
from geoutils.raster import Mask, Raster # noqa
89
from geoutils.vector import Vector # noqa
9-
from geoutils.pointcloud import PointCloud # noqa
1010

1111
try:
1212
from geoutils._version import __version__ as __version__ # noqa

geoutils/interface/raster_point.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ def _raster_to_pointcloud(
228228

229229
if not as_array:
230230
pc = gpd.GeoDataFrame(
231-
pixel_data.T,
232-
columns=all_column_names,
233-
geometry=gpd.points_from_xy(x_coords_2, y_coords_2),
234-
crs=source_raster.crs,
235-
)
231+
pixel_data.T,
232+
columns=all_column_names,
233+
geometry=gpd.points_from_xy(x_coords_2, y_coords_2),
234+
crs=source_raster.crs,
235+
)
236236
return pc
237237
else:
238238
# Merge the coordinates and pixel data an array of N x K

geoutils/pointcloud/pointcloud.py

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
"""Module for PointCloud class."""
2+
23
from __future__ import annotations
34

45
import os.path
5-
import warnings
6-
from typing import Iterable, Literal, Any
76
import pathlib
7+
import warnings
8+
from typing import Any, Iterable, Literal
89

10+
import geopandas as gpd
11+
import numpy as np
912
import pandas as pd
1013
from pyproj import CRS
11-
import numpy as np
12-
import geopandas as gpd
1314
from rasterio.coords import BoundingBox
1415
from shapely.geometry.base import BaseGeometry
1516

1617
import geoutils as gu
18+
from geoutils._typing import ArrayLike, NDArrayNum, Number
1719
from geoutils.interface.gridding import _grid_pointcloud
18-
from geoutils.raster.sampling import subsample_array
1920
from geoutils.interface.raster_point import _raster_to_pointcloud
20-
from geoutils._typing import Number, NDArrayNum, ArrayLike
21+
from geoutils.raster.sampling import subsample_array
2122

2223
try:
2324
import laspy
25+
2426
has_laspy = True
2527
except ImportError:
2628
has_laspy = False
@@ -36,15 +38,18 @@ def _load_laspy_data(filename: str, columns: list[str]) -> gpd.GeoDataFrame:
3638
data = np.vstack([las[n] for n in columns]).T
3739

3840
# Build geodataframe
39-
gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=las.x, y=las.y,
40-
crs=las.header.parse_crs(prefer_wkt=False)),
41-
data=data,
42-
columns=columns)
41+
gdf = gpd.GeoDataFrame(
42+
geometry=gpd.points_from_xy(x=las.x, y=las.y, crs=las.header.parse_crs(prefer_wkt=False)),
43+
data=data,
44+
columns=columns,
45+
)
4346

4447
return gdf
4548

4649

47-
def _load_laspy_metadata(filename: str, ) -> tuple[CRS, int, BoundingBox, pd.Index]:
50+
def _load_laspy_metadata(
51+
filename: str,
52+
) -> tuple[CRS, int, BoundingBox, pd.Index]:
4853
"""Load point cloud metadata from LAS/LAZ/COPC file."""
4954

5055
with laspy.open(filename) as f:
@@ -62,7 +67,7 @@ def _load_laspy_metadata(filename: str, ) -> tuple[CRS, int, BoundingBox, pd.Ind
6267
#
6368
# with laspy.open(filename) as f:
6469
# new_hdr = laspy.LasHeader(version="1.4", point_format=6)
65-
# # You can set the scales and offsets to values tha suits your data
70+
# # You can set the scales and offsets to values that suits your data
6671
# new_hdr.scales = np.array([1.0, 0.5, 0.1])
6772
# new_las = laspy.LasData(header = new_hdr, points=)
6873
#
@@ -91,9 +96,11 @@ class PointCloud(gu.Vector):
9196
See the API for more details.
9297
"""
9398

94-
def __init__(self,
95-
filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry,
96-
data_column: str | None = "z"):
99+
def __init__(
100+
self,
101+
filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry,
102+
data_column: str | None = "z",
103+
):
97104
"""
98105
Instantiate a point cloud from either a data column name and a vector (filename, GeoPandas dataframe or series,
99106
or a Shapely geometry), or only with a point cloud file type.
@@ -117,8 +124,10 @@ def __init__(self,
117124
return
118125
# For filename, rely on parent Vector class or LAS file reader
119126
else:
120-
if isinstance(filename_or_dataset, (str, pathlib.Path)) and \
121-
os.path.splitext(filename_or_dataset)[-1] in [".las", ".laz"]:
127+
if isinstance(filename_or_dataset, (str, pathlib.Path)) and os.path.splitext(filename_or_dataset)[-1] in [
128+
".las",
129+
".laz",
130+
]:
122131
# Load only metadata, and not the data
123132
crs, nb_points, bounds, columns = _load_laspy_metadata(filename_or_dataset)
124133
self._name = filename_or_dataset
@@ -132,19 +141,18 @@ def __init__(self,
132141
super().__init__(filename_or_dataset)
133142
# Verify that the vector can be cast as a point cloud
134143
if not all(p == "Point" for p in self.ds.geom_type):
135-
raise ValueError("This vector file contains non-point geometries, "
136-
"cannot be instantiated as a point cloud.")
144+
raise ValueError(
145+
"This vector file contains non-point geometries, " "cannot be instantiated as a point cloud."
146+
)
137147

138148
# Set data column following user input
139149
self.set_data_column(new_data_column=data_column)
140150

141-
142151
# TODO: Could also move to Vector directly?
143152
##############################################
144153
# OVERRIDDEN VECTOR METHODS TO SUPPORT LOADING
145154
##############################################
146155

147-
148156
@property
149157
def ds(self) -> gpd.GeoDataFrame:
150158
"""Geodataframe of the point cloud."""
@@ -168,15 +176,15 @@ def ds(self, new_ds: gpd.GeoDataFrame | gpd.GeoSeries) -> None:
168176
@property
169177
def crs(self) -> CRS:
170178
"""Coordinate reference system of the vector."""
171-
# Overridding method in Vector
179+
# Overriding method in Vector
172180
if self.is_loaded:
173181
return super().crs
174182
else:
175183
return self._crs
176184

177185
@property
178186
def bounds(self) -> BoundingBox:
179-
# Overridding method in Vector
187+
# Overriding method in Vector
180188
if self.is_loaded:
181189
return super().bounds
182190
else:
@@ -185,7 +193,7 @@ def bounds(self) -> BoundingBox:
185193
@property
186194
def all_columns(self) -> pd.Index:
187195
"""Index of all columns of the point cloud, excluding the column of 2D point geometries."""
188-
# Overridding method in Vector
196+
# Overriding method in Vector
189197
if self.is_loaded:
190198
all_columns = super().columns
191199
all_columns_nongeom = all_columns[all_columns != "geometry"]
@@ -210,8 +218,10 @@ def set_data_column(self, new_data_column: str) -> None:
210218
"""Set new column as point cloud data column."""
211219

212220
if new_data_column not in self.all_columns:
213-
raise ValueError(f"Data column {new_data_column} not found among columns, available columns "
214-
f"are: {', '.join(self.all_columns)}.")
221+
raise ValueError(
222+
f"Data column {new_data_column} not found among columns, available columns "
223+
f"are: {', '.join(self.all_columns)}."
224+
)
215225
self._data_column = new_data_column
216226

217227
@property
@@ -266,13 +276,16 @@ def from_array(cls, array: NDArrayNum, crs: CRS, data_column: str | None = "z")
266276
array_in = array
267277

268278
# Build geodataframe
269-
gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy(x=array_in[0, :], y=array_in[1, :], crs=crs),
270-
data={data_column: array_in[2, :]})
279+
gdf = gpd.GeoDataFrame(
280+
geometry=gpd.points_from_xy(x=array_in[0, :], y=array_in[1, :], crs=crs), data={data_column: array_in[2, :]}
281+
)
271282

272283
return cls(filename_or_dataset=gdf, data_column=data_column)
273284

274285
@classmethod
275-
def from_tuples(cls, tuples_xyz: Iterable[tuple[Number, Number, Number]], crs: CRS, data_column: str | None = "z") -> PointCloud:
286+
def from_tuples(
287+
cls, tuples_xyz: Iterable[tuple[Number, Number, Number]], crs: CRS, data_column: str | None = "z"
288+
) -> PointCloud:
276289
"""Create point cloud from an iterable of 3-tuples (X coordinate, Y coordinate, Z value)."""
277290

278291
return cls.from_array(np.array(tuples_xyz), crs=crs, data_column=data_column)
@@ -316,30 +329,40 @@ def pointcloud_equal(self, other: PointCloud, **kwargs: Any):
316329

317330
return all([vector_eq, data_column_eq])
318331

319-
def grid(self,
320-
ref: gu.Raster | None,
321-
grid_coords: tuple[np.ndarray, np.ndarray] | None,
322-
resampling: Literal["nearest", "linear", "cubic"],
323-
dist_nodata_pixel: float = 1.) -> gu.Raster:
332+
def grid(
333+
self,
334+
ref: gu.Raster | None,
335+
grid_coords: tuple[np.ndarray, np.ndarray] | None,
336+
resampling: Literal["nearest", "linear", "cubic"],
337+
dist_nodata_pixel: float = 1.0,
338+
) -> gu.Raster:
324339
"""Grid point cloud into a raster."""
325340

326341
if isinstance(ref, gu.Raster):
327342
if grid_coords is None:
328-
warnings.warn("Both reference raster and grid coordinates were passed for gridding, "
329-
"using only the reference raster.")
343+
warnings.warn(
344+
"Both reference raster and grid coordinates were passed for gridding, "
345+
"using only the reference raster."
346+
)
330347
grid_coords = ref.coords(grid=False)
331348
else:
332349
grid_coords = grid_coords
333350

334-
array, transform = _grid_pointcloud(self.ds, grid_coords=grid_coords, data_column_name=self.data_column,
335-
resampling=resampling, dist_nodata_pixel=dist_nodata_pixel)
351+
array, transform = _grid_pointcloud(
352+
self.ds,
353+
grid_coords=grid_coords,
354+
data_column_name=self.data_column,
355+
resampling=resampling,
356+
dist_nodata_pixel=dist_nodata_pixel,
357+
)
336358

337359
return gu.Raster.from_array(data=array, transform=transform, crs=self.crs, nodata=None)
338360

339361
def subsample(self, subsample: float | int, random_state: int | np.random.Generator | None = None) -> PointCloud:
340362

341-
subsample = subsample_array(array=self.ds[self.data_column].values, subsample=subsample,
342-
return_indices=True, random_state=random_state)
363+
subsample = subsample_array(
364+
array=self.ds[self.data_column].values, subsample=subsample, return_indices=True, random_state=random_state
365+
)
343366

344367
return PointCloud(self.ds[subsample])
345368

geoutils/raster/raster.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3390,18 +3390,20 @@ def to_pointcloud(
33903390
:returns: A point cloud, or array of the shape (N, 2 + count) where N is the sample count.
33913391
"""
33923392

3393-
return gu.PointCloud(_raster_to_pointcloud(
3394-
source_raster=self,
3395-
data_column_name=data_column_name,
3396-
data_band=data_band,
3397-
auxiliary_data_bands=auxiliary_data_bands,
3398-
auxiliary_column_names=auxiliary_column_names,
3399-
subsample=subsample,
3400-
skip_nodata=skip_nodata,
3401-
as_array=as_array,
3402-
random_state=random_state,
3403-
force_pixel_offset=force_pixel_offset,
3404-
))
3393+
return gu.PointCloud(
3394+
_raster_to_pointcloud(
3395+
source_raster=self,
3396+
data_column_name=data_column_name,
3397+
data_band=data_band,
3398+
auxiliary_data_bands=auxiliary_data_bands,
3399+
auxiliary_column_names=auxiliary_column_names,
3400+
subsample=subsample,
3401+
skip_nodata=skip_nodata,
3402+
as_array=as_array,
3403+
random_state=random_state,
3404+
force_pixel_offset=force_pixel_offset,
3405+
)
3406+
)
34053407

34063408
@classmethod
34073409
def from_pointcloud_regular(

geoutils/vector/vector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class Vector:
6464
See the API for more details.
6565
"""
6666

67-
def __init__(self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry | dict[str, Any]):
67+
def __init__(
68+
self, filename_or_dataset: str | pathlib.Path | gpd.GeoDataFrame | gpd.GeoSeries | BaseGeometry | dict[str, Any]
69+
):
6870
"""
6971
Instantiate a vector from either a filename, a GeoPandas dataframe or series, or a Shapely geometry.
7072

0 commit comments

Comments
 (0)