Human-friendly links
[GPXSee.git] / src / coordinates.cpp
blob34e21e14168520ccdbedd559ce4eb2575642a8df
1 #include "rd.h"
2 #include "wgs84.h"
3 #include "coordinates.h"
5 #define MIN_LAT deg2rad(-90.0)
6 #define MAX_LAT deg2rad(90.0)
7 #define MIN_LON deg2rad(-180.0)
8 #define MAX_LON deg2rad(180.0)
10 qreal Coordinates::distanceTo(const Coordinates &c) const
12 qreal dLat = deg2rad(c.lat() - _lat);
13 qreal dLon = deg2rad(c.lon() - _lon);
14 qreal a = pow(sin(dLat / 2.0), 2.0)
15 + cos(deg2rad(_lat)) * cos(deg2rad(c.lat())) * pow(sin(dLon / 2.0), 2.0);
17 return (WGS84_RADIUS * (2.0 * atan2(sqrt(a), sqrt(1.0 - a))));
20 QPointF Coordinates::toMercator() const
22 return QPointF(_lon, rad2deg(log(tan(M_PI/4.0 + deg2rad(_lat)/2.0))));
25 Coordinates Coordinates::fromMercator(const QPointF &m)
27 return Coordinates(m.x(), rad2deg(2 * atan(exp(deg2rad(m.y()))) - M_PI/2));
30 bool operator==(const Coordinates &c1, const Coordinates &c2)
32 return (c1.lat() == c2.lat() && c1.lon() == c2.lon());
35 QDebug operator<<(QDebug dbg, const Coordinates &coordinates)
37 dbg.nospace() << "Coordinates(" << coordinates.lon() << ", "
38 << coordinates.lat() << ")";
40 return dbg.maybeSpace();
43 QPair<Coordinates, Coordinates> Coordinates::boundingRect(qreal distance) const
45 qreal radDist = distance / WGS84_RADIUS;
47 qreal minLat = deg2rad(_lat) - radDist;
48 qreal maxLat = deg2rad(_lat) + radDist;
50 qreal minLon, maxLon;
51 if (minLat > MIN_LAT && maxLat < MAX_LAT) {
52 qreal deltaLon = asin(sin(radDist) / cos(_lat));
53 minLon = deg2rad(_lon) - deltaLon;
54 if (minLon < MIN_LON)
55 minLon += 2.0 * M_PI;
56 maxLon = deg2rad(_lon) + deltaLon;
57 if (maxLon > MAX_LON)
58 maxLon -= 2.0 * M_PI;
59 } else {
60 // a pole is within the distance
61 minLat = qMax(minLat, MIN_LAT);
62 maxLat = qMin(maxLat, MAX_LAT);
63 minLon = MIN_LON;
64 maxLon = MAX_LON;
67 return QPair<Coordinates, Coordinates>(Coordinates(rad2deg(qMin(minLon,
68 maxLon)), rad2deg(qMin(minLat, maxLat))), Coordinates(rad2deg(qMax(minLon,
69 maxLon)), rad2deg(qMax(minLat, maxLat))));