Skip to content

Commit

Permalink
Add vector reproject inplace
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jan 26, 2024
1 parent 09de140 commit 8a425d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
6 changes: 3 additions & 3 deletions geoutils/raster/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def astype(self, dtype: DTypeLike, inplace: bool = False) -> Raster | None:
:param dtype: Any numpy dtype or string accepted by numpy.astype.
:param inplace: Whether to modify the raster in-place.
:returns: Raster with updated dtype.
:returns: Raster with updated dtype (or None if inplace).
"""
# Check that dtype is supported by rasterio
if not rio.dtypes.check_dtype(dtype):
Expand Down Expand Up @@ -1989,7 +1989,7 @@ def crop(
will match the extent exactly, adjusting the pixel resolution to fit the extent.
:param inplace: Whether to update the raster in-place.
:returns: A new raster, or None if cropping in-place.
:returns: A new raster (or None if inplace).
"""
assert mode in [
"match_extent",
Expand Down Expand Up @@ -2179,7 +2179,7 @@ def reproject(
:param n_threads: Number of threads. Defaults to (os.cpu_count() - 1).
:param memory_limit: Memory limit in MB for warp operations. Larger values may perform better.
:returns: Reprojected raster.
:returns: Reprojected raster (or None if inplace).
"""
# --- Sanity checks on inputs and defaults -- #
Expand Down
48 changes: 43 additions & 5 deletions geoutils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ def crop(
crop_geom: gu.Raster | Vector | list[float] | tuple[float, ...],
clip: bool,
*,
inplace: Literal[False] = ...,
inplace: Literal[False] = False,
) -> VectorType:
...

Expand All @@ -991,7 +991,7 @@ def crop(
crop_geom: gu.Raster | Vector | list[float] | tuple[float, ...],
clip: bool,
*,
inplace: bool = ...,
inplace: bool = False,
) -> VectorType | None:
...

Expand All @@ -1015,7 +1015,9 @@ def crop(
coordinates. If ``crop_geom`` is a raster or a vector, will crop to the bounds. If ``crop_geom`` is a
list of coordinates, the order is assumed to be [xmin, ymin, xmax, ymax].
:param clip: Whether to clip the geometry to the given extent (by default keeps all intersecting).
:param inplace: Update the vector in-place or return copy.
:param inplace: Whether to update the vector in-place.
:returns: Cropped vector (or None if inplace).
"""
if isinstance(crop_geom, (gu.Raster, Vector)):
# For another Vector or Raster, we reproject the bounding box in the same CRS as self
Expand All @@ -1039,11 +1041,42 @@ def crop(
new_vector._ds = new_vector.ds.clip(mask=(xmin, ymin, xmax, ymax))
return new_vector

@overload
def reproject(
self: Vector,
ref: gu.Raster | rio.io.DatasetReader | VectorType | gpd.GeoDataFrame | str | None = None,
crs: CRS | str | int | None = None,
*,
inplace: Literal[False] = False,
) -> Vector:
...

@overload
def reproject(
self: Vector,
ref: gu.Raster | rio.io.DatasetReader | VectorType | gpd.GeoDataFrame | str | None = None,
crs: CRS | str | int | None = None,
*,
inplace: Literal[True],
) -> None:
...

@overload
def reproject(
self: Vector,
ref: gu.Raster | rio.io.DatasetReader | VectorType | gpd.GeoDataFrame | str | None = None,
crs: CRS | str | int | None = None,
*,
inplace: bool = False,
) -> Vector | None:
...

def reproject(
self: Vector,
ref: gu.Raster | rio.io.DatasetReader | VectorType | gpd.GeoDataFrame | str | None = None,
crs: CRS | str | int | None = None,
inplace: bool = False,
) -> Vector | None:
"""
Reproject vector to a specified coordinate reference system.
Expand All @@ -1057,8 +1090,9 @@ def reproject(
Can be provided as a raster, vector, Rasterio dataset, GeoPandas dataframe, or path to the file.
:param crs: Specify the Coordinate Reference System or EPSG to reproject to. If dst_ref not set,
defaults to self.crs.
:param inplace: Whether to update the vector in-place.
:returns: Reprojected vector.
:returns: Reprojected vector (or None if inplace).
"""

# Check that either ref or crs is provided
Expand Down Expand Up @@ -1092,7 +1126,11 @@ def reproject(
# Determine user-input target CRS
crs = CRS.from_user_input(crs)

return Vector(self.ds.to_crs(crs=crs))
if inplace:
self.ds = self.ds.to_crs(crs=crs)
return None
else:
return Vector(self.ds.to_crs(crs=crs))

@overload
def create_mask(
Expand Down

0 comments on commit 8a425d9

Please sign in to comment.