API reference
earthcarekit.geo.distance
Algorithms to calculate geospatial distances.
Notes
This module depends on other internal modules:
geodesic
geodesic(
a: ArrayLike,
b: ArrayLike,
units: str = "km",
tolerance: float = 1e-12,
max_iterations: int = 10,
) -> float64 | NDArray[float64]
Calculates the geodesic distances between points on Earth (i.e. WSG 84 ellipsoid) using Vincenty's inverse method.
Supports single or sequences of coordiates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Coordinates [lat, lon] or array of shape (N, 2), in decimal degrees. |
required |
b
|
ArrayLike
|
Second coordinates, same format/shape as |
required |
units
|
str
|
Output units, "km" (default) or "m". |
'km'
|
tolerance
|
float
|
Convergence threshold in radians. Default is 1e-12. |
1e-12
|
max_iterations
|
int
|
Maximum iterations before failure. Default is 10. |
10
|
Returns:
| Type | Description |
|---|---|
float64 | NDArray[float64]
|
float or np.ndarray: The geodesic distance or distances between the point in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are incompatible or units are invalid. |
Note
Uses WGS84 (a=6378137.0 m, f=1/298.257223563). May fail for nearly antipodal points.
Examples:
>>> geodesic([51.352757, 12.43392], [38.559, 68.856])
4548.675334434374
>>> geodesic([0, 0], [[0, 0], [10, 0], [20, 0]])
array([ 0. , 1105.85483324, 2212.36625417])
>>> geodesic([[0, 0], [10, 0], [20, 0]], [[0, 0], [10, 0], [20, 0]])
array([0., 0., 0.])
References
- Vincenty, T. (1975). Direct and Inverse Solutions of Geodesics on the Ellipsoid with application of nested equations. Survey Review, 23(176), 88-93. https://doi.org/10.1179/sre.1975.23.176.88
Source code in earthcarekit/geo/distance/_vincenty.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
haversine
haversine(
a: ArrayLike,
b: ArrayLike,
units: Literal["m", "km"] = "km",
radius: float = MEAN_EARTH_RADIUS_METERS,
)
Calculates the great-circle (spherical) distance between pairs of latitude/longitude coordinates using the haversine formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
An array-like object of shape (..., 2) containing latitude and longitude coordinates in degrees. The last dimension must be 2: (lat, lon). |
required |
b
|
ArrayLike
|
An array-like object of the same shape as |
required |
units
|
Literal['m', 'km']
|
Unit of the output distance. Must be either "km" for kilometers or "m" for meters. Defaults to "km". |
'km'
|
radius
|
float
|
Radius of the sphere to use for distance calculation.
Defaults to MEAN_EARTH_RADIUS_METERS (based on WSG 84 ellipsoid: ~6371008.77 meters).
Note: If |
MEAN_EARTH_RADIUS_METERS
|
Returns:
| Type | Description |
|---|---|
|
np.ndarray: Array of great-circle distances between |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the shapes of |
Examples:
>>> haversine([51.352757, 12.43392], [38.559, 68.856])
4537.564747442274
>>> haversine([0, 0], [[0, 0], [10, 0], [20, 0]])
array([ 0. , 1111.95079735, 2223.90159469])
>>> haversine([[0, 0], [10, 0], [20, 0]], [[0, 0], [10, 0], [20, 0]])
array([0., 0., 0.])
Source code in earthcarekit/geo/distance/_haversine.py
vincenty
vincenty(
a: ArrayLike,
b: ArrayLike,
units: str = "km",
tolerance: float = 1e-12,
max_iterations: int = 10,
) -> float64 | NDArray[float64]
Calculates the geodesic distances between points on Earth (i.e. WSG 84 ellipsoid) using Vincenty's inverse method.
Supports single or sequences of coordiates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Coordinates [lat, lon] or array of shape (N, 2), in decimal degrees. |
required |
b
|
ArrayLike
|
Second coordinates, same format/shape as |
required |
units
|
str
|
Output units, "km" (default) or "m". |
'km'
|
tolerance
|
float
|
Convergence threshold in radians. Default is 1e-12. |
1e-12
|
max_iterations
|
int
|
Maximum iterations before failure. Default is 10. |
10
|
Returns:
| Type | Description |
|---|---|
float64 | NDArray[float64]
|
float or np.ndarray: The geodesic distance or distances between the point in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are incompatible or units are invalid. |
Note
Uses WGS84 (a=6378137.0 m, f=1/298.257223563). May fail for nearly antipodal points.
Examples:
>>> geodesic([51.352757, 12.43392], [38.559, 68.856])
4548.675334434374
>>> geodesic([0, 0], [[0, 0], [10, 0], [20, 0]])
array([ 0. , 1105.85483324, 2212.36625417])
>>> geodesic([[0, 0], [10, 0], [20, 0]], [[0, 0], [10, 0], [20, 0]])
array([0., 0., 0.])
References
- Vincenty, T. (1975). Direct and Inverse Solutions of Geodesics on the Ellipsoid with application of nested equations. Survey Review, 23(176), 88-93. https://doi.org/10.1179/sre.1975.23.176.88
Source code in earthcarekit/geo/distance/_vincenty.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |