Skip to content

Remove implicit use of CRS in code base #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
657b612
Add function to test if lat and lon are in geographic coords
Sep 22, 2024
b594b7f
Add test for geographic coords in dist_to_coast
Sep 22, 2024
e36ded8
Add test for geographic coords in coord_on_land
Sep 22, 2024
dc78a13
Merge branch 'develop-white' into feature/remove_implicit_use_crs
Oct 23, 2024
b88ecc4
Switch branch feature/remove_implicite_use_crs to black format
Oct 23, 2024
96ede79
Merge branch 'develop-black' into feature/remove_implicit_use_crs
Oct 23, 2024
2bc5c6a
Merge branch 'develop' into feature/remove_implicit_use_crs
Oct 23, 2024
b139e8f
Merge branch 'develop' into feature/remove_implicit_use_crs
Jan 14, 2025
c8aac80
Merge branch 'develop' into feature/remove_implicit_use_crs
Mar 5, 2025
320d0f7
Extend allowed extent for geo coords
Mar 5, 2025
9ef7323
Modify allowed extents in geo_coords check function to allow for bounds
Mar 6, 2025
f27c4aa
Include possible wrong lat lon extension in error message
Mar 6, 2025
df439a5
Add check for geo coords in get_country_code
Mar 6, 2025
d3aea1e
Add test for geocoords in convert_wgs_to_utm
Mar 6, 2025
fb3cf7e
Update error message in coord_on_land
Mar 6, 2025
cf57963
Harmonize error messages
Mar 6, 2025
cb1e718
Adapt test get_country_geometries_fail
Mar 6, 2025
2a1f9b1
Check for geo coords in match_coordinates
Mar 6, 2025
322a9d3
Add check for lat in get_gridcellarea
Mar 6, 2025
60dc067
Modify check geo coords function to please linter
Mar 6, 2025
a0e02be
Add test for geo coords in calc of coriolis param
Mar 6, 2025
127d9cc
Add tests for check_if_geo_coords function
Mar 6, 2025
737fcd4
Merge branch 'develop' into feature/remove_implicit_use_crs
Apr 8, 2025
eca9f77
Incorporate suggestions from review 1
luseverin Apr 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions climada/hazard/trop_cyclone/trop_cyclone_windfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,15 @@ def _coriolis_parameter(lat: np.ndarray) -> np.ndarray:
cp : np.ndarray of same shape as input
Coriolis parameter.
"""
if not u_coord.check_if_geo_coords(lat, 0):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
return 2 * V_ANG_EARTH * np.sin(np.radians(np.abs(lat)))


Expand Down
101 changes: 97 additions & 4 deletions climada/util/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@
are not considered."""


def check_if_geo_coords(lat, lon):
"""Check if latitude and longitude arrays are likely in geographic coordinates,
testing if min/max values are within -90 to 90 for latitude and -540 to 540
for longitude. Lon coordinates of <-360 or >360 are allowed to cover cases
of objects being defined close to the 180 meridian.

Parameters
----------
lat, lon : ndarrays of floats, same shape
Latitudes and longitudes of points.

Returns
-------
test : bool
True if lat/lon ranges seem to be in the geographic coordinates range, otherwise False.
"""
lat = np.array(lat)
lon = np.array(lon)

# Check if latitude is within -90 to 90 and longitude is within -540 to 540
# and extent are smaller than 180 and 360 respectively
test = (
lat.min() >= -91 and lat.max() <= 91 and lon.min() >= -541 and lon.max() <= 541
) and ((lat.max() - lat.min()) <= 181 and (lon.max() - lon.min()) <= 361)
return bool(test)


def latlon_to_geosph_vector(lat, lon, rad=False, basis=False):
"""Convert lat/lon coodinates to radial vectors (on geosphere)

Expand Down Expand Up @@ -448,13 +475,24 @@ def get_gridcellarea(lat, resolution=0.5, unit="ha"):
unit: string, optional
unit of the output area (default: ha, alternatives: m2, km2)
"""

# first check that lat is in geographic coordinates
if not check_if_geo_coords(lat, 0):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
if unit == "m2":
area = (ONE_LAT_KM * resolution) ** 2 * np.cos(np.deg2rad(lat)) * 1000000
elif unit == "km2":
area = (ONE_LAT_KM * resolution) ** 2 * np.cos(np.deg2rad(lat))
else:
elif unit == "ha":
area = (ONE_LAT_KM * resolution) ** 2 * np.cos(np.deg2rad(lat)) * 100
else:
raise ValueError(f"'{unit}' unit not recognized. Please use any of 'm2', 'km2' or 'ha'.")

return area

