Skip to content

Commit

Permalink
exposures: appropriate error message in latitude/longitude properties (
Browse files Browse the repository at this point in the history
…#982)

* exposures: appropriate error message in latitude/longitude

* Update test_base.py

fix typo

* typo

* add hint to error message

Co-authored-by: Chahan M. Kropf <[email protected]>

* add hint to error message

Co-authored-by: Chahan M. Kropf <[email protected]>

* adjust test string and fix whitespace

---------

Co-authored-by: Chahan M. Kropf <[email protected]>
  • Loading branch information
emanuel-schmid and chahank authored Dec 13, 2024
1 parent 595b31f commit 39b4898
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
32 changes: 30 additions & 2 deletions climada/entity/exposures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,40 @@ def gdf(self):
@property
def latitude(self):
"""Latitude array of exposures"""
return self.data.geometry.y.values
try:
return self.data.geometry.y.values
except ValueError as valerr:
nonpoints = list(
self.data[
self.data.geometry.type != "Point"
].geometry.type.drop_duplicates()
)
if nonpoints:
raise ValueError(
"Can only calculate latitude from Points."
f" GeoDataFrame contains {', '.join(nonpoints)}."
" Please see the lines_polygons module tutorial."
) from valerr
raise

Check warning on line 149 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered line

Line 149 is not covered by tests

@property
def longitude(self):
"""Longitude array of exposures"""
return self.data.geometry.x.values
try:
return self.data.geometry.x.values
except ValueError as valerr:
nonpoints = list(
self.data[
self.data.geometry.type != "Point"
].geometry.type.drop_duplicates()
)
if nonpoints:
raise ValueError(
"Can only calculate longitude from Points."
f" GeoDataFrame contains {', '.join(nonpoints)}."
" Please see the lines_polygons module tutorial."
) from valerr
raise

Check warning on line 168 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered line

Line 168 is not covered by tests

@property
def geometry(self):
Expand Down
35 changes: 34 additions & 1 deletion climada/entity/exposures/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import rasterio
import scipy as sp
from rasterio.windows import Window
from shapely.geometry import Point
from shapely.geometry import MultiPolygon, Point, Polygon
from sklearn.metrics import DistanceMetric

import climada.util.coordinates as u_coord
Expand Down Expand Up @@ -652,6 +652,39 @@ def test_to_crs_epsg_crs(self):
Exposures.to_crs(self, crs="GCS", epsg=26915)
self.assertEqual("one of crs or epsg must be None", str(cm.exception))

def test_latlon_with_polygons(self):
"""Check for proper error message if the data frame contains non-Point shapes"""
poly = Polygon(
[(10.0, 0.0), (10.0, 1.0), (11.0, 1.0), (11.0, 0.0), (10.0, 0.0)]
)
point = Point((1, -1))
multi = MultiPolygon(
[
(
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
[((0.1, 1.1), (0.1, 1.2), (0.2, 1.2), (0.2, 1.1))],
)
]
)
poly = Polygon()
exp = Exposures(geometry=[poly, point, multi, poly])
with self.assertRaises(ValueError) as valer:
exp.latitude
self.assertEqual(
"Can only calculate latitude from Points."
" GeoDataFrame contains Polygon, MultiPolygon."
" Please see the lines_polygons module tutorial.",
str(valer.exception),
)
with self.assertRaises(ValueError) as valer:
exp.longitude
self.assertEqual(
"Can only calculate longitude from Points."
" GeoDataFrame contains Polygon, MultiPolygon."
" Please see the lines_polygons module tutorial.",
str(valer.exception),
)


class TestImpactFunctions(unittest.TestCase):
"""Test impact function handling"""
Expand Down

0 comments on commit 39b4898

Please sign in to comment.