Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
657b612
Add function to test if lat and lon are in geographic coords
luseverin Sep 22, 2024
b594b7f
Add test for geographic coords in dist_to_coast
luseverin Sep 22, 2024
e36ded8
Add test for geographic coords in coord_on_land
luseverin Sep 22, 2024
dc78a13
Merge branch 'develop-white' into feature/remove_implicit_use_crs
luseverin Oct 23, 2024
b88ecc4
Switch branch feature/remove_implicite_use_crs to black format
luseverin Oct 23, 2024
96ede79
Merge branch 'develop-black' into feature/remove_implicit_use_crs
luseverin Oct 23, 2024
2bc5c6a
Merge branch 'develop' into feature/remove_implicit_use_crs
luseverin Oct 23, 2024
b139e8f
Merge branch 'develop' into feature/remove_implicit_use_crs
luseverin Jan 14, 2025
c8aac80
Merge branch 'develop' into feature/remove_implicit_use_crs
luseverin Mar 5, 2025
320d0f7
Extend allowed extent for geo coords
luseverin Mar 5, 2025
9ef7323
Modify allowed extents in geo_coords check function to allow for bounds
luseverin Mar 6, 2025
f27c4aa
Include possible wrong lat lon extension in error message
luseverin Mar 6, 2025
df439a5
Add check for geo coords in get_country_code
luseverin Mar 6, 2025
d3aea1e
Add test for geocoords in convert_wgs_to_utm
luseverin Mar 6, 2025
fb3cf7e
Update error message in coord_on_land
luseverin Mar 6, 2025
cf57963
Harmonize error messages
luseverin Mar 6, 2025
cb1e718
Adapt test get_country_geometries_fail
luseverin Mar 6, 2025
2a1f9b1
Check for geo coords in match_coordinates
luseverin Mar 6, 2025
322a9d3
Add check for lat in get_gridcellarea
luseverin Mar 6, 2025
60dc067
Modify check geo coords function to please linter
luseverin Mar 6, 2025
a0e02be
Add test for geo coords in calc of coriolis param
luseverin Mar 6, 2025
127d9cc
Add tests for check_if_geo_coords function
luseverin Mar 6, 2025
737fcd4
Merge branch 'develop' into feature/remove_implicit_use_crs
luseverin Apr 8, 2025
eca9f77
Incorporate suggestions from review 1
luseverin Apr 8, 2025
99635bf
Start adapating match_coordinates function to other crs
luseverin May 15, 2025
a642c04
Add handling of unit in match_centroids
luseverin May 15, 2025
eea31cb
Rename deg to degrees
luseverin May 15, 2025
35f0f7f
Add function to infer unit of coordinates
luseverin May 15, 2025
782b53d
Use infer_unit_coords in match_centroids function
luseverin May 15, 2025
ffee7f8
Add tests for unit_infer and unit-adapted match_coordinates
luseverin May 15, 2025
61e7b0f
Make test_tack_land_params compliant with new requirement of geodetic…
luseverin May 15, 2025
ef250c4
Correct wrong is_geodetic crs attribute
luseverin May 15, 2025
bca6e3a
Add restriction for haversine with non-degree units
luseverin May 15, 2025
54e2f75
Finish adding test for handling of different units
luseverin May 15, 2025
63bf8b5
Add test invalid unit for match coords and docstrings
luseverin May 15, 2025
da1c965
Update docstrings
luseverin May 15, 2025
5d53512
Try to fix new linter issues
luseverin May 15, 2025
54c8565
Correct error in logger warning
luseverin May 15, 2025
e1c9633
Force check_antimeridian to False in distance euclidean if unit is no…
luseverin Jun 19, 2025
fbcb163
Remove outdated test for raise of ValueError when check_antimeridian …
luseverin Jun 19, 2025
41b66bb
Add function to get coordinates from axis of coordinates'crs
luseverin Jun 27, 2025
7d1d3f3
Update unit names to be consistent with epsg notation and remove unit…
luseverin Jun 27, 2025
2176402
Clarify docstring check_if_geo_coords
luseverin Jun 27, 2025
8edb184
Make conversion between radians and degrees more consistent
luseverin Jun 27, 2025
91600a9
Remove deprecated infer_unit_coords function
luseverin Jun 27, 2025
f0d8dcc
Update tests
luseverin Jun 27, 2025
d45e55f
Force input coords and centroids to have crs attribute in match_centr…
luseverin Jun 27, 2025
409ff73
Re-harmonize unit names and raise error when crs of centroids or coor…
luseverin Jun 27, 2025
028fa0b
Add missing tests
luseverin Jun 27, 2025
2da2e2e
Fix linter issues
luseverin Jun 27, 2025
e2022ba
Refix linter
luseverin Jun 27, 2025
9c4b51e
Wrap valuerror raising in check_if_geo_coords
luseverin Jul 28, 2025
f14d5f0
Update tests
luseverin Jul 28, 2025
a733e8e
Remove useless info in get_crs_unit docstring
luseverin Jul 28, 2025
685c241
Raise separate exceptions for missing crs in coord_gdf and centroids
luseverin Jul 28, 2025
a5d5c20
Fix error in _nearest_neighbor_approx docstring
luseverin Jul 28, 2025
16bcc84
Make clearer error message for _nearest_neighbor with non-allowed units
luseverin Jul 30, 2025
07a4a73
Simplify crs check in match_centroids
luseverin Jul 30, 2025
f2d49b3
Correct typo and update tests for new error messages in match_centroids
luseverin Jul 30, 2025
0f3de31
Only warn for check_antimeridian when it is true and add tests
luseverin Jul 30, 2025
c67d6da
Allow for units other than degree km and m by keeping distance thresh…
luseverin Jul 30, 2025
a260848
Update thresholds in test to be consisten with handling of thresholds…
luseverin Jul 30, 2025
2f6bb5e
Remove mention to calling functions in error message in _nearest_neig…
luseverin Jul 30, 2025
734541a
Remove mention to calling functions in error message in _nearest_neig…
luseverin Jul 30, 2025
86d8126
Update docstrings match_centroids
luseverin Jul 30, 2025
faf8666
update docstrings for match_coordinates
luseverin Jul 30, 2025
b55b02a
Update changelog
luseverin Jul 30, 2025
45175c2
Remove confusing comments in code
luseverin Aug 5, 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
61 changes: 37 additions & 24 deletions climada/util/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,13 +1174,15 @@ def match_coordinates(

check_if_geo_coords(coords[:, 0], coords[:, 1])
check_if_geo_coords(coords_to_assign[:, 0], coords_to_assign[:, 1])
elif unit == "m":
coords *= 1e-3
coords_to_assign *= 1e-3
elif unit == "km":
pass
else:
raise ValueError("Unit must be one of 'degree', 'm' or 'km'.")
elif unit != "km":
LOGGER.warning(
"You are using coordinates systems defined in %s. "
"The following distance threshold will be used for coordinates "
"matching: %i %s. Please adapt the distance threshold if needed. ",
unit,
threshold,
unit,
)

not_assigned_idx_mask = assigned_idx == -1
assigned_idx[not_assigned_idx_mask] = nearest_neighbor_funcs[distance](
Expand Down Expand Up @@ -1245,21 +1247,17 @@ def match_centroids(

try:
if not equal_crs(coord_gdf.crs, centroids.crs):
raise ValueError("Set hazard and GeoDataFrame to same CRS first!")
if coord_gdf.crs is None:
raise ValueError(
"Please provide a coord_gdf (coordinate GeoDataFrame)"
"object with a valid crs attribute."
"Please provide coord_gdf and centroids defined in a common CRS."
)
if centroids.crs is None :
if coord_gdf.crs is None or centroids.crs is None:
raise ValueError(
"Please provide centroids "
"object with a valid crs attribute."
"Please provide coord_gdf and centroids with valid crs attributes."
)
Comment thread
luseverin marked this conversation as resolved.
except AttributeError as exc:
# a crs attribute is needed for unit inference
raise ValueError(
"Please provide coordinate GeoDataFrame and Hazard object with a valid crs attribute."
"Please provide coord_gdf and centroids with valid crs attributes."
) from exc

# get unit of coordinate systems from axis of crs
Expand Down Expand Up @@ -1288,8 +1286,8 @@ def _nearest_neighbor_approx(
centroids, coordinates, unit, threshold, check_antimeridian=True
):
"""Compute the nearest centroid for each coordinate using the
euclidean distance d = ((dlon)cos(lat))^2+(dlat)^2. For distant points
(e.g. more than 100km apart) use the haversine distance.
squared equirectangular approximation distance d = ((dlon)cos(lat))^2+(dlat)^2.
For distant points (e.g. more than 100km apart) use the haversine distance.

Parameters
----------
Expand Down Expand Up @@ -1318,8 +1316,10 @@ def _nearest_neighbor_approx(
# first check that unit is in degree
if unit != "degree":
raise ValueError(
"Only degree unit is supported for nearest neighbor approximation."
"Please use euclidean distance for non-degree units."
"Only degree unit is supported for nearest neighbor matching using"
"'approx' distance. Please use euclidean distance by setting "
"'distance=euclidean' in the match_coordinates or match_centroids "
"function when using units that are not degrees."
Comment thread
luseverin marked this conversation as resolved.
Outdated
)
# Compute only for the unique coordinates. Copy the results for the
# not unique coordinates
Expand Down Expand Up @@ -1386,8 +1386,10 @@ def _nearest_neighbor_haversine(centroids, coordinates, unit, threshold):
# first check that unit is in degree
if unit != "degree":
raise ValueError(
"Only degree unit is supported for nearest neighbor approximation."
"Please use euclidean distance for non-degree units."
"Only degree unit is supported for nearest neighbor matching using"
"'haversine' distance. Please use euclidean distance by setting "
"'distance=euclidean' in the match_coordinates or match_centroids "
"function when using units that are not degrees."
Comment thread
luseverin marked this conversation as resolved.
Outdated
)
# Construct tree from centroids
tree = BallTree(np.radians(centroids), metric="haversine")
Expand Down Expand Up @@ -1471,17 +1473,28 @@ def _nearest_neighbor_euclidean(
centroids = np.rad2deg(centroids)
coordinates = np.rad2deg(coordinates)
dist = dist * EARTH_RADIUS_KM
threshold_unit = "km"
else:
# if unit is not in degree, check_antimeridian is forced to False
check_antimeridian = False
threshold_unit = (
unit # the unit of the threshold is considered to be in input unit
)
if check_antimeridian:
# if unit is not in degree, check_antimeridian is forced to False
check_antimeridian = False
LOGGER.warning(
"Handling of antimeridian crossing is not implemented for non-degree"
" coordinates ('check_antimeridian' has been set to False). Please use"
" degree-based coordinates system if you want to enable antimeridian crossing."
)

# Raise a warning if the minimum distance is greater than the
# threshold and set an unvalid index -1
num_warn = np.sum(dist > threshold)
if num_warn:
LOGGER.warning(
"Distance to closest centroid is greater than %i km for %i coordinates.",
"Distance to closest centroid is greater than %i %s for %i coordinates.",
threshold,
threshold_unit,
num_warn,
)
assigned[dist > threshold] = -1
Expand Down
41 changes: 31 additions & 10 deletions climada/util/test/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,8 @@ def test_match_centroids(self):
with self.assertRaises(ValueError) as cm:
u_coord.match_centroids(gdf, centroids)
self.assertIn(
"Set hazard and GeoDataFrame to same CRS first!", str(cm.exception)
"Please provide coord_gdf and centroids defined in a common CRS.",
str(cm.exception),
)

# Test 4: no crs
Expand All @@ -971,8 +972,7 @@ def test_match_centroids(self):
with self.assertRaises(ValueError) as cm:
u_coord.match_centroids(gdf, centroids)
self.assertIn(
"Please provide coordinate GeoDataFrame and "
"Hazard object with a valid crs attribute.",
"Please provide coord_gdf and centroids with valid crs attributes.",
str(cm.exception),
)

Expand Down Expand Up @@ -1256,7 +1256,7 @@ def repeat_coord_pass(self, dist):
# Check copied coordinates have same neighbors
self.assertEqual(neighbors[2], neighbors[0])

def antimeridian_warning(self, dist):
def distance_threshold_warning_antimeridian(self, dist):
"""Check that a warning is raised when minimum distance greater than threshold"""
# Load input
centroids, exposures = self.data_antimeridian_values()
Expand Down Expand Up @@ -1285,7 +1285,7 @@ def test_approx_repeat_coord_pass(self):

def test_approx_antimeridian_warning(self):
"""Call normal_warning test for approximate distance"""
self.antimeridian_warning("approx")
self.distance_threshold_warning_antimeridian("approx")

def test_haver_normal_pass(self):
"""Call normal_pass test for haversine distance"""
Expand All @@ -1301,7 +1301,7 @@ def test_haver_repeat_coord_pass(self):

def test_haver_antimeridian_warning(self):
"""Call normal_warning test for haversine distance"""
self.antimeridian_warning("haversine")
self.distance_threshold_warning_antimeridian("haversine")

def test_euc_normal_pass(self):
"""Call normal_pass test for euclidean distance"""
Expand All @@ -1317,7 +1317,7 @@ def test_euc_repeat_coord_pass(self):

def test_euc_antimeridian_warning(self):
"""Call normal_warning test for euclidean distance"""
self.antimeridian_warning("euclidean")
self.distance_threshold_warning_antimeridian("euclidean")

def test_diff_outcomes(self):
"""Different NN interpolation outcomes"""
Expand Down Expand Up @@ -1360,15 +1360,15 @@ def setUp_match_coordinates(self):
# test with different unit
self.expected_results = {
"degree": [
# test with different thresholds (in degree)
# test with different thresholds (in degree (converted to km))
(100, [2, 1, 2, 0, 3, -1, 4]),
(20, [-1, 1, 2, 0, 3, -1, -1]),
(0, [-1, 1, 2, 0, -1, -1, -1]),
],
"m": [
# test with different thresholds (in m)
(100, [2, 1, 2, 0, 3, -1, -1]),
(20, [-1, 1, 2, 0, 3, -1, -1]),
(100e3, [2, 1, 2, 0, 3, -1, -1]),
(20e3, [-1, 1, 2, 0, 3, -1, -1]),
(0, [-1, 1, 2, 0, -1, -1, -1]),
],
"km": [
Expand Down Expand Up @@ -1502,6 +1502,27 @@ def test_nearest_neighbor_haversine_invalid_unit(self):
u_coord.NEAREST_NEIGHBOR_THRESHOLD,
)

def antimeridian_warning_invalid_unit(self):
"""Check that a warning is raised when coords are
non-degree and check_antimeridian is True"""

self.setUp_match_coordinates()
coords_to_assign = np.deg2rad(self.coords_to_assign) * EARTH_RADIUS_KM # to km
coords = np.deg2rad(self.coords) * EARTH_RADIUS_KM

with self.assertLogs("climada.util.coordinates", level="WARNING") as cm:
neighbors = u_coord.match_coordinates(
coords_to_assign, coords, check_antimeridian=True
)
self.assertIn(
"Handling of antimeridian crossing is not implemented for non-degree"
" coordinates ('check_antimeridian' has been set to False). Please use"
" degree-based coordinates system if you want to enable antimeridian crossing.",
cm.output[1],
)

np.testing.assert_array_equal(neighbors, self.data_ref_antimeridian())


class TestGetGeodata(unittest.TestCase):
def test_nat_earth_resolution_pass(self):
Expand Down
Loading