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
;
51 if (minLat
> MIN_LAT
&& maxLat
< MAX_LAT
) {
52 qreal deltaLon
= asin(sin(radDist
) / cos(_lat
));
53 minLon
= deg2rad(_lon
) - deltaLon
;
56 maxLon
= deg2rad(_lon
) + deltaLon
;
60 // a pole is within the distance
61 minLat
= qMax(minLat
, MIN_LAT
);
62 maxLat
= qMin(maxLat
, MAX_LAT
);
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
))));