swift
Loading...
Searching...
No Matches
earthangle.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QRegularExpression>
7#include <Qt>
8#include <QtGlobal>
9
10#include "misc/geo/latitude.h"
11#include "misc/geo/longitude.h"
12#include "misc/pq/units.h"
13
14using namespace swift::misc::physical_quantities;
15
16namespace swift::misc::geo
17{
18 template <class LATorLON>
24
25 template <class LATorLON>
31
32 template <class LATorLON>
34 {
36 return *this;
37 }
38
39 template <class LATorLON>
40 LATorLON CEarthAngle<LATorLON>::operator+(const CEarthAngle &latOrLon) const
41 {
42 LATorLON l(*this);
43 l += latOrLon;
44 return l;
45 }
46
47 template <class LATorLON>
48 LATorLON CEarthAngle<LATorLON>::operator-(const CEarthAngle &latOrLon) const
49 {
50 LATorLON l(*this);
51 l -= latOrLon;
52 return l;
53 }
54
55 template <class LATorLON>
56 LATorLON CEarthAngle<LATorLON>::operator*(double multiply) const
57 {
58 LATorLON l(*this);
59 l *= multiply;
60 return l;
61 }
62
63 template <class LATorLON>
64 LATorLON CEarthAngle<LATorLON>::fromWgs84(const QString &wgsCoordinate)
65 {
66 const QString wgs = wgsCoordinate.simplified().trimmed();
67 if (wgs.isEmpty())
68 {
69 return LATorLON();
70 }
71
72 // support for 5deg, 1.2rad
73 if (CAngleUnit::deg().endsStringWithNameOrSymbol(wgs) || CAngleUnit::rad().endsStringWithNameOrSymbol(wgs))
74 {
75 LATorLON latOrLon;
76 latOrLon.parseFromString(wgs);
77 return latOrLon;
78 }
79
80 // number only -> parsed as degrees
81 bool isDouble {};
82 const double valueDegrees = wgs.toDouble(&isDouble);
83 if (isDouble)
84 {
85 CAngle a(valueDegrees, CAngleUnit::deg());
86 return LATorLON(a);
87 }
88
89 // http://www.regular-expressions.info/floatingpoint.html
90 thread_local const QRegularExpression rx(R"([+-]?\d+(?:\.\d+)?)");
91 int deg = 0;
92 int min = 0;
93 double sec = 0.0;
94 int c = 0;
96 while (i.hasNext() && c < 3)
97 {
98 const QRegularExpressionMatch match = i.next();
99 bool ok {};
100 if (match.hasMatch())
101 {
102 const QString cap = match.captured(0);
103 switch (c++)
104 {
105 case 0: deg = cap.toInt(&ok); break;
106 case 1: min = cap.toInt(&ok); break;
107 case 2: sec = cap.toDouble(&ok); break;
108 default: break;
109 }
110 }
111 Q_UNUSED(ok); // ok for debugging purposes
112 }
113
114 if (wgs.contains('S', Qt::CaseInsensitive) || wgs.contains('W', Qt::CaseInsensitive))
115 {
116 deg *= -1;
117 min *= -1;
118 sec *= -1;
119 }
120
121 physical_quantities::CAngle a(deg, min, sec);
122 return LATorLON(a);
123 }
124
125 template <class LATorLON>
127 : physical_quantities::CAngle(0.0, swift::misc::physical_quantities::CAngleUnit::deg())
128 {}
129
130 template <class LATorLON>
132 : physical_quantities::CAngle(value, unit)
133 {}
134
135 template <class LATorLON>
137 : physical_quantities::CAngle(angle)
138 {}
139
140 template <class LATorLON>
145
146 template <class LATorLON>
147 LATorLON const *CEarthAngle<LATorLON>::derived() const
148 {
149 return static_cast<LATorLON const *>(this);
150 }
151
152 template <class LATorLON>
153 LATorLON *CEarthAngle<LATorLON>::derived()
154 {
155 return static_cast<LATorLON *>(this);
156 }
157
158 template <class LATorLON>
160 {
161 return CIcons::GeoPosition;
162 }
163
164 template <class LATorLON>
165 QString CEarthAngle<LATorLON>::toWgs84(const QChar pos, const QChar neg, int fractionalDigits) const
166 {
168 const QChar pn = v.sign < 0 ? neg : pos;
169
170 static const QString vs("%1° %2' %3\" %4");
171 if (fractionalDigits < 1)
172 {
173 return vs.arg(v.deg).arg(v.min).arg(v.sec).arg(pn);
174 }
175
176 static const QString vsf("%1° %2' %3.%4\" %5");
177 return vsf.arg(v.deg).arg(v.min).arg(v.sec).arg(v.fractionalSecAsString(fractionalDigits)).arg(pn);
178 }
179
180 // see here for the reason of thess forward instantiations
181 // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
183 template class CEarthAngle<CLatitude>;
184 template class CEarthAngle<CLongitude>;
186
187} // namespace swift::misc::geo
static CAngleUnit rad()
Radians.
Definition units.h:268
static CAngleUnit deg()
Degrees.
Definition units.h:276
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
Base class for latitude / longitude.
Definition earthangle.h:21
CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
QString toWgs84(const QChar pos, const QChar neg, int fractionalDigits=3) const
To WGS84 string.
CEarthAngle & operator*=(double multiply)
Multiply operator *=.
CEarthAngle & operator+=(const CEarthAngle &latOrLon)
Plus operator +=.
static LATorLON fromWgs84(const QString &wgsCoordinate)
Latitude / Longitude from a WGS string such as.
LATorLON operator+(const CEarthAngle &latOrLon) const
Plus operator +.
CEarthAngle()
Default constructor.
QString convertToQString(bool i18n=false) const
Cast as QString.
LATorLON operator*(double multiply) const
Multiply operator *.
CEarthAngle & operator-=(const CEarthAngle &latOrLon)
Minus operator-=.
Physical unit angle (radians, degrees).
Definition angle.h:23
CAngle()
Default constructor.
Definition angle.h:26
DegMinSecFractionalSec asSexagesimalDegMinSec(bool range180Degrees=false) const
As individual values.
Definition angle.cpp:47
Specialized class for angles (degrees, radian).
Definition units.h:231
static CAngleUnit deg()
Degrees.
Definition units.h:276
CPhysicalQuantity & operator+=(const CPhysicalQuantity &other)
CPhysicalQuantity & operator-=(const CPhysicalQuantity &other)
QString valueRoundedWithUnit(const CAngleUnit &unit, int digits=-1, bool withGroupSeparator=false, bool i18n=false) const
Free functions in swift::misc.
QRegularExpressionMatchIterator globalMatch(QStringView subjectView, qsizetype offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
QString captured(QAnyStringView name) const const
bool hasMatch() const const
QRegularExpressionMatch next()
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString simplified() const const
double toDouble(bool *ok) const const
int toInt(bool *ok, int base) const const
QString trimmed() const const
CaseInsensitive
Value as individual values.
Definition angle.h:36
QString fractionalSecAsString(int width=-1) const
Fractional seconds as string.
Definition angle.h:53