Skip to content

Commit

Permalink
Add all tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Jan 27, 2024
1 parent 4f07d9e commit 48ee983
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions geoutils/raster/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,8 @@ def reproject(
self._crs = crs
self._nodata = nodata
self._transform = transform
# A little trick to force the right shape of data in, then update the mask properly through the data setter
self._data = data.squeeze()
self.data = data
return None
else:
Expand Down
22 changes: 17 additions & 5 deletions geoutils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,20 @@ def ds(self, new_ds: gpd.GeoDataFrame | gpd.GeoSeries) -> None:
else:
raise ValueError("The dataset of a vector must be set with a GeoSeries or a GeoDataFrame.")

def vector_equal(self, other: gu.Vector) -> bool:
"""Check if two vectors are equal."""
def vector_equal(self, other: gu.Vector, **kwargs: Any) -> bool:
"""
Check if two vectors are equal.
Keyword arguments are passed to geopandas.assert_geodataframe_equal.
"""

return assert_geodataframe_equal(self.ds, other.ds)
try:
assert_geodataframe_equal(self.ds, other.ds, **kwargs)
vector_eq = True
except AssertionError:
vector_eq = False

return vector_eq

@property
def name(self) -> str | None:
Expand Down Expand Up @@ -1126,11 +1136,13 @@ def reproject(
# Determine user-input target CRS
crs = CRS.from_user_input(crs)

new_ds = self.ds.to_crs(crs=crs)

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

@overload
def create_mask(
Expand Down
25 changes: 24 additions & 1 deletion tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,14 @@ def test_shift(self, example: str) -> None:
orig_bounds = r.bounds

# Shift raster by georeferenced units (default)
# Check the default behaviour is not inplace
r_notinplace = r.shift(xoff=1, yoff=1)
assert isinstance(r_notinplace, gu.Raster)

# Check inplace
r.shift(xoff=1, yoff=1, inplace=True)
# Both shifts should have yielded the same transform
assert r.transform == r_notinplace.transform

# Only bounds should change
assert orig_transform.c + 1 == r.transform.c
Expand Down Expand Up @@ -1476,7 +1483,7 @@ def test_reproject(self, example: str) -> None:

plt.show()

# - Check that if mask is modified afterwards, it is taken into account during reproject - #
# -- Check that if mask is modified afterwards, it is taken into account during reproject -- #
# Create a raster with (additional) random gaps
r_gaps = r.copy()
nsamples = 200
Expand All @@ -1503,6 +1510,22 @@ def test_reproject(self, example: str) -> None:
r3 = r_nodata.reproject(r2)
assert r_nodata.nodata == r3.nodata

# -- Check inplace behaviour works -- #

# Check when transform is updated (via res)
r_tmp_res = r.copy()
r_res = r_tmp_res.reproject(res=r.res[0]/2)
r_tmp_res.reproject(res=r.res[0]/2, inplace=True)

assert r_res.raster_equal(r_tmp_res)

# Check when CRS is updated
r_tmp_crs = r.copy()
r_crs = r_tmp_crs.reproject(crs=out_crs)
r_tmp_crs.reproject(crs=out_crs, inplace=True)

assert r_crs.raster_equal(r_tmp_crs)

# -- Test additional errors raised for argument combinations -- #

# If both ref and crs are set
Expand Down
5 changes: 5 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def test_reproject(self) -> None:
assert isinstance(v1, gu.Vector)
assert v1.crs.to_epsg() == 32617

# Check the inplace behaviour matches the not-inplace one
v2 = v0.copy()
v2.reproject(crs=32617, inplace=True)
v2.vector_equal(v1)

# Check that the reprojection is the same as with geopandas
gpd1 = v0.ds.to_crs(epsg=32617)
assert_geodataframe_equal(gpd1, v1.ds)
Expand Down

0 comments on commit 48ee983

Please sign in to comment.