Skip to content

Commit 5801311

Browse files
added three bounds functions
1 parent 2f972ea commit 5801311

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

climada/util/coordinates.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,104 @@ def get_country_code(lat, lon, gridded=False):
16291629
return region_id
16301630

16311631

1632+
def boundsNESW_from_global():
1633+
"""
1634+
Return global NESW bounds in EPSG 4326
1635+
1636+
Returns
1637+
-------
1638+
list:
1639+
The calculated bounding box as [north, east, south, west] in EPSG 4326
1640+
"""
1641+
return [90, 180, -90, -180]
1642+
1643+
1644+
def boundsNESW_from_country_codes(country_codes, rel_margin=0.2):
1645+
"""
1646+
Return NESW bounds in EPSG 4326 for the combined area defined by given country ISO codes.
1647+
1648+
Parameters
1649+
----------
1650+
country_codes : list
1651+
A list of ISO country codes (e.g.,['ITA'], ['ITA', 'CHE']).
1652+
rel_margin : float
1653+
A relative margin to extend the bounding box in all directions. Default is 0.2.
1654+
1655+
Returns
1656+
-------
1657+
list:
1658+
The calculated bounding box as [north, east, south, west] in EPSG 4326
1659+
"""
1660+
[north, east, south, west] = [-90, -180, 90, 180]
1661+
1662+
# loop through ISO codes
1663+
for iso in country_codes:
1664+
geo = get_country_geometries(iso).to_crs(epsg=4326)
1665+
iso_west, iso_south, iso_east, iso_north = geo.total_bounds
1666+
if np.any(np.isnan([iso_west, iso_south, iso_east, iso_north])):
1667+
LOGGER.warning(
1668+
f"ISO code '{iso}' not recognized. This region will not be included."
1669+
)
1670+
continue
1671+
1672+
north = max(north, iso_north)
1673+
east = max(east, iso_east)
1674+
south = min(south, iso_south)
1675+
west = min(west, iso_west)
1676+
1677+
# no countries recognized
1678+
if [north, east, south, west] == [-90, -180, 90, 180]:
1679+
raise Exception("No ISO code was recognized.")
1680+
1681+
# add relative margin
1682+
lat_margin = rel_margin * (north - south)
1683+
lon_margin = rel_margin * (east - west)
1684+
north = min(north + lat_margin, 90)
1685+
east = min(east + lon_margin, 180)
1686+
south = max(south - lat_margin, -90)
1687+
west = max(west - lon_margin, -180)
1688+
1689+
return [north, east, south, west]
1690+
1691+
1692+
def boundsNESW_from_NESW(*, north, east, south, west, rel_margin=0.0):
1693+
"""
1694+
Return NESW bounds in EPSG 4326 with relative margin from given NESW values in EPSG 4326.
1695+
1696+
Parameters
1697+
----------
1698+
north : (float, int)
1699+
Maximal latitude in EPSG 4326.
1700+
east : (float, int)
1701+
Maximal longitute in EPSG 4326.
1702+
south : (float, int)
1703+
Minimal latitude in EPSG 4326.
1704+
west : (float, int)
1705+
Minimal longitude in EPSG 4326.
1706+
rel_margin : float
1707+
A relative margin to extend the bounding box in all directions. Default is 0.2.
1708+
1709+
Returns
1710+
-------
1711+
list:
1712+
The calculated bounding box as [north, east, south, west] in EPSG 4326
1713+
"""
1714+
1715+
# simple bounds check
1716+
if not ((90 >= north > south >= -90) and (180 >= east > west >= -180)):
1717+
raise ValueError("Given bounds are not in standard order or standard bounds")
1718+
1719+
# add relative margin
1720+
lat_margin = rel_margin * (north - south)
1721+
lon_margin = rel_margin * (east - west)
1722+
north = min(north + lat_margin, 90)
1723+
east = min(east + lon_margin, 180)
1724+
south = max(south - lat_margin, -90)
1725+
west = max(west - lon_margin, -180)
1726+
1727+
return [north, east, south, west]
1728+
1729+
16321730
def get_admin1_info(country_names):
16331731
"""Provide Natural Earth registry info and shape files for admin1 regions
16341732

0 commit comments

Comments
 (0)