Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Removed:
### Fixed

- `util.lines_polys_handler` solve polygon disaggregation issue in metre-based projection [#666](https://github.com/CLIMADA-project/climada_python/pull/666)
- Problem with `pyproj.CRS` as `Impact` attribute, [#706](https://github.com/CLIMADA-project/climada_python/issues/706). Now CRS is always stored as `str` in WKT format.

### Deprecated

Expand Down
9 changes: 7 additions & 2 deletions climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import xlsxwriter
from tqdm import tqdm
import h5py
from pyproj import CRS as pyprojCRS
from rasterio.crs import CRS as rasterioCRS # pylint: disable=no-name-in-module

from climada.entity import Exposures, Tag
from climada.hazard import Tag as TagHaz
Expand Down Expand Up @@ -72,6 +74,8 @@ class Impact():
ordinal 1 (ordinal format of datetime library)
coord_exp : np.array
exposures coordinates [lat, lon] (in degrees)
crs : str
WKT string of the impact's crs
eai_exp : np.array
expected impact for each exposure within a period of 1/frequency_unit
at_event : np.array
Expand Down Expand Up @@ -126,7 +130,8 @@ def __init__(self,
coord_exp : np.array, optional
exposures coordinates [lat, lon] (in degrees)
crs : Any, optional
coordinate reference system
Coordinate reference system. CRS instances from ``pyproj`` and ``rasterio``
will be transformed into WKT. Other types are not handled explicitly.
eai_exp : np.array, optional
expected impact for each exposure within a period of 1/frequency_unit
at_event : np.array, optional
Expand All @@ -149,7 +154,7 @@ def __init__(self,
self.event_name = [] if event_name is None else event_name
self.date = np.array([], int) if date is None else date
self.coord_exp = np.array([], float) if coord_exp is None else coord_exp
self.crs = crs
self.crs = crs.to_wkt() if isinstance(crs, (pyprojCRS, rasterioCRS)) else crs
self.eai_exp = np.array([], float) if eai_exp is None else eai_exp
self.at_event = np.array([], float) if at_event is None else at_event
self.frequency = np.array([],float) if frequency is None else frequency
Expand Down
15 changes: 14 additions & 1 deletion climada/engine/test/test_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import numpy.testing as npt
from scipy import sparse
import h5py
from pyproj import CRS
from rasterio.crs import CRS as rCRS

from climada.entity.tag import Tag
from climada.hazard.tag import Tag as TagHaz
Expand Down Expand Up @@ -98,6 +100,17 @@ def test_from_eih_pass(self):
np.stack([exp.gdf.latitude.values, exp.gdf.longitude.values], axis=1)
)

def test_pyproj_crs(self):
"""Check if initializing with a pyproj.CRS transforms it into a string"""
crs = CRS.from_epsg(4326)
impact = Impact(crs=crs)
self.assertEqual(impact.crs, crs.to_wkt())

def test_rasterio_crs(self):
"""Check if initializing with a rasterio.crs.CRS transforms it into a string"""
crs = rCRS.from_epsg(4326)
impact = Impact(crs=crs)
self.assertEqual(impact.crs, crs.to_wkt())

class TestImpactConcat(unittest.TestCase):
"""test Impact.concat"""
Expand Down Expand Up @@ -926,7 +939,7 @@ def _compare_file_to_imp(self, filepath, impact, dense_imp_mat):
npt.assert_array_equal(file["event_name"].asstr(), impact.event_name)
npt.assert_array_equal(file["date"], impact.date)
npt.assert_array_equal(file["coord_exp"], impact.coord_exp)
self.assertEqual(file.attrs["crs"], DEF_CRS)
self.assertEqual(file.attrs["crs"], impact.crs)
npt.assert_array_equal(file["eai_exp"], impact.eai_exp)
npt.assert_array_equal(file["at_event"], impact.at_event)
npt.assert_array_equal(file["frequency"], impact.frequency)
Expand Down