Skip to content

Commit 3314b0b

Browse files
Merge branch 'develop'
# Conflicts: # climada/_version.py # setup.py
2 parents a0f6d7d + edfce31 commit 3314b0b

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
Release date: YYYY-MM-DD
6+
7+
Code freeze date: YYYY-MM-DD
8+
9+
### Description
10+
11+
### Dependency Changes
12+
13+
### Added
14+
15+
### Changed
16+
17+
### Fixed
18+
19+
- bug in `climada.util.coordinates.bounding_box_from_countries` occurring if the country is a polygon and not a multipolygon
20+
[#1018](https://github.com/CLIMADA-project/climada_python/pull/1018)
21+
22+
### Deprecated
23+
24+
### Removed
25+
326
## 6.0.0
427

528
Release date: 2025-03-03

climada/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "6.0.0"
1+
__version__ = "6.0.1-dev"

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):

requirements/env_climada.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies:
3030
- pyxlsb>=1.0
3131
- rasterio>=1.4
3232
- requests>=2.32
33+
- rtree>=1.3,<1.4 # 1.4.0 doesn't pass the `pip check`
3334
- salib>=1.5
3435
- seaborn>=0.13
3536
- scikit-learn>=1.6

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
setup(
4444
name="climada",
45-
version="6.0.0",
45+
version="6.0.1-dev",
4646
description="CLIMADA in Python",
4747
long_description=long_description,
4848
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)