API reference#
earthcarekit.geo
Geospatial utilities for handling coordinates, coordinate transformations, distance calculation, and interpolation.
Notes#
This module depends on other internal modules:
create_spherical_grid
#
create_spherical_grid(
nlat: int,
nlon: int | None = None,
reduced: bool = False,
lat_spacing: Literal["regular", "sinusoidal", "gaussian"] = "regular",
min_lat: float = -90.0,
max_lat: float = 90.0,
min_lon: float = -180.0,
max_lon: float = 180.0,
) -> SphericalGrid
Generate a spherical gird with regular, sinusoidal or gaussian latitudes and uniform or reduced longitudes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nlat
|
int
|
Number of latitude. |
required |
nlon
|
int | None
|
Nuber of longitudes at the equator. If None, set to |
None
|
reduced
|
bool
|
If True, reduces longitudes near poles using |
False
|
lat_spacing
|
regular or sinusoidal or gaussian
|
Method used to place latitudes. Defaults to "regular". |
'regular'
|
Returns:
| Name | Type | Description |
|---|---|---|
SphericalGrid |
SphericalGrid
|
A container storing |
SphericalGrid
|
|
|
SphericalGrid
|
|
|
SphericalGrid
|
|
|
SphericalGrid
|
|
|
SphericalGrid
|
|
Source code in earthcarekit/geo/grid/_create_global_grid.py
ecef_to_geo
#
ecef_to_geo(
x: SupportsFloat,
y: SupportsFloat,
z: SupportsFloat,
target_radius: float = 1.0,
perfect_sphere: bool = True,
semi_major: float = SEMI_MAJOR_AXIS_METERS,
semi_minor: float = SEMI_MINOR_AXIS_METERS,
) -> tuple[float, float, float]
Converts Earth-centered, Earth-fixed (ECEF) coordinates (x, y, z) to geodetic coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
SupportsFloat
|
Cartesian ECEF x-coordinate in meters. |
required |
y
|
SupportsFloat
|
Cartesian ECEF y-coordinate in meters. |
required |
z
|
SupportsFloat
|
Cartesian ECEF z-coordinate in meters. |
required |
target_radius
|
float
|
Target mean radius of the ECEF coordinate system; defaults to 1.0. |
1.0
|
perfect_sphere
|
bool
|
If True, assume a spherical Earth; otherwise, use ellipsoidal (WGS 84). |
True
|
semi_major
|
float
|
Semi-major axis of the ellipsoid in meters; defaults to WGS 84 (6378137). |
SEMI_MAJOR_AXIS_METERS
|
semi_minor
|
float
|
Semi-minor axis of the ellipsoid in meters; defaults to WGS 84 (6356752.314245). |
SEMI_MINOR_AXIS_METERS
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
Geodetic coordinates (latitude, longitude, altitude)
|
Source code in earthcarekit/geo/convertsions.py
geo_to_ecef
#
geo_to_ecef(
lat: SupportsFloat,
lon: SupportsFloat,
alt: SupportsFloat | None = None,
target_radius: float = 1.0,
perfect_sphere: bool = True,
semi_major: float = SEMI_MAJOR_AXIS_METERS,
semi_minor: float = SEMI_MINOR_AXIS_METERS,
) -> tuple[float, float, float]
Converts geodetic coordinates (latitude, longitude, altitude) to Earth-centered, Earth-fixed (ECEF) coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
SupportsFloat
|
Latitude in degrees (positive north, negative south). |
required |
lon
|
SupportsFloat
|
Longitude in degrees (positive east, negative west). |
required |
alt
|
SupportsFloat | None
|
Height above the Earth ellipsoid in meters; None assumes ellipsoid surface. |
None
|
target_radius
|
float
|
Target mean radius of the ECEF coordinate system; defaults to 1.0. |
1.0
|
perfect_sphere
|
bool
|
If True, use a sphere with radius |
True
|
semi_major
|
float
|
Semi-major axis of the ellipsoid in meters; defaults to WGS 84 (6378137). |
SEMI_MAJOR_AXIS_METERS
|
semi_minor
|
float
|
Semi-minor axis of the ellipsoid in meters; defaults to WGS 84 (6356752.314245). |
SEMI_MINOR_AXIS_METERS
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float]
|
ECEF coordinates (x, y, z) in meters
|
Source code in earthcarekit/geo/convertsions.py
geodesic
#
geodesic(
a: ArrayLike,
b: ArrayLike,
units: str = "km",
tolerance: float = 1e-12,
max_iterations: int = 10,
) -> float64 | NDArray[float64]
Calculates geodesic distances on the WGS 84 ellipsoid using Vincenty's inverse method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Coordinates [lat, lon] or (N, 2) array in decimal degrees. |
required |
b
|
ArrayLike
|
Second coordinates, same format/shape as |
required |
units
|
str
|
Output units ("km" or "m"); defaults to "km". |
'km'
|
tolerance
|
float
|
Convergence threshold in radians; defaults to 1e-12. |
1e-12
|
max_iterations
|
int
|
Maximum iterations before failure; defaults to 10. |
10
|
Returns:
| Type | Description |
|---|---|
float64 | NDArray[float64]
|
Geodesic distance(s) in the specified units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are incompatible, units are invalid, or convergence fails. |
Notes
Uses WGS 84 parameters (a=6378137.0 m, f=1/298.257223563). May fail for nearly antipodal points.
Examples:
>>> import earthcarekit as eck
>>> eck.geodesic([51.352757, 12.43392], [38.559, 68.856])
4548.675334434374
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 | |
get_central_coords
#
Calculates the central lat/lon coordinates.
Source code in earthcarekit/geo/coordinates.py
get_central_latitude
#
Calculates the central latitude coordinate.
Source code in earthcarekit/geo/coordinates.py
get_central_longitude
#
Calculates the central longitude coordinate.
Source code in earthcarekit/geo/coordinates.py
get_coord_between
#
Interpolates between two coordinates by fraction f (0 to 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord1
|
ArrayLike
|
The first lat/lon point. |
required |
coord2
|
ArrayLike
|
The second lat/lon point. |
required |
f
|
float
|
A fractional value between 0 and 1. Defaults to 0.5, i.e., the mid point between coord1 and coord2. |
0.5
|
Returns:
| Type | Description |
|---|---|
NDArray
|
A 2-element |
Source code in earthcarekit/geo/interpolate.py
get_coords
#
get_coords(
ds: Dataset,
*,
lat_var: str = TRACK_LAT_VAR,
lon_var: str = TRACK_LON_VAR,
flatten: bool = False
) -> NDArray
Takes a xarray.Dataset and returns the lat/lon coordinates as a numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat_var
|
str
|
Name of the latitude variable. |
TRACK_LAT_VAR
|
lon_var
|
str
|
Name of the longitude variable. |
TRACK_LON_VAR
|
flatten
|
bool
|
If True, the coordinates will be flattened to a 2D array
|
False
|
Returns:
| Type | Description |
|---|---|
NDArray
|
The extracted lat/lon coordinates. |
Source code in earthcarekit/geo/coordinates.py
haversine
#
haversine(
a: ArrayLike,
b: ArrayLike,
units: Literal["m", "km"] = "km",
radius: float = MEAN_EARTH_RADIUS_METERS,
)
Calculates great-circle distances between latitude/longitude pairs using the haversine formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Coordinates as (..., 2) array (lat, lon) in degrees. |
required |
b
|
ArrayLike
|
Corresponding coordinates as (..., 2) array (lat, lon) in degrees. |
required |
units
|
Literal['m', 'km']
|
Output unit ("m" or "km"); defaults to "km". |
'km'
|
radius
|
float
|
Sphere radius in meters; defaults to mean earth radius (~6371008.77 m). |
MEAN_EARTH_RADIUS_METERS
|
Returns:
| Type | Description |
|---|---|
|
Great-circle distances in the specified units; shape matches input excluding last dimension. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes of |
Examples:
>>> import earthcarekit as eck
>>> eck.haversine([51.352757, 12.43392], [38.559, 68.856])
4537.564747442274
Source code in earthcarekit/geo/distance/_haversine.py
interpgeo
#
Interpolates along the geodesic from (lon1, lat1) to (lon2, lat2) by fraction f (0 to 1) and returns interpolated (lon, lat).
Source code in earthcarekit/geo/interpolate.py
sequence_ecef_to_geo
#
sequence_ecef_to_geo(
x: NDArray | list[SupportsFloat],
y: NDArray | list[SupportsFloat],
z: NDArray | list[SupportsFloat],
target_radius: float = 1.0,
perfect_sphere: bool = True,
semi_major: float = SEMI_MAJOR_AXIS_METERS,
semi_minor: float = SEMI_MINOR_AXIS_METERS,
) -> NDArray
Converts sequences of Earth-centered, Earth-fixed (ECEF) coordinates to geodetic coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray | list[SupportsFloat]
|
ECEF x-coordinates in meters. |
required |
y
|
NDArray | list[SupportsFloat]
|
ECEF y-coordinates in meters. |
required |
z
|
NDArray | list[SupportsFloat]
|
ECEF z-coordinates in meters. |
required |
target_radius
|
float
|
Target mean radius of the ECEF coordinate system; defaults to 1.0. |
1.0
|
perfect_sphere
|
bool
|
If True, assume a spherical Earth; otherwise, use ellipsoidal (WGS 84). |
True
|
semi_major
|
float
|
Semi-major axis of the ellipsoid in meters; defaults to WGS 84 (6378137). |
SEMI_MAJOR_AXIS_METERS
|
semi_minor
|
float
|
Semi-minor axis of the ellipsoid in meters; defaults to WGS 84 (6356752.314245). |
SEMI_MINOR_AXIS_METERS
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Geodetic coordinates as a 2D array (N, 3); (latitude, longitude, altitude) for each input point. |
Source code in earthcarekit/geo/convertsions.py
sequence_geo_to_ecef
#
sequence_geo_to_ecef(
lats: NDArray | list[SupportsFloat],
lons: NDArray | list[SupportsFloat],
alts: NDArray | list[SupportsFloat] | SupportsFloat | None = None,
target_radius: float = 1.0,
perfect_sphere: bool = True,
semi_major: float = SEMI_MAJOR_AXIS_METERS,
semi_minor: float = SEMI_MINOR_AXIS_METERS,
) -> NDArray
Converts sequences of geodetic coordinates to Earth-centered, Earth-fixed (ECEF) coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lats
|
NDArray | list[SupportsFloat]
|
Latitude values in degrees (positive north, negative south). |
required |
lons
|
NDArray | list[SupportsFloat]
|
Longitude values in degrees (positive east, negative west). |
required |
alts
|
NDArray | list[SupportsFloat] | SupportsFloat | None
|
Height above the ellipsoid in meters; None assumes ellipsoid surface. |
None
|
target_radius
|
float
|
Target mean radius of the ECEF coordinate system; defaults to 1.0. |
1.0
|
perfect_sphere
|
bool
|
If True, assume a spherical Earth; otherwise, use ellipsoidal (WGS 84). |
True
|
semi_major
|
float
|
Semi-major axis of the ellipsoid in meters; defaults to WGS 84 (6378137). |
SEMI_MAJOR_AXIS_METERS
|
semi_minor
|
float
|
Semi-minor axis of the ellipsoid in meters; defaults to WGS 84 (6356752.314245). |
SEMI_MINOR_AXIS_METERS
|
Returns:
| Type | Description |
|---|---|
NDArray
|
ECEF coordinates as a 2D array (N, 3), where N is the number of input points. |
Source code in earthcarekit/geo/convertsions.py
vincenty
#
vincenty(
a: ArrayLike,
b: ArrayLike,
units: str = "km",
tolerance: float = 1e-12,
max_iterations: int = 10,
) -> float64 | NDArray[float64]
Calculates geodesic distances on the WGS 84 ellipsoid using Vincenty's inverse method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Coordinates [lat, lon] or (N, 2) array in decimal degrees. |
required |
b
|
ArrayLike
|
Second coordinates, same format/shape as |
required |
units
|
str
|
Output units ("km" or "m"); defaults to "km". |
'km'
|
tolerance
|
float
|
Convergence threshold in radians; defaults to 1e-12. |
1e-12
|
max_iterations
|
int
|
Maximum iterations before failure; defaults to 10. |
10
|
Returns:
| Type | Description |
|---|---|
float64 | NDArray[float64]
|
Geodesic distance(s) in the specified units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input shapes are incompatible, units are invalid, or convergence fails. |
Notes
Uses WGS 84 parameters (a=6378137.0 m, f=1/298.257223563). May fail for nearly antipodal points.
Examples:
>>> import earthcarekit as eck
>>> eck.geodesic([51.352757, 12.43392], [38.559, 68.856])
4548.675334434374
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 | |