Skip to content

Commit 39b4898

Browse files
exposures: appropriate error message in latitude/longitude properties (#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]>
1 parent 595b31f commit 39b4898

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

climada/entity/exposures/base.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,40 @@ def gdf(self):
132132
@property
133133
def latitude(self):
134134
"""Latitude array of exposures"""
135-
return self.data.geometry.y.values
135+
try:
136+
return self.data.geometry.y.values
137+
except ValueError as valerr:
138+
nonpoints = list(
139+
self.data[
140+
self.data.geometry.type != "Point"
141+
].geometry.type.drop_duplicates()
142+
)
143+
if nonpoints:
144+
raise ValueError(
145+
"Can only calculate latitude from Points."
146+
f" GeoDataFrame contains {', '.join(nonpoints)}."
147+
" Please see the lines_polygons module tutorial."
148+
) from valerr
149+
raise
136150

137151
@property
138152
def longitude(self):
139153
"""Longitude array of exposures"""
140-
return self.data.geometry.x.values
154+
try:
155+
return self.data.geometry.x.values
156+
except ValueError as valerr:
157+
nonpoints = list(
158+
self.data[
159+
self.data.geometry.type != "Point"
160+
].geometry.type.drop_duplicates()
161+
)
162+
if nonpoints:
163+
raise ValueError(
164+
"Can only calculate longitude from Points."
165+
f" GeoDataFrame contains {', '.join(nonpoints)}."
166+
" Please see the lines_polygons module tutorial."
167+
) from valerr
168+
raise
141169

142170
@property
143171
def geometry(self):

climada/entity/exposures/test/test_base.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import rasterio
2828
import scipy as sp
2929
from rasterio.windows import Window
30-
from shapely.geometry import Point
30+
from shapely.geometry import MultiPolygon, Point, Polygon
3131
from sklearn.metrics import DistanceMetric
3232

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

655+
def test_latlon_with_polygons(self):
656+
"""Check for proper error message if the data frame contains non-Point shapes"""
657+
poly = Polygon(
658+
[(10.0, 0.0), (10.0, 1.0), (11.0, 1.0), (11.0, 0.0), (10.0, 0.0)]
659+
)
660+
point = Point((1, -1))
661+
multi = MultiPolygon(
662+
[
663+
(
664+
((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)),
665+
[((0.1, 1.1), (0.1, 1.2), (0.2, 1.2), (0.2, 1.1))],
666+
)
667+
]
668+
)
669+
poly = Polygon()
670+
exp = Exposures(geometry=[poly, point, multi, poly])
671+
with self.assertRaises(ValueError) as valer:
672+
exp.latitude
673+
self.assertEqual(
674+
"Can only calculate latitude from Points."
675+
" GeoDataFrame contains Polygon, MultiPolygon."
676+
" Please see the lines_polygons module tutorial.",
677+
str(valer.exception),
678+
)
679+
with self.assertRaises(ValueError) as valer:
680+
exp.longitude
681+
self.assertEqual(
682+
"Can only calculate longitude from Points."
683+
" GeoDataFrame contains Polygon, MultiPolygon."
684+
" Please see the lines_polygons module tutorial.",
685+
str(valer.exception),
686+
)
687+
655688

656689
class TestImpactFunctions(unittest.TestCase):
657690
"""Test impact function handling"""

0 commit comments

Comments
 (0)