Skip to content

Commit a9f9e6a

Browse files
Allow Exposures.from_raster to set Exposure metadata (#1091)
* Allow metadata values to be set when reading Exposures from raster * Fix linting * Fix linting * Update CHANGELOG.md * Don't restrict Exposures.from_raster attrs parameter to metadata Previously we only let the attrs parameter set Exposures metadata attributes. We remove this restriction so that it can specify any parameter in the Exposures.__init__ method. --------- Co-authored-by: emanuel-schmid <[email protected]>
1 parent dc85d0f commit a9f9e6a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Removed:
3030
- Added instructions to install Climada petals on Euler cluster in `doc.guide.Guide_Euler.ipynb` [#1029](https://github.com/CLIMADA-project/climada_python/pull/1029)
3131
- Added util methods to handle crs coordinates consistently: `is_geo_coords`, `check_if_geo_coords`, `get_crs_unit`, `estimate_matching_threshold`, `degree_to_km`, and `km_to_degree` [#1080](https://github.com/CLIMADA-project/climada_python/pull/1080)
3232
- `ImpactFunc` and `ImpactFuncSet` now support equality comparisons via `==` [#1027](https://github.com/CLIMADA-project/climada_python/pull/1027)
33+
- Added optional `attrs` parameter to `Exposures.from_raster` method to set additional object properties through the method's `Exposures.__init__` call.
3334

3435
### Changed
3536

climada/entity/exposures/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ def from_raster(
723723
width=None,
724724
height=None,
725725
resampling=Resampling.nearest,
726+
attrs=None,
726727
):
727728
"""Read raster data and set latitude, longitude, value and meta
728729
@@ -750,11 +751,15 @@ def from_raster(
750751
resampling : rasterio.warp,.Resampling optional
751752
resampling
752753
function used for reprojection to dst_crs
754+
attrs : dict
755+
dictionary of kwargs passed to the resulting Exposures.__init__
753756
754757
returns
755758
--------
756759
Exposures
757760
"""
761+
attrs = attrs or {}
762+
758763
meta, value = u_coord.read_raster(
759764
file_name,
760765
[band],
@@ -781,6 +786,7 @@ def from_raster(
781786
},
782787
meta=meta,
783788
crs=meta["crs"],
789+
**attrs,
784790
)
785791

786792
def plot_scatter(

climada/entity/exposures/test/test_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ def test__init__mda_in_kwargs(self):
181181

182182
def test_read_raster_pass(self):
183183
"""from_raster"""
184-
exp = Exposures.from_raster(HAZ_DEMO_FL, window=Window(10, 20, 50, 60))
184+
exp = Exposures.from_raster(
185+
HAZ_DEMO_FL, window=Window(10, 20, 50, 60), attrs={"value_unit": "USD"}
186+
)
185187
exp.check()
186188
self.assertTrue(u_coord.equal_crs(exp.crs, DEF_CRS))
187189
self.assertAlmostEqual(
@@ -202,6 +204,7 @@ def test_read_raster_pass(self):
202204
self.assertAlmostEqual(
203205
exp.gdf["value"].values.reshape((60, 50))[25, 12], 0.056825936
204206
)
207+
self.assertEqual(exp.value_unit, "USD")
205208

206209
def test_assign_raster_pass(self):
207210
"""Test assign_centroids with raster hazard"""

0 commit comments

Comments
 (0)