def crossingParallels(self, other, lat, wrap=False): '''Return the pair of meridians at which a great circle defined by this and an other point crosses the given latitude. @param other: The other point defining great circle (L{LatLon}). @param lat: Latitude at the crossing (degrees). @keyword wrap: Wrap and unroll longitudes (bool). @return: 2-Tuple (lon1, lon2) in (degrees180) or None if the great circle doesn't reach the given I{lat}. ''' self.others(other) a1, b1 = self.to2ab() a2, b2 = other.to2ab() a = radians(lat) db, b2 = unrollPI(b1, b2, wrap=wrap) ca, ca1, ca2, cdb = map1(cos, a, a1, a2, db) sa, sa1, sa2, sdb = map1(sin, a, a1, a2, db) x = sa1 * ca2 * ca * sdb y = sa1 * ca2 * ca * cdb - ca1 * sa2 * ca z = ca1 * ca2 * sa * sdb h = hypot(x, y) if h < EPS or abs(z) > h: return None # great circle doesn't reach latitude m = atan2(-y, x) + b1 # longitude at max latitude d = acos(z / h) # delta longitude to intersections return degrees180(m - d), degrees180(m + d)
def to3llh(self, datum=Datums.WGS84): '''Convert this (geocentric) Cartesian (x/y/z) point to (ellipsoidal) geodetic lat-, longitude and height on the given datum. Uses Bowring’s (1985) formulation for μm precision in concise form: U{'The accuracy of geodetic latitude and height equations' <http://www.researchgate.net/publication/ 233668213_The_Accuracy_of_Geodetic_Latitude_and_Height_Equations>}, B. R. Bowring, Survey Review, Vol 28, 218, Oct 1985. See also Ralph M. Toms U{'An Efficient Algorithm for Geocentric to Geodetic Coordinate Conversion' <http://www.osti.gov/scitech/biblio/110235>}, Sept 1995 and U{'An Improved Algorithm for Geocentric to Geodetic Coordinate Conversion'<http://www.osti.gov/scitech/servlets/purl/231228>}, Apr 1996, from Lawrence Livermore National Laboratory. @keyword datum: Optional datum to use (L{Datum}). @return: 3-Tuple (lat, lon, heigth) in (degrees90, degrees180, meter). ''' E = datum.ellipsoid x, y, z = self.to3xyz() p = hypot(x, y) # distance from minor axis r = hypot(p, z) # polar radius if min(p, r) > EPS: # parametric latitude (Bowring eqn 17, replaced) t = (E.b * z) / (E.a * p) * (1 + E.e22 * E.b / r) c = 1 / hypot1(t) s = t * c # geodetic latitude (Bowring eqn 18) a = atan2(z + E.e22 * E.b * s**3, p - E.e2 * E.a * c**3) b = atan2(y, x) # ... and longitude # height above ellipsoid (Bowring eqn 7) ca, sa = cos(a), sin(a) # r = E.a / E.e2s(sa) # length of normal terminated by minor axis # h = p * ca + z * sa - (E.a * E.a / r) h = fsum_(p * ca, z * sa, -E.a * E.e2s(sa)) a, b = degrees90(a), degrees180(b) # see <http://GIS.StackExchange.com/questions/28446/> elif p > EPS: # latitude arbitrarily zero a, b, h = 0.0, degrees180(atan2(y, x)), p - E.a else: # polar latitude, longitude arbitrarily zero a, b, h = copysign(90.0, z), 0.0, abs(z) - E.b return a, b, h
def to3lld(self, datum=None): '''Convert this L{Lcc} to a geodetic lat- and longitude. @keyword datum: Optional datum to use, otherwise use this I{Lcc}'s conic.datum (I{Datum}). @return: 3-Tuple (lat, lon, datum) in (degrees90, degrees180, datum). @raise TypeError: If I{datum} is not ellipsoidal. ''' c = self.conic if datum: c = c.toDatum(datum) e = self.easting - c._E0 n = c._r0 - self.northing + c._N0 r_ = copysign(hypot(e, n), c._n) t_ = pow(r_ / c._aF, c._n_) x = c._xdef(t_) # XXX c._lon0 while True: p, x = x, c._xdef(t_ * c._pdef(x)) if abs(x - p) < 1e-9: # XXX EPS too small? break y = (atan(e / n) + c._opt3) * c._n_ + c._lon0 return degrees90(x), degrees180(y), c.datum
def to2ll(self, datum=None): '''Convert this WM coordinate to a geodetic lat- and longitude. @keyword datum: Optional datum (I{Datum}). @return: 2-Tuple (lat, lon) in (degrees90, degrees180). @raise TypeError: If I{datum} is not ellipsoidal. @raise ValueError: Invalid I{radius}. ''' r = self.radius x = self._x / r y = 2 * atan(exp(self._y / r)) - PI_2 if datum: E = datum.ellipsoid if not E.isEllipsoidal: raise TypeError('%s not %s: %r' % ('datum', 'ellipsoidal', datum)) # <http://earth-info.nga.mil/GandG/wgs84/web_mercator/ # %28U%29%20NGA_SIG_0011_1.0.0_WEBMERC.pdf> y = y / r if E.e: y -= E.e * atanh(E.e * tanh(y)) y *= E.a x *= E.a / r return degrees90(y), degrees180(x)
def toLatLon(self, LatLon=_LL, datum=None): '''Convert this Lcc to a lat-/longitude instance. @keyword LatLon: LatLon (sub-)class to instantiate. @keyword datum: Datum to use, otherwise use this Lcc's conic.datum (L{Datum}). @return: The position (L{LatLon}). ''' if not issubclass(LatLon, _LL): raise TypeError('%s not %s: %r' % ('LatLon', 'ellipsoidal', LatLon)) c = self.conic if datum: c = c.toDatum(datum) e = self.easting - c._E0 n = c._r0 - self.northing + c._N0 r_ = copysign(hypot(e, n), c._n) t_ = pow(r_ / c._aF, c._n_) x = c._xdef(t_) # XXX c._lon0 while True: p, x = x, c._xdef(t_ * c._pdef(x)) if abs(x - p) < 1e-9: # XXX EPS too small? break y = (atan(e / n) + c._opt3) * c._n_ + c._lon0 return LatLon(degrees90(x), degrees180(y), height=self.height, datum=c.datum)
def intermediateTo(self, other, fraction, height=None, wrap=False): '''Locate the point at given fraction between this and an other point. @param other: The other point (L{LatLon}). @param fraction: Fraction between both points (float, 0.0 = this point, 1.0 = the other point). @keyword height: Optional height, overriding the fractional height (meter). @keyword wrap: Wrap and unroll longitudes (bool). @return: Intermediate point (L{LatLon}). @raise TypeError: The I{other} point is not L{LatLon}. @example: >>> p1 = LatLon(52.205, 0.119) >>> p2 = LatLon(48.857, 2.351) >>> p = p1.intermediateTo(p2, 0.25) # 51.3721°N, 000.7073°E @JSname: I{intermediatePointTo}. ''' self.others(other) a1, b1 = self.to2ab() a2, b2 = other.to2ab() db, b2 = unrollPI(b1, b2, wrap=wrap) r = haversine_(a2, a1, db) if r > EPS: cb1, cb2, ca1, ca2 = map1(cos, b1, b2, a1, a2) sb1, sb2, sa1, sa2, sr = map1(sin, b1, b2, a1, a2, r) A = sin((1 - fraction) * r) / sr B = sin(fraction * r) / sr x = A * ca1 * cb1 + B * ca2 * cb2 y = A * ca1 * sb1 + B * ca2 * sb2 z = A * sa1 + B * sa2 a = atan2(z, hypot(x, y)) b = atan2(y, x) else: # points too close a = favg(a1, a2, f=fraction) b = favg(b1, b2, f=fraction) if height is None: h = self._havg(other, f=fraction) else: h = height return self.classof(degrees90(a), degrees180(b), height=h)
def to2ll(self): '''Convert this vector to (geodetic) lat- and longitude. @return: 2-Tuple (lat, lon) in (degrees90, degrees180). @example: >>> v = Vector3d(0.500, 0.500, 0.707) >>> a, b = v.to2ll() # 45.0, 45.0 ''' a = atan2(self.z, hypot(self.x, self.y)) b = atan2(self.y, self.x) return degrees90(a), degrees180(b)
def rhumbDestination(self, distance, bearing, radius=R_M, height=None): '''Return the destination point having travelled along a rhumb (loxodrome) line from this point the given distance on the given bearing. @param distance: Distance travelled (same units as radius). @param bearing: Bearing from this point (compass degrees). @keyword radius: Optional, mean earth radius (meter). @keyword height: Optional height, overriding the default height (meter or same unit as radius). @return: The destination point (spherical LatLon). @example: >>> p = LatLon(51.127, 1.338) >>> q = p.rhumbDestination(40300, 116.7) # 50.9642°N, 001.8530°E @JSname: I{rhumbDestinationPoint} ''' a1, b1 = self.to2ab() r = float(distance) / float(radius) # angular distance in radians t = radians(bearing) da = r * cos(t) a2 = a1 + da # normalize latitude if past pole if a2 > PI_2: a2 = PI - a2 elif a2 < -PI_2: a2 = -PI - a2 dp = log(tanPI_2_2(a2) / tanPI_2_2(a1)) # E-W course becomes ill-conditioned with 0/0 if abs(dp) > EPS: q = da / dp else: q = cos(a1) if abs(q) > EPS: b2 = b1 + r * sin(t) / q else: b2 = b1 h = self.height if height is None else height return self.classof(degrees90(a2), degrees180(b2), height=h)
def _direct(self, distance, bearing, llr, height=None): '''(INTERNAL) Direct Vincenty method. @raise VincentyError: Vincenty fails to converge for the current L{LatLon.epsilon} and L{LatLon.iterations} limit. ''' E = self.ellipsoid() c1, s1, t1 = _r3(self.lat, E.f) i = radians(bearing) # initial bearing (forward azimuth) ci, si = cos(i), sin(i) s12 = atan2(t1, ci) * 2 sa = c1 * si c2a = 1 - sa**2 if c2a < EPS: c2a = 0 A, B = 1, 0 else: # e22 == (a / b)**2 - 1 A, B = _p2(c2a * E.e22) s = d = distance / (E.b * A) for _ in range(self._iterations): cs, ss, c2sm = cos(s), sin(s), cos(s12 + s) s_, s = s, d + _ds(B, cs, ss, c2sm) if abs(s - s_) < self._epsilon: break else: raise VincentyError('no convergence %r' % (self, )) t = s1 * ss - c1 * cs * ci # final bearing (reverse azimuth +/- 180) r = degrees360(atan2(sa, -t)) if llr: # destination latitude in [-270, 90) a = degrees90( atan2(s1 * cs + c1 * ss * ci, (1 - E.f) * hypot(sa, t))) # destination longitude in [-180, 180) b = degrees180( atan2(ss * si, c1 * cs - s1 * ss * ci) - _dl(E.f, c2a, sa, s, cs, ss, c2sm) + radians(self.lon)) h = self.height if height is None else height r = self.classof(a, b, height=h, datum=self.datum), r return r
def rhumbMidpointTo(self, other, height=None): '''Return the (loxodromic) midpoint between this and an other point. @param other: The other point (spherical LatLon). @keyword height: Optional height, overriding the mean height (meter or same unit as radius). @return: The midpoint (spherical LatLon). @raise TypeError: The other point is not spherical. @example: >>> p = LatLon(51.127, 1.338) >>> q = LatLon(50.964, 1.853) >>> m = p.rhumb_midpointTo(q) >>> m.toStr() # '51.0455°N, 001.5957°E' ''' self.others(other) # see <http://mathforum.org/library/drmath/view/51822.html> a1, b1 = self.to2ab() a2, b2 = other.to2ab() if abs(b2 - b1) > PI: b1 += PI2 # crossing anti-meridian a3 = favg(a1, a2) b3 = favg(b1, b2) f1 = tanPI_2_2(a1) if abs(f1) > EPS: f2 = tanPI_2_2(a2) f = f2 / f1 if abs(f) > EPS: f = log(f) if abs(f) > EPS: f3 = tanPI_2_2(a3) b3 = fsum_(b1 * log(f2), -b2 * log(f1), (b2 - b1) * log(f3)) / f h = self._havg(other) if height is None else height return self.classof(degrees90(a3), degrees180(b3), height=h)
def _destination2(a, b, r, t): '''(INTERNAL) Computes destination lat-/longitude. @param a: Latitude (radians). @param b: Longitude (radians). @param r: Angular distance (radians). @param t: Bearing (compass radians). @return: 2-Tuple (lat, lon) of (degrees90, degrees180). ''' # see <http://www.edwilliams.org/avform.htm#LL> ca, cr, ct = map1(cos, a, r, t) sa, sr, st = map1(sin, a, r, t) a = asin(ct * sr * ca + cr * sa) d = atan2(st * sr * ca, cr - sa * sin(a)) # note, use addition, not subtraction of d (since # in EdWilliams.org/avform.htm W is + and E is -) return degrees90(a), degrees180(b + d)
def rhumbMidpointTo(self, other): '''Return the loxodromic midpoint between this and an other point. @param other: The other point (spherical LatLon). @return: The midpoint (spherical LatLon). @raise TypeError: The other point is not spherical. @example: >>> p = LatLon(51.127, 1.338) >>> q = LatLon(50.964, 1.853) >>> m = p.rhumb_midpointTo(q) >>> m.toStr() # '51.0455°N, 001.5957°E' ''' self.others(other) # see http://mathforum.org/library/drmath/view/51822.html a1, b1 = self.to2ab() a2, b2 = other.to2ab() if abs(b2 - b1) > PI: b1 += PI2 # crossing anti-meridian a3 = (a1 + a2) * 0.5 b3 = (b1 + b2) * 0.5 f1 = tanPI_2_2(a1) if abs(f1) > EPS: f2 = tanPI_2_2(a2) f = f2 / f1 if abs(f) > EPS: f = log(f) if abs(f) > EPS: f3 = tanPI_2_2(a3) b3 = (b1 * log(f2) - b2 * log(f1) + (b2 - b1) * log(f3)) / f return self.topsub(degrees90(a3), degrees180(b3), height=self._alter(other))
def midpointTo(self, other, height=None, wrap=False): '''Find the midpoint between this and an other point. @param other: The other point (L{LatLon}). @keyword height: Optional height for midpoint, overriding the mean height (meter). @keyword wrap: Wrap and unroll longitudes (bool). @return: Midpoint (L{LatLon}). @raise TypeError: The I{other} point is not L{LatLon}. @example: >>> p1 = LatLon(52.205, 0.119) >>> p2 = LatLon(48.857, 2.351) >>> m = p1.midpointTo(p2) # '50.5363°N, 001.2746°E' ''' self.others(other) # see <http://mathforum.org/library/drmath/view/51822.html> a1, b1 = self.to2ab() a2, b2 = other.to2ab() db, b2 = unrollPI(b1, b2, wrap=wrap) ca1, ca2, cdb = map1(cos, a1, a2, db) sa1, sa2, sdb = map1(sin, a1, a2, db) x = ca2 * cdb + ca1 y = ca2 * sdb a = atan2(sa1 + sa2, hypot(x, y)) b = atan2(y, x) + b1 if height is None: h = self._havg(other) else: h = height return self.classof(degrees90(a), degrees180(b), height=h)
def toLatLon(self, LatLon=None, datum=Datums.WGS84): '''Convert this OSGR coordinate to an (ellipsoidal) geodetic point. I{Note formulation implemented here due to Thomas, Redfearn, etc. is as published by OS, but is inferior to Krüger as used by e.g. Karney 2011.} @keyword LatLon: Optional ellipsoidal LatLon class to use for the point (I{LatLon}). @keyword datum: Optional datum to use (I{Datum}). @return: The geodetic point (I{LatLon}) or 3-tuple (lat, lon, datum) if I{LatLon} is None. @raise TypeError: If I{LatLon} is not ellipsoidal or if I{datum} conversion failed. @example: >>> from pygeodesy import ellipsoidalVincenty as eV >>> g = Osgr(651409.903, 313177.270) >>> p = g.toLatLon(eV.LatLon) # 52°39′28.723″N, 001°42′57.787″E >>> # to obtain (historical) OSGB36 lat-/longitude point >>> p = g.toLatLon(eV.LatLon, datum=Datums.OSGB36) # 52°39′27.253″N, 001°43′04.518″E ''' if self._latlon: return self._latlon3(LatLon, datum) E = _OSGB36.ellipsoid # Airy130 a_F0 = E.a * _F0 b_F0 = E.b * _F0 e, n = self._easting, self._northing n_N0 = n - _N0 a, M = _A0, 0 sa = Fsum(a) while True: t = n_N0 - M if t < _10um: break sa.fadd(t / a_F0) a = sa.fsum() M = b_F0 * _M(E.Mabcd, a) ca, sa, ta = cos(a), sin(a), tan(a) s = E.e2s2(sa) v = a_F0 / sqrt(s) # nu r = v * E.e12 / s # rho vr = v / r # == s / E.e12 x2 = vr - 1 # η2 v3, v5, v7 = fpowers(v, 7, 3) # PYCHOK false! ta2, ta4, ta6 = fpowers(ta**2, 3) # PYCHOK false! tar = ta / r V4 = (a, tar / (2 * v), tar / (24 * v3) * fdot( (1, 3, -9), 5 + x2, ta2, ta2 * x2), tar / (720 * v5) * fdot( (61, 90, 45), 1, ta2, ta4)) csa = 1.0 / ca X5 = (_B0, csa / v, csa / (6 * v3) * fsum_(vr, ta, ta), csa / (120 * v5) * fdot( (5, 28, 24), 1, ta2, ta4), csa / (5040 * v7) * fdot( (61, 662, 1320, 720), ta, ta2, ta4, ta6)) d, d2, d3, d4, d5, d6, d7 = fpowers(e - _E0, 7) # PYCHOK false! a = fdot(V4, 1, -d2, d4, -d6) b = fdot(X5, 1, d, -d3, d5, -d7) self._latlon = _eLLb(degrees90(a), degrees180(b), datum=_OSGB36) return self._latlon3(LatLon, datum)
def opt3(self): '''Get the optional meridian (degrees180). ''' return degrees180(self._opt3)
def lon0(self): '''Get the central meridian (degrees180). ''' return degrees180(self._lon0)
def toLatLon(self, LatLon, datum=Datums.WGS84): '''Convert this OSGR coordinate to an ellipsoidal lat-/longitude point. Note formulation implemented here due to Thomas, Redfearn, etc. is as published by OS, but is inferior to Krüger as used by e.g. Karney 2011. @param LatLon: Ellipsoidal LatLon class to use (L{LatLon}). @param datum: Darum to use (L{Datum}). @return: The elliposoidal point (L{LatLon}). @raise TypeError: If L{LatLon} is not ellipsoidal. @example: >>> from pygeodesy import ellipsoidalVincenty as eV >>> g = Osgr(651409.903, 313177.270) >>> p = g.toLatLon(ev.LatLon) # 52°39′28.723″N, 001°42′57.787″E >>> # to obtain (historical) OSGB36 lat-/longitude point >>> p = g.toLatLon(ev.LatLon, datum=Datums.OSGB36) # 52°39′27.253″N, 001°43′04.518″E ''' if self._latlon and self._latlon.__class__ is LatLon \ and self._latlon.datum == datum: return self._latlon # set below if not issubclass(LatLon, LatLonEllipsoidalBase): raise TypeError('%s not %s: %r' % ('LatLon', 'ellipsoidal', LatLon)) E = _OSGB36.ellipsoid # Airy130 Mabcd = E.Mabcd e, n = self._easting, self._northing a, M = _A0, 0 while True: t = n - _N0 - M if t < _10um: break a += t / (E.a * _F0) M = E.b * _M(Mabcd, a) ca, sa, ta = cos(a), sin(a), tan(a) s = 1 - E.e2 * sa * sa v = E.a * _F0 / sqrt(s) r = v * E.e12 / s x2 = v / r - 1 # η v3 = v * v * v v5 = v * v * v3 v7 = v * v * v5 ta2 = ta * ta ta4 = ta2 * ta2 ta6 = ta4 * ta2 V4 = (a, ta / ( 2 * r * v), ta / ( 24 * r * v3) * fdot((5, 3, 1, -9), 1, ta2, x2, x2 * ta2), ta / (720 * r * v5) * fdot((61, 90, 45), 1, ta2, ta4)) sca = 1 / ca X5 = (_B0, sca / v, sca / ( 6 * v3) * (v / r + 2 * ta), sca / ( 120 * v5) * fdot((5, 28, 24), 1, ta2, ta4), sca / (5040 * v7) * fdot((61, 662, 1320, 720), ta, ta2, ta4, ta6)) d = e - _E0 d2 = d * d d4 = d2 * d2 d6 = d2 * d4 a = fdot(V4, 1, -d2, d4, -d6) b = fdot(X5, 1, d, -d2 * d, d4 * d, -d6 * d) ll = LatLon(degrees90(a), degrees180(b), datum=_OSGB36) if datum != _OSGB36: ll = ll.convertDatum(datum) ll = LatLon(ll.lat, ll.lon, datum=datum) self._latlon = ll return ll
def toLatLon(self, LatLon): '''Convert this UTM coordinate to an (ellipsoidal) geodetic point. @param LatLon: The I{LatLon} class to use for the point (I{LatLon}) or None. @return: Point of this UTM coordinate (I{LatLon}) or 5-tuple (lat, lon, datum, convergence, scale) if I{LatLon} is None. @raise TypeError: If I{LatLon} is not ellipsoidal. @raise ValueError: Invalid meridional radius or H-value. @example: >>> u = Utm(31, 'N', 448251.795, 5411932.678) >>> from pygeodesy import ellipsoidalVincenty as eV >>> ll = u.toLatLon(eV.LatLon) # 48°51′29.52″N, 002°17′40.20″E ''' if self._latlon: return self._latlon5(LatLon) E = self._datum.ellipsoid # XXX vs LatLon.datum.ellipsoid x = self._easting - _FalseEasting # relative to central meridian y = self._northing if self._hemi == 'S': # relative to equator y -= _FalseNorthing # from Karney 2011 Eq 15-22, 36 A0 = _K0 * E.A if A0 < EPS: raise ValueError('%s invalid: %r' % ('meridional', E.A)) x /= A0 # η eta y /= A0 # ξ ksi B6 = _K6s(E.Beta6, x, y) # 6th-order Krüger series, 1-origin y = -B6.ys(-y) # ξ' x = -B6.xs(-x) # η' shx = sinh(x) cy, sy = cos(y), sin(y) H = hypot(shx, cy) if H < EPS: raise ValueError('%s invalid: %r' % ('H', H)) T = t0 = sy / H # τʹ q = 1.0 / E.e12 d = 1 sd = Fsum(T) # toggles on +/-1.12e-16 eg. 31 N 400000 5000000 while abs(d) > EPS: # 1e-12 h = hypot1(T) s = sinh(E.e * atanh(E.e * T / h)) t = T * hypot1(s) - s * h d = (t0 - t) / hypot1(t) * (q + T**2) / h sd.fadd(d) T = sd.fsum() # τi a = atan(T) # lat b = atan2(shx, cy) + radians(_cmlon(self._zone)) ll = _eLLb(degrees90(a), degrees180(b), datum=self._datum) # convergence: Karney 2011 Eq 26, 27 p = -B6.ps(-1) q = B6.qs(0) ll._convergence = degrees(atan(tan(y) * tanh(x)) + atan2(q, p)) # scale: Karney 2011 Eq 28 ll._scale = E.e2s(sin(a)) * hypot1(T) * H * (A0 / E.a / hypot(p, q)) self._latlon = ll return self._latlon5(LatLon)