|
| 1 | +from .ellipsoid import Ellipsoid |
| 2 | +from math import atan, radians, degrees, tan |
| 3 | + |
| 4 | +try: |
| 5 | + import numpy |
| 6 | +except ImportError: |
| 7 | + numpy = None |
| 8 | + |
| 9 | + |
| 10 | +def geodetic2geocentric(geodetic_lat: float, ell: Ellipsoid = None, deg: bool = True) -> float: |
| 11 | + if numpy is not None: |
| 12 | + fun = numpy.vectorize(geodetic2geocentric_point) |
| 13 | + return fun(geodetic_lat, ell, deg) |
| 14 | + else: |
| 15 | + return geodetic2geocentric_point(geodetic_lat, ell, deg) |
| 16 | + |
| 17 | + |
| 18 | +def geodetic2geocentric_point(geodetic_lat: float, ell: Ellipsoid = None, deg: bool = True) -> float: |
| 19 | + """ |
| 20 | + convert geodetic latitude to geocentric latitude. |
| 21 | +
|
| 22 | + this is like Matlab geocentricLatitude() |
| 23 | + https://www.mathworks.com/help/map/ref/geocentriclatitude.html |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + geodetic_lat : float |
| 28 | + geodetic latitude |
| 29 | + ell : Ellipsoid, optional |
| 30 | + reference ellipsoid (default WGS84) |
| 31 | + deg : bool, optional |
| 32 | + degrees input/output (False: radians in/out) |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + geocentric_lat : float |
| 37 | + geocentric latiude |
| 38 | +
|
| 39 | + Notes |
| 40 | + ----- |
| 41 | + Equations from J. P. Snyder, "Map Projections - A Working Manual", |
| 42 | + US Geological Survey Professional Paper 1395, US Government Printing |
| 43 | + Office, Washington, DC, 1987, pp. 13-18. |
| 44 | + """ |
| 45 | + |
| 46 | + if ell is None: |
| 47 | + ell = Ellipsoid() |
| 48 | + |
| 49 | + if abs(geodetic_lat) > 90: |
| 50 | + raise ValueError("-90 <= latitude <= 90") |
| 51 | + |
| 52 | + if deg is True: |
| 53 | + geodetic_lat = radians(geodetic_lat) |
| 54 | + |
| 55 | + geocentric_lat = atan((1 - (ell.eccentricity) ** 2) * tan(geodetic_lat)) |
| 56 | + |
| 57 | + if deg is True: |
| 58 | + geocentric_lat = degrees(geocentric_lat) |
| 59 | + |
| 60 | + return geocentric_lat |
| 61 | + |
| 62 | + |
| 63 | +def geocentric2geodetic(geocentric_lat: float, ell: Ellipsoid = None, deg: bool = True) -> float: |
| 64 | + if numpy is not None: |
| 65 | + fun = numpy.vectorize(geocentric2geodetic_point) |
| 66 | + return fun(geocentric_lat, ell, deg) |
| 67 | + else: |
| 68 | + return geocentric2geodetic_point(geocentric_lat, ell, deg) |
| 69 | + |
| 70 | + |
| 71 | +def geocentric2geodetic_point(geocentric_lat: float, ell: Ellipsoid = None, deg: bool = True) -> float: |
| 72 | + """ |
| 73 | + converts from geocentric latitude to geodetic latitude |
| 74 | +
|
| 75 | + like Matlab geodeticLatitudeFromGeocentric |
| 76 | + https://www.mathworks.com/help/map/ref/geodeticlatitudefromgeocentric.html |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + geocentric_lat : float or numpy.ndarray of float |
| 81 | + geocentric latitude |
| 82 | + ell : Ellipsoid, optional |
| 83 | + reference ellipsoid (default WGS84) |
| 84 | + deg : bool, optional |
| 85 | + degrees input/output (False: radians in/out) |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + geodetic_lat : float or numpy.ndarray of float |
| 90 | + geodetic latiude |
| 91 | +
|
| 92 | + Notes |
| 93 | + ----- |
| 94 | + Equations from J. P. Snyder, "Map Projections - A Working Manual", |
| 95 | + US Geological Survey Professional Paper 1395, US Government Printing |
| 96 | + Office, Washington, DC, 1987, pp. 13-18. |
| 97 | + """ |
| 98 | + |
| 99 | + if ell is None: |
| 100 | + ell = Ellipsoid() |
| 101 | + |
| 102 | + if abs(geocentric_lat) > 90: |
| 103 | + raise ValueError("-90 <= latitude <= 90") |
| 104 | + |
| 105 | + if deg is True: |
| 106 | + geocentric_lat = radians(geocentric_lat) |
| 107 | + |
| 108 | + geodetic_lat = atan(tan(geocentric_lat) / (1 - (ell.eccentricity) ** 2)) |
| 109 | + |
| 110 | + if deg is True: |
| 111 | + geodetic_lat = degrees(geodetic_lat) |
| 112 | + |
| 113 | + return geodetic_lat |
0 commit comments