diff --git a/climada/hazard/centroids/centr.py b/climada/hazard/centroids/centr.py index 2cf0878d4b..b0da5af249 100644 --- a/climada/hazard/centroids/centr.py +++ b/climada/hazard/centroids/centr.py @@ -193,8 +193,7 @@ def to_default_crs(self, inplace=True): self.to_crs(DEF_CRS, inplace=inplace) def to_crs(self, crs, inplace=False): - """ Project the current centroids to the default CRS (epsg4326) - Modifies the object in place. + """ Project the current centroids to the desired crs Parameters ---------- @@ -211,7 +210,8 @@ def to_crs(self, crs, inplace=False): Centroids in the new crs """ if inplace: - self.gdf.to_crs(crs) + self.gdf.to_crs(crs, inplace=True) + return None return Centroids.from_geodataframe(self.gdf.to_crs(crs)) @classmethod @@ -263,14 +263,13 @@ def from_exposures(cls, exposures): col_names = [ column for column in exposures.gdf.columns - if column in ['region_id', 'on_land'] + if column in DEF_COLS ] # Legacy behaviour # Exposures can be without geometry column #TODO: remove once exposures is real geodataframe with geometry. if 'geometry' in exposures.gdf.columns: - col_names += ['geometry'] gdf = exposures.gdf[col_names] return cls.from_geodataframe(gdf) diff --git a/climada/hazard/centroids/test/test_centr.py b/climada/hazard/centroids/test/test_centr.py index 798b281e39..f1494cf382 100644 --- a/climada/hazard/centroids/test/test_centr.py +++ b/climada/hazard/centroids/test/test_centr.py @@ -136,8 +136,8 @@ def test_from_csv(self): self.assertEqual(centroids.crs, 'epsg:3857') # Delete file - Path(tmpfile).unlink() - + Path(tmpfile).unlink() + def test_write_read_csv(self): """Write and read a Centroids CSV file correctly.""" # Create Centroids with latitude and longitude arrays @@ -158,8 +158,8 @@ def test_write_read_csv(self): self.assertEqual(centroids_in.crs, centroids_out.crs) #delete file - Path(tmpfile).unlink() - + Path(tmpfile).unlink() + def test_from_excel_def_crs(self): """Read a centroid excel file correctly and use default CRS.""" # Create temporary excel file containing centroids data @@ -168,10 +168,10 @@ def test_from_excel_def_crs(self): lon = np.array([0, 0, 0, 180, -180]) df = pd.DataFrame({'lat':lat, 'lon':lon}) df.to_excel(tmpfile, sheet_name = 'centroids', index=False) - + # Read centroids using from_excel method centroids = Centroids.from_excel(file_path=tmpfile) - + # test attributes np.testing.assert_array_equal(centroids.lat, lat) np.testing.assert_array_equal(centroids.lon, lon) @@ -179,7 +179,7 @@ def test_from_excel_def_crs(self): #delete file Path(tmpfile).unlink() - + def test_from_excel(self): """Read a centroid excel file correctly which contains CRS information.""" # Create temporary excel file containing centroids data @@ -189,10 +189,10 @@ def test_from_excel(self): df = pd.DataFrame({'lat':lat, 'lon':lon}) df['crs'] = CRS.from_user_input(3857).to_wkt() df.to_excel(tmpfile, sheet_name = 'centroids', index=False) - + # Read centroids using from_excel method centroids = Centroids.from_excel(file_path=tmpfile) - + # test attributes np.testing.assert_array_equal(centroids.lat, lat) np.testing.assert_array_equal(centroids.lon, lon) @@ -221,7 +221,7 @@ def test_write_read_excel(self): self.assertEqual(centroids_in.crs, centroids_out.crs) #delete file - Path(tmpfile).unlink() + Path(tmpfile).unlink() def test_from_geodataframe(self): """Test that constructing a valid Centroids instance from gdf works.""" @@ -229,20 +229,22 @@ def test_from_geodataframe(self): lat = np.arange(170, 180) lon = np.arange(-50, -40) region_id = np.arange(1, 11) + on_land = np.ones(10).astype(bool) extra = np.repeat(str('a'), 10) gdf = gpd.GeoDataFrame({ 'geometry' : gpd.points_from_xy(lon, lat), 'region_id' : region_id, + 'on_land': on_land, 'extra' : extra }, crs=crs) centroids = Centroids.from_geodataframe(gdf) - for name, array in zip(['lat', 'lon', 'region_id'], - [lat, lon, region_id]): + for name, array in zip(['lat', 'lon', 'region_id', 'on_land'], + [lat, lon, region_id, on_land]): np.testing.assert_array_equal(array, getattr(centroids, name)) - np.testing.assert_array_equal(extra, centroids.gdf.extra.values) + self.assertTrue('extra' not in centroids.gdf.columns) self.assertTrue(u_coord.equal_crs(centroids.crs, crs)) def test_from_geodataframe_invalid(self):