Skip to content

Commit

Permalink
added ValueError if ISO code not recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Dec 13, 2024
1 parent 679c2ec commit 8ad36cb
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions climada/util/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,21 +784,17 @@ def get_country_geometries(
if isinstance(country_names, str):
country_names = [country_names]

# print warning if ISO code not recognized
# raise error if a country name is not recognized
for country_name in country_names:
if not country_name in nat_earth[["ISO_A3", "WB_A3", "ADM0_A3"]].values:
LOGGER.warning(f"ISO code {country_name} not recognized.")
raise ValueError(f"ISO code {country_name} not recognized.")

Check warning on line 790 in climada/util/coordinates.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered line

Line 790 is not covered by tests

country_mask = np.isin(
nat_earth[["ISO_A3", "WB_A3", "ADM0_A3"]].values,
country_names,
).any(axis=1)
out = out[country_mask]

# exit with Value error if no country code was recognized
if out.size == 0:
raise ValueError(f"None of the given country codes were recognized.")

if extent:
if extent[1] - extent[0] > 360:
raise ValueError(
Expand Down Expand Up @@ -1729,10 +1725,15 @@ def get_country_bounding_box(country_names, buffer=1.0):
country_geometry = get_country_geometries(country_names).geometry
longitudes, latitudes = [], []
for multipolygon in country_geometry:
for polygon in multipolygon.geoms: # Loop through each polygon
if isinstance(multipolygon, Polygon): # if entry is polygon
for coord in polygon.exterior.coords: # Extract exterior coordinates
longitudes.append(coord[0])
latitudes.append(coord[1])

Check warning on line 1731 in climada/util/coordinates.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered lines

Lines 1729-1731 are not covered by tests
else: # if entry is multipolygon
for polygon in multipolygon.geoms:
for coord in polygon.exterior.coords: # Extract exterior coordinates
longitudes.append(coord[0])
latitudes.append(coord[1])

return latlon_bounds(np.array(latitudes), np.array(longitudes), buffer=buffer)

Expand Down

0 comments on commit 8ad36cb

Please sign in to comment.