Skip to content

Commit dc95e42

Browse files
add unit tests with multiple wkb and xy columns
1 parent 6279ec5 commit dc95e42

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

climada/hazard/centroids/centr.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ def __eq__(self, other):
177177

178178
try:
179179
pd.testing.assert_frame_equal(self.gdf, other.gdf, check_like=True)
180-
return True
181180
except AssertionError:
182181
return False
183182

183+
if not (self.gdf.geometry == other.gdf.geometry).all():
184+
return False
185+
186+
return True
187+
184188
def to_default_crs(self, inplace=True):
185189
"""Project the current centroids to the default CRS (epsg4326)
186190
@@ -483,11 +487,11 @@ def plot(self, *, axis=None, figsize=(9, 13), **kwargs):
483487
-------
484488
ax : cartopy.mpl.geoaxes.GeoAxes instance
485489
"""
486-
if axis == None:
490+
if axis is None:
487491
fig, axis = plt.subplots(
488492
figsize=figsize, subplot_kw={"projection": ccrs.PlateCarree()}
489493
)
490-
if type(axis) != cartopy.mpl.geoaxes.GeoAxes:
494+
if type(axis) is not cartopy.mpl.geoaxes.GeoAxes:
491495
raise AttributeError(
492496
f"The axis provided is of type: {type(axis)} "
493497
"The function requires a cartopy.mpl.geoaxes.GeoAxes."
@@ -964,13 +968,19 @@ def from_hdf5(cls, file_name):
964968
# the CRS was stored in '_crs'/'crs'
965969
crs = metadata.get("crs")
966970
gdf = gpd.GeoDataFrame(store["centroids"])
967-
for xycol in metadata.get("xy_columns", []):
968-
gdf[xycol] = gpd.points_from_xy(
969-
x=gdf[xycol + ".x"], y=gdf[xycol + ".y"], crs=crs
970-
)
971-
gdf.drop(columns=[xycol + ".x", xycol + ".y"], inplace=True)
972-
for wkbcol in metadata.get("wkb_columns", []):
973-
gdf[wkbcol] = gpd.GeoSeries.from_wkb(gdf[wkbcol], crs=crs)
971+
with warnings.catch_warnings():
972+
# setting a column named 'geometry' triggers a future warning
973+
# with geopandas 0.14
974+
warnings.simplefilter(action="ignore", category=FutureWarning)
975+
976+
for xycol in metadata.get("xy_columns", []):
977+
gdf[xycol] = gpd.points_from_xy(
978+
x=gdf[xycol + ".x"], y=gdf[xycol + ".y"], crs=crs
979+
)
980+
gdf.drop(columns=[xycol + ".x", xycol + ".y"], inplace=True)
981+
for wkbcol in metadata.get("wkb_columns", []):
982+
gdf[wkbcol] = gpd.GeoSeries.from_wkb(gdf[wkbcol], crs=crs)
983+
gdf.set_geometry("geometry", crs=crs, inplace=True)
974984

975985
except TypeError:
976986
with h5py.File(file_name, "r") as data:

climada/hazard/centroids/test/test_centr.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,51 @@ def test_read_write_hdf5(self):
725725
self.assertTrue(centroids_w == centroids_r)
726726
tmpfile.unlink()
727727

728+
def test_read_write_hdf5_with_additional_columns(self):
729+
tmpfile = Path("test_write_hdf5.out.hdf5")
730+
crs = DEF_CRS
731+
centroids_w = Centroids(
732+
lat=VEC_LAT,
733+
lon=VEC_LON,
734+
crs=crs,
735+
region_id=REGION_ID,
736+
on_land=ON_LAND,
737+
)
738+
centroids_w.gdf = (
739+
centroids_w.gdf.join(
740+
gpd.GeoDataFrame(
741+
{"more_points": [shapely.Point(i, i) for i in range(8)]}
742+
).set_geometry("more_points")
743+
)
744+
.join(
745+
gpd.GeoDataFrame(
746+
{
747+
"some_shapes": [
748+
shapely.Point((2, 2)),
749+
shapely.Point((3, 3)),
750+
shapely.Polygon([(0, 0), (1, 1), (1, 0), (0, 0)]),
751+
shapely.LineString([(0, 1), (1, 0)]),
752+
]
753+
* 2
754+
}
755+
).set_geometry("some_shapes")
756+
)
757+
.join(
758+
gpd.GeoDataFrame(
759+
{
760+
"more_shapes": [
761+
shapely.LineString([(0, 1), (1, 2)]),
762+
]
763+
* 8
764+
}
765+
).set_geometry("more_shapes")
766+
)
767+
)
768+
centroids_w.write_hdf5(tmpfile)
769+
centroids_r = Centroids.from_hdf5(tmpfile)
770+
self.assertTrue(centroids_w == centroids_r)
771+
tmpfile.unlink()
772+
728773
def test_from_hdf5_nonexistent_file(self):
729774
"""Test raising FileNotFoundError when creating Centroids object from a nonexistent HDF5 file"""
730775
file_name = "/path/to/nonexistentfile.h5"

0 commit comments

Comments
 (0)