@@ -1123,7 +1123,7 @@ def estimate_matching_threshold(coords_to_assign):
11231123
11241124
11251125def degree_to_km (degree ):
1126- """
1126+ r """
11271127 Convert an angle from degrees to kilometers.
11281128
11291129 This function converts a given angle in degrees to its equivalent distance in
@@ -1146,8 +1146,9 @@ def degree_to_km(degree):
11461146
11471147 Notes
11481148 -----
1149- The conversion is based on the formula: $distance = angle_{radians} \\ times R$,
1150- where $R$ is the Earth's radius.
1149+ The conversion is based on the formula:
1150+ .. math:: $distance = angle_{radians} \\times R$
1151+ where R is the Earth's radius in km.
11511152
11521153 Examples
11531154 --------
@@ -1158,7 +1159,7 @@ def degree_to_km(degree):
11581159
11591160
11601161def km_to_degree (km ):
1161- """
1162+ r """
11621163 Convert a distance from kilometers to degrees.
11631164
11641165 This function converts a given distance in kilometers on the Earth's surface
@@ -1181,8 +1182,9 @@ def km_to_degree(km):
11811182
11821183 Notes
11831184 -----
1184- The conversion is based on the formula: $angle_{radians} = distance / R$,
1185- where $R$ is the Earth's radius.
1185+ The conversion is based on the formula:
1186+ .. math::$angle_{radians} = distance / R$
1187+ where R is the Earth's radius in km.
11861188
11871189 Examples
11881190 --------
@@ -1225,10 +1227,12 @@ def match_coordinates(
12251227 "approx". Default: "euclidean"
12261228 crs : str or pyproj crs, optinal
12271229 Coordinate reference system (crs) of the input coordinates.
1228- Default: EPSG:4326
1230+ Default: EPSG:4326 (degrees lat/lon)
12291231 threshold : float, optional
1232+ Assignement threshold in units of the provided crs.
12301233 If the distance to the nearest neighbor exceeds `threshold`, the index `-1` is assigned.
1231- Set `threshold` to 0 to disable nearest neighbor matching. Default: 100 (km or crs.unit)
1234+ Set `threshold` to 0 to disable nearest neighbor matching.
1235+ Default: twice the highest resolution of the coords_to_assign
12321236 kwargs: dict, optional
12331237 Keyword arguments to be passed on to nearest-neighbor finding functions in case of
12341238 non-exact matching with the specified `distance`.
@@ -1251,13 +1255,21 @@ def match_coordinates(
12511255 The nearest neighbor matching works with lat/lon coordinates (in degrees), and projected coordinates
12521256 (e.g. meters, kilometers). However, projected coordinates, require "euclidean" distance to be used
12531257 for nearest neighbor matching. Furthermore, the handling of antimeridian crossing is disabled
1254- (check_antimeridian=False) and a warning is raised when the user attempts to use this option with
1255- a non-degree coordinates system.
1258+ (check_antimeridian=False).
12561259
12571260 Make sure that all coordinates are according to the same coordinate reference system. In case
12581261 of lat/lon coordinates, the "haversine" distance is able to correctly compute the distance
1259- across the antimeridian. However, when exact matches are enforced with `threshold=0`, lat/lon
1262+ across the antimeridian. When exact matches are enforced with `threshold=0`, lat/lon
12601263 coordinates need to be given in the same longitudinal range (such as (-180, 180)).
1264+
1265+ See Also
1266+ --------
1267+ climada.util.coordinates.estimate_matching_threshold:
1268+ method to calculate the default threshold
1269+ climada.util.coordinates.km_to_degree:
1270+ method to approximately convert kilometers to degrees
1271+ climada.util.coordinates.degree_to_km:
1272+ method to approximately convert degrees to kilometers
12611273 """
12621274 crs = PCRS .from_user_input (crs )
12631275
@@ -1335,8 +1347,14 @@ def match_centroids(
13351347 threshold = None ,
13361348):
13371349 """Assign to each gdf coordinate point its closest centroids's coordinate.
1338- If distances > threshold in points' distances, -1 is returned.
1339- If centroids are in a raster and coordinate point is outside of it ``-1`` is assigned
1350+ If there is no exact match for some entry, an attempt is made to assign the geographically
1351+ nearest neighbor. If the distance to the nearest neighbor exceeds `threshold`, the index `-1`
1352+ is assigned.
1353+
1354+ Make sure that all coordinates are according to the same coordinate reference system (crs).
1355+
1356+ You can disable nearest neighbor matching by setting `threshold` to 0, in which case
1357+ only exactly matching coordinates are assigned to each other.
13401358
13411359 Parameters
13421360 ----------
@@ -1352,41 +1370,42 @@ def match_centroids(
13521370 for geographic crs differs from the true distance.
13531371 Default: "euclidean"
13541372 threshold : float, optional
1355- If the distance (in the unit of the crs otherwise) to the nearest
1356- neighbor exceeds `threshold`, the index `-1` is assigned.
1357- Set `threshold` to 0, to enforce exact coordinate matching only.
1358- Default: 2 times the minimal grid spacing assuming centroids to ge
1359- on a grid
1373+ Assignement threshold in units of the provided crs.
1374+ If the distance to the nearest neighbor exceeds `threshold`, the index `-1` is assigned.
1375+ Set `threshold` to 0 to disable nearest neighbor matching.
1376+ Default: twice the highest resolution of the coords_to_assign
13601377
13611378 See Also
13621379 --------
1363- climada.util.coordinates.match_grid_points: method to associate centroids to
1364- coordinate points when centroids is a raster
1365- climada.util.coordinates.match_coordinates: method to associate centroids to
1366- coordinate points
1380+ climada.util.coordinates.match_coordinates: method to match coordinates
13671381 climada.util.coordinates.estimate_matching_threshold: method to estimate the
13681382 default threshold
1383+ climada.util.coordinates.km_to_degree:
1384+ method to approximately convert kilometers to degrees
1385+ climada.util.coordinates.degree_to_km:
1386+ method to approximately convert degrees to kilometers
1387+ climada.util.coordinates.match_grid_points: method to associate centroids to
1388+ coordinate points when centroids is a raster
13691389
13701390 Notes
13711391 -----
1372- The default order of use is:
1373-
1374- 1. if centroid raster is defined, assign the closest raster point
1375- to each gdf coordinate point.
1376- 2. if no raster, assign centroids to the nearest neighbor using
1377- euclidian metric
1378-
1379- Both cases can introduce innacuracies for coordinates in lat/lon
1380- coordinates as distances in degrees differ from distances in meters
1381- on the Earth surface, in particular for higher latitude and distances
1382- larger than 100km. If more accuracy is needed, please use 'haversine'
1383- distance metric. This however is slower for (quasi-)gridded data,
1384- and works only for non-gridded data.
1385-
1386- Projected coordinates (defined in units other than degrees) are supported.
1387- However, nearest neighbor matching for projected coordinates is only possible
1388- using 'euclidean' distance, and handling of coordinates crossing the antimeridian
1389- is not implemented.
1392+ For coordinates in lat/lon coordinates distances in degrees differ from
1393+ distances in meters on the Earth surface, in particular for higher
1394+ latitude and distances larger than 100km. If more accuracy for degree
1395+ coordinates is needed, please use 'haversine' distance metric,
1396+ which however is slower.
1397+
1398+ Caution: nearest neighbourg matching can introduce serious artefacts
1399+ such as:
1400+ - coordinates centroids with shifted grids can lead
1401+ to systematically wrong assignements.
1402+ - centroids covering larger areas than coordinates may lead
1403+ to sub-optimal matching if the threshold is too large
1404+ - projected crs often diverge at the anti-meridian and close points
1405+ on either side will be at a large distance. For proper handling
1406+ of the anti-meridian please use degree coordinates in EPSG:4326.
1407+ This might be relevant for countries like the Fidji or the US that
1408+ cross the anti-meridian.
13901409 """
13911410
13921411 try :
0 commit comments