Skip to content

Commit

Permalink
Update to crs with inplace argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahan Kropf committed Jan 8, 2024
1 parent 26e2702 commit e6fa996
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
9 changes: 4 additions & 5 deletions climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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
Expand Down Expand Up @@ -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.

Check warning on line 271 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO: remove once exposures is real geodataframe with geometry.
Raw output
no description found
if 'geometry' in exposures.gdf.columns:
col_names += ['geometry']
gdf = exposures.gdf[col_names]
return cls.from_geodataframe(gdf)

Expand Down
28 changes: 15 additions & 13 deletions climada/hazard/centroids/test/test_centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -168,18 +168,18 @@ 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)
self.assertEqual(centroids.crs, DEF_CRS)

#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
Expand All @@ -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)
Expand Down Expand Up @@ -221,28 +221,30 @@ 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."""
crs = DEF_CRS
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):
Expand Down

0 comments on commit e6fa996

Please sign in to comment.