Skip to content

Commit df3ec13

Browse files
exposures: appropriate error message in latitude/longitude
1 parent f198a6a commit df3ec13

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

climada/entity/exposures/base.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,38 @@ 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+
) from valerr
148+
raise
136149

137150
@property
138151
def longitude(self):
139152
"""Longitude array of exposures"""
140-
return self.data.geometry.x.values
153+
try:
154+
return self.data.geometry.x.values
155+
except ValueError as valerr:
156+
nonpoints = list(
157+
self.data[
158+
self.data.geometry.type != "Point"
159+
].geometry.type.drop_duplicates()
160+
)
161+
if nonpoints:
162+
raise ValueError(
163+
"Can only calculate longitude from Points."
164+
f" GeoDataFrame contains {', '.join(nonpoints)}"
165+
) from valerr
166+
raise
141167

142168
@property
143169
def geometry(self):

climada/entity/exposures/test/test_base.py

Lines changed: 30 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,35 @@ 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. GeoDataFrame contains Polygon, MulitPolygon",
675+
str(valer.exception),
676+
)
677+
with self.assertRaises(ValueError) as valer:
678+
exp.longitude
679+
self.assertEqual(
680+
"Can only calculate latitude from Points. GeoDataFrame contains Polygon, MulitPolygon",
681+
str(valer.exception),
682+
)
683+
655684

656685
class TestImpactFunctions(unittest.TestCase):
657686
"""Test impact function handling"""

0 commit comments

Comments
 (0)