|
| 1 | +import numpy as np |
| 2 | +import pymap3d as pm |
| 3 | + |
| 4 | +def geodetic2nvector(lat, lon, ell=None, deg=True): |
| 5 | + """ |
| 6 | + Convert geodetic coordinates (latitude, longitude) to an n-vector. |
| 7 | +
|
| 8 | + Parameters: |
| 9 | + lat : float or array-like |
| 10 | + Geodetic latitude(s). |
| 11 | + lon : float or array-like |
| 12 | + Geodetic longitude(s). |
| 13 | + ell : str or tuple, optional |
| 14 | + Reference ellipsoid (default is None, which uses WGS84). |
| 15 | + deg : bool, optional |
| 16 | + If True (default), inputs are in degrees. If False, use radians. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + n1, n2, n3 : ndarray |
| 20 | + Components of the n-vector in the Earth-Centered Earth-Fixed (ECEF) coordinate system. |
| 21 | + """ |
| 22 | + lat, lon = np.atleast_1d(lat), np.atleast_1d(lon) |
| 23 | + |
| 24 | + if deg: |
| 25 | + lat, lon = np.radians(lat), np.radians(lon) |
| 26 | + |
| 27 | + sin_lat, cos_lat = np.sin(lat), np.cos(lat) |
| 28 | + sin_lon, cos_lon = np.sin(lon), np.cos(lon) |
| 29 | + |
| 30 | + n1 = cos_lat * cos_lon |
| 31 | + n2 = cos_lat * sin_lon |
| 32 | + n3 = sin_lat |
| 33 | + |
| 34 | + return n1, n2, n3 |
| 35 | + |
| 36 | +def nvector2geodetic(n1, n2, n3, ell=None, deg=True): |
| 37 | + """ |
| 38 | + Convert an n-vector back to geodetic coordinates (latitude, longitude). |
| 39 | +
|
| 40 | + Parameters: |
| 41 | + n1, n2, n3 : float or array-like |
| 42 | + Components of the n-vector in the Earth-Centered Earth-Fixed (ECEF) coordinate system. |
| 43 | + ell : str or tuple, optional |
| 44 | + Reference ellipsoid (default is None, which uses WGS84). |
| 45 | + deg : bool, optional |
| 46 | + If True (default), returns latitude and longitude in degrees. If False, in radians. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + lat, lon : ndarray |
| 50 | + Geodetic latitude(s) and longitude(s). |
| 51 | + """ |
| 52 | + n1, n2, n3 = np.atleast_1d(n1), np.atleast_1d(n2), np.atleast_1d(n3) |
| 53 | + |
| 54 | + # Compute latitude and longitude from n-vector |
| 55 | + lat = np.arcsin(n3) |
| 56 | + lon = np.arctan2(n2, n1) |
| 57 | + |
| 58 | + if deg: |
| 59 | + lat, lon = np.degrees(lat), np.degrees(lon) |
| 60 | + |
| 61 | + return lat, lon |
| 62 | + |
| 63 | +def ecef2nvector(x, y, z, ell=None, deg=True): |
| 64 | + """ |
| 65 | + Convert ECEF coordinates to an n-vector. |
| 66 | +
|
| 67 | + Parameters: |
| 68 | + x, y, z : float or array-like |
| 69 | + ECEF coordinates in meters. |
| 70 | + ell : str or tuple, optional |
| 71 | + Reference ellipsoid (default is None, which uses WGS84). |
| 72 | + deg : bool, optional |
| 73 | + If True (default), geodetic2nvector() inputs are in degrees. If False, in radians. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + n1, n2, n3 : ndarray |
| 77 | + Components of the n-vector in the Earth-Centered Earth-Fixed (ECEF) coordinate system. |
| 78 | + """ |
| 79 | + lat, lon, _ = pm.ecef2geodetic(x, y, z, ell=ell, deg=deg) |
| 80 | + return geodetic2nvector(lat, lon, ell=ell, deg=deg) |
| 81 | + |
| 82 | +def nvector2ecef(n1, n2, n3, alt=0, ell=None, deg=True): |
| 83 | + """ |
| 84 | + Convert an n-vector to ECEF coordinates. |
| 85 | +
|
| 86 | + Parameters: |
| 87 | + n1, n2, n3 : float or array-like |
| 88 | + Components of the n-vector in the Earth-Centered Earth-Fixed (ECEF) coordinate system. |
| 89 | + alt : float or array-like, optional |
| 90 | + Altitude in meters (default is 0). |
| 91 | + ell : str or tuple, optional |
| 92 | + Reference ellipsoid (default is None, which uses WGS84). |
| 93 | + deg : bool, optional |
| 94 | + If True (default), nvector2geodetic() outputs are in degrees. If False, in radians. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + x, y, z : ndarray |
| 98 | + ECEF coordinates in meters. |
| 99 | + """ |
| 100 | + lat, lon = nvector2geodetic(n1, n2, n3, ell=ell, deg=deg) |
| 101 | + return pm.geodetic2ecef(lat, lon, alt, ell=ell, deg=deg) |
0 commit comments