Skip to content

Commit f198a6a

Browse files
revert last two commints (coordinates, test_coordinates)
1 parent 5801311 commit f198a6a

File tree

2 files changed

+0
-155
lines changed

2 files changed

+0
-155
lines changed

climada/util/coordinates.py

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,104 +1629,6 @@ 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-
17301632
def get_admin1_info(country_names):
17311633
"""Provide Natural Earth registry info and shape files for admin1 regions
17321634

climada/util/test/test_coordinates.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,62 +2294,6 @@ def test_mask_raster_with_geometry(self):
22942294
)
22952295

22962296

2297-
class TestBoundsFromUserInput(unittest.TestCase):
2298-
"""Unit tests for the bounds_from_user_input function."""
2299-
2300-
def test_boundsNESW_from_global(self):
2301-
"""Test for 'global' area selection."""
2302-
result = u_coord.boundsNESW_from_global()
2303-
expected = [90, 180, -90, -180]
2304-
np.testing.assert_almost_equal(result, expected)
2305-
2306-
def test_boundsNESW_from_country_codes(self):
2307-
"""Test for a list of ISO country codes."""
2308-
result = u_coord.boundsNESW_from_country_codes(
2309-
["ITA"], rel_margin=0.2
2310-
) # Testing with Italy (ITA)
2311-
# Real expected bounds for Italy (calculated or manually known)
2312-
expected = [
2313-
49.404409157600064,
2314-
20.900365510000075,
2315-
33.170049669400036,
2316-
4.219788779000066,
2317-
] # Italy's bounding box
2318-
2319-
np.testing.assert_array_almost_equal(result, expected, decimal=4)
2320-
2321-
def test_bounding_box(self):
2322-
"""Test for bounding box input with margin applied."""
2323-
[north, east, south, west] = [50, -100, 30, -120]
2324-
result = u_coord.boundsNESW_from_NESW(
2325-
north=north, south=south, west=west, east=east, rel_margin=0.1
2326-
)
2327-
expected = [
2328-
50 + 2,
2329-
-100 + 2,
2330-
30 - 2,
2331-
-120 - 2,
2332-
] # Apply margin calculation
2333-
np.testing.assert_array_almost_equal(result, expected)
2334-
2335-
def test_invalid_input_string(self):
2336-
"""Test for invalid string input."""
2337-
with self.assertRaises(Exception):
2338-
u_coord.boundsNESW_from_country_codes("DEU")
2339-
2340-
def test_empty_input(self):
2341-
"""Test for empty input."""
2342-
with self.assertRaises(Exception):
2343-
u_coord.boundsNESW_from_country_codes([])
2344-
2345-
def test_invalid_coordinate_input(self):
2346-
"""Test for str in coordinates input input."""
2347-
with self.assertRaises(ValueError):
2348-
u_coord.boundsNESW_from_NESW(north=40, south=50, east=30, west=10)
2349-
with self.assertRaises(TypeError):
2350-
u_coord.boundsNESW_from_NESW(north=40, south="20", east=30, west=10)
2351-
2352-
23532297
# Execute Tests
23542298
if __name__ == "__main__":
23552299
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestFunc)
@@ -2358,5 +2302,4 @@ def test_invalid_coordinate_input(self):
23582302
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRasterMeta))
23592303
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestRasterIO))
23602304
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestDistance))
2361-
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestBoundsFromUserInput))
23622305
unittest.TextTestRunner(verbosity=2).run(TESTS)

0 commit comments

Comments
 (0)