Skip to content

Commit 17fa00e

Browse files
Merge pull request #1018 from CLIMADA-project/hotfix_bounding_box_from_countries
hotfix: bug in bounding_box_from_countries
2 parents 35274ac + 4de8101 commit 17fa00e

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

climada/util/coordinates.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import shapely.vectorized
4747
import shapely.wkt
4848
from cartopy.io import shapereader
49-
from shapely.geometry import MultiPolygon, Point, Polygon, box
49+
from shapely.geometry import MultiPolygon, Point, box
5050
from sklearn.neighbors import BallTree
5151

5252
import climada.util.hdf5_handler as u_hdf5
@@ -1723,19 +1723,20 @@ def bounding_box_from_countries(country_names, buffer=1.0):
17231723
"""
17241724

17251725
country_geometry = get_country_geometries(country_names).geometry
1726-
longitudes, latitudes = [], []
1727-
for multipolygon in country_geometry:
1728-
if isinstance(multipolygon, Polygon): # if entry is polygon
1729-
for coord in polygon.exterior.coords: # Extract exterior coordinates
1730-
longitudes.append(coord[0])
1731-
latitudes.append(coord[1])
1732-
else: # if entry is multipolygon
1733-
for polygon in multipolygon.geoms:
1734-
for coord in polygon.exterior.coords: # Extract exterior coordinates
1735-
longitudes.append(coord[0])
1736-
latitudes.append(coord[1])
1726+
generator_country_geometry = (
1727+
poly if isinstance(poly, MultiPolygon) else MultiPolygon([poly])
1728+
for poly in country_geometry
1729+
)
1730+
lon, lat = np.concatenate(
1731+
[
1732+
np.array(pg.exterior.coords).T
1733+
for poly in generator_country_geometry
1734+
for pg in poly.geoms
1735+
],
1736+
axis=1,
1737+
)
17371738

1738-
return latlon_bounds(np.array(latitudes), np.array(longitudes), buffer=buffer)
1739+
return latlon_bounds(lat, lon, buffer=buffer)
17391740

17401741

17411742
def bounding_box_from_cardinal_bounds(*, northern, eastern, western, southern):
@@ -1756,12 +1757,13 @@ def bounding_box_from_cardinal_bounds(*, northern, eastern, western, southern):
17561757
Returns
17571758
-------
17581759
tuple
1759-
The resulting normalized bounding box (min_lon, min_lat, max_lon, max_lat) with -180 <= min_lon < max_lon < 540
1760+
The resulting normalized bounding box (min_lon, min_lat, max_lon, max_lat)
1761+
with -180 <= min_lon < max_lon < 540
17601762
17611763
"""
17621764

17631765
# latitude bounds check
1764-
if not ((90 >= northern > southern >= -90)):
1766+
if not 90 >= northern > southern >= -90:
17651767
raise ValueError(
17661768
"Given northern bound is below given southern bound or out of bounds"
17671769
)

climada/util/test/test_coordinates.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,7 @@ def test_bounding_box_global(self):
23052305

23062306
def test_bounding_box_from_countries(self):
23072307
"""Test for a list of ISO country codes."""
2308+
# Italy is a multipolygon geometry
23082309
result = u_coord.bounding_box_from_countries(
23092310
["ITA"], buffer=1.0
23102311
) # Testing with Italy (ITA)
@@ -2314,7 +2315,21 @@ def test_bounding_box_from_countries(self):
23142315
34.48924388200004,
23152316
19.517425977000073,
23162317
48.08521494500006,
2317-
] # Italy's bounding box
2318+
] # Italy's bounding box with 1 degree buffer
2319+
np.testing.assert_array_almost_equal(result, expected)
2320+
2321+
# Switzerland is a polygon geometry
2322+
result = u_coord.bounding_box_from_countries(
2323+
["CHE"], buffer=0.0
2324+
) # Testing with Switzerland (CHE)
2325+
# Real expected bounds for Switzerland (calculated or manually known)
2326+
expected = [
2327+
5.954809204000128,
2328+
45.82071848599999,
2329+
10.466626831000013,
2330+
47.801166077000076,
2331+
] # CHE's bounding box with 0 degree buffer
2332+
np.testing.assert_array_almost_equal(result, expected)
23182333

23192334
# invalid input
23202335
with self.assertRaises(ValueError):

0 commit comments

Comments
 (0)