Expand Down Expand Up @@ -507,6 +545,15 @@ def convert_wgs_to_utm(lon, lat):
epsg_code : int
EPSG code of UTM projection.
"""
if not check_if_geo_coords(lat, lon):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
epsg_utm_base = 32601 + (0 if lat >= 0 else 100)
return epsg_utm_base + (math.floor((lon + 180) / 6) % 60)

Expand Down Expand Up @@ -564,6 +611,15 @@ def dist_to_coast(coord_lat, lon=None, highres=False, signed=False):
raise ValueError(
f"Mismatching input coordinates size: {lat.size} != {lon.size}"
)
if not check_if_geo_coords(lat, lon):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
return dist_to_coast_nasa(lat, lon, highres=highres, signed=signed)


Expand Down Expand Up @@ -679,6 +735,15 @@ def coord_on_land(lat, lon, land_geom=None):
)
if lat.size == 0:
return np.empty((0,), dtype=bool)
if not check_if_geo_coords(lat, lon):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
delta_deg = 1
lons = lon.copy()
if land_geom is None:
Expand Down Expand Up @@ -796,9 +861,14 @@ def get_country_geometries(
out = out[country_mask]

if extent:
if extent[1] - extent[0] > 360:
if not check_if_geo_coords(extent[2:], extent[:2]):
raise ValueError(
f"longitude extent range is greater than 360: {extent[0]} to {extent[1]}"
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)

if extent[1] < extent[0]:
Expand Down Expand Up @@ -1089,6 +1159,19 @@ def match_coordinates(

# assign remaining coordinates to their geographically nearest neighbor
if threshold > 0 and exact_assign_idx.size != coords_view.size:
# check that coords are geographic before proceeding to nearest neighbor search
if not (
check_if_geo_coords(coords[:, 0], coords[:, 1])
and check_if_geo_coords(coords_to_assign[:, 0], coords_to_assign[:, 1])
):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
not_assigned_idx_mask = assigned_idx == -1
assigned_idx[not_assigned_idx_mask] = nearest_neighbor_funcs[distance](
coords_to_assign, coords[not_assigned_idx_mask], threshold, **kwargs
Expand Down Expand Up @@ -1595,6 +1678,16 @@ def get_country_code(lat, lon, gridded=False):
if lat.size == 0:
return np.empty((0,), dtype=int)
LOGGER.info("Setting region_id %s points.", str(lat.size))
# first check that input lat lon are geographic
if not check_if_geo_coords(lat, lon):
raise ValueError(
"Input lat and lon coordinates do not seem to correspond"
" to geographic coordinates in degrees. This can be because"
" total extents are > 180 for lat or > 360 for lon, lat coordinates"
" are outside of -90<lat<90, or lon coordinates are outside of -540<lon<540."
" If you use degree values outside of these ranges,"
" please shift the coordinates to the valid ranges."
)
if gridded:
base_file = u_hdf5.read(NATEARTH_CENTROIDS[150])
meta, region_id = base_file["meta"], base_file["region_id"]
Expand Down
34 changes: 22 additions & 12 deletions climada/util/test/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ def data_arrays_resampling_demo():
class TestFunc(unittest.TestCase):
"""Test auxiliary functions"""

def test_check_geo_coords(self):
"""Test the check_if_geo_coords function"""
# test correct geographical coords
lat, lon = np.array([0, -2, 5]), np.array([-179, 175, 178])
self.assertEqual(u_coord.check_if_geo_coords(lat, lon), True)
# test wrong lat
lat, lon = np.array([0, 200, 5]), np.array([-179, 175, 178])
self.assertEqual(u_coord.check_if_geo_coords(lat, lon), False)
# test wrong lon
lat, lon = np.array([0, -2, 5]), np.array([-400, 175, 176])
self.assertEqual(u_coord.check_if_geo_coords(lat, lon), False)
lat, lon = np.array([0, -2, 5]), np.array([700, 1000, 800])
self.assertEqual(u_coord.check_if_geo_coords(lat, lon), False)
# test wrong extent
lat, lon = np.array([0, -2, 5]), np.array([-179, 175, 370])
self.assertEqual(u_coord.check_if_geo_coords(lat, lon), False)

def test_lon_normalize(self):
"""Test the longitude normalization function"""
data = np.array([-180, 20.1, -30, 190, -350])
Expand Down Expand Up @@ -1457,18 +1474,11 @@ def test_get_country_geometries_all_pass(self):

def test_get_country_geometries_fail(self):
"""get_country_geometries with offensive parameters"""
with self.assertRaises(ValueError) as cm:
u_coord.get_country_geometries(extent=(-20, 350, 0, 0))
self.assertIn(
"longitude extent range is greater than 360: -20 to 350", str(cm.exception)
)
with self.assertRaises(ValueError) as cm:
u_coord.get_country_geometries(extent=(350, -20, 0, 0))
self.assertIn(
"longitude extent at the left (350) is larger "
"than longitude extent at the right (-20)",
str(cm.exception),
)
self.assertRaises(ValueError,
u_coord.get_country_geometries(extent=(-20, 350, 0, 0)))
self.assertRaises(ValueError,
u_coord.get_country_geometries(extent=(340, -20, 0, 0)))


def test_country_code_pass(self):
"""Test set_region_id"""
Expand Down
Loading