13#include <QtCore/QLocale>
14#include <QtCore/QRegularExpression>
15#include <QtCore/QString>
19#include <GeographicLib/Geocentric.hpp>
20#include <GeographicLib/Geodesic.hpp>
21#include <GeographicLib/GeodesicLine.hpp>
22#include <GeographicLib/LocalCartesian.hpp>
23#include <GeographicLib/MGRS.hpp>
24#include <GeographicLib/PolygonArea.hpp>
25#include <GeographicLib/UTMUPS.hpp>
36void convertGeoToNed(
const QGeoCoordinate &coord,
const QGeoCoordinate &origin,
double &x,
double &y,
double &z)
38 if (coord == origin) {
44 const double originAlt = std::isnan(origin.altitude()) ? 0.0 : origin.altitude();
45 const double coordAlt = std::isnan(coord.altitude()) ? 0.0 : coord.altitude();
47 double east, north, up;
48 GeographicLib::LocalCartesian ltp(origin.latitude(), origin.longitude(), originAlt,
49 GeographicLib::Geocentric::WGS84());
50 ltp.Forward(coord.latitude(), coord.longitude(), coordAlt, east, north, up);
58void convertNedToGeo(
double x,
double y,
double z,
const QGeoCoordinate &origin, QGeoCoordinate &coord)
61 const double east = y;
62 const double north = x;
66 const double originAlt = std::isnan(origin.altitude()) ? 0.0 : origin.altitude();
69 GeographicLib::LocalCartesian ltp(origin.latitude(), origin.longitude(), originAlt,
70 GeographicLib::Geocentric::WGS84());
71 ltp.Reverse(east, north, up, lat, lon, alt);
73 coord.setLatitude(lat);
74 coord.setLongitude(lon);
75 coord.setAltitude(alt);
85 GeographicLib::LocalCartesian ltp(ref.latitude(), ref.longitude(), ref.altitude(),
86 GeographicLib::Geocentric::WGS84());
87 ltp.Forward(coord.latitude(), coord.longitude(), coord.altitude(), x, y, z);
88 return QVector3D(x, y, z);
94 GeographicLib::LocalCartesian ltp(ref.latitude(), ref.longitude(), ref.altitude(),
95 GeographicLib::Geocentric::WGS84());
96 ltp.Reverse(enu.x(), enu.y(), enu.z(), lat, lon, alt);
97 return QGeoCoordinate(lat, lon, alt);
107 GeographicLib::Geocentric::WGS84().Forward(coord.latitude(), coord.longitude(), coord.altitude(), x, y, z);
108 return QVector3D(x, y, z);
113 double lat, lon, alt;
114 GeographicLib::Geocentric::WGS84().Reverse(ecef.x(), ecef.y(), ecef.z(), lat, lon, alt);
115 return QGeoCoordinate(lat, lon, alt);
122 GeographicLib::Geocentric::WGS84().Reverse(ecef.x(), ecef.y(), ecef.z(), lat, lon, h);
126 GeographicLib::LocalCartesian ltp(ref.latitude(), ref.longitude(), ref.altitude(),
127 GeographicLib::Geocentric::WGS84());
128 ltp.Forward(lat, lon, h, x, y, z);
129 return QVector3D(x, y, z);
136 GeographicLib::LocalCartesian ltp(ref.latitude(), ref.longitude(), ref.altitude(),
137 GeographicLib::Geocentric::WGS84());
138 ltp.Reverse(enu.x(), enu.y(), enu.z(), lat, lon, h);
142 GeographicLib::Geocentric::WGS84().Forward(lat, lon, h, x, y, z);
143 return QVector3D(x, y, z);
155 GeographicLib::UTMUPS::Forward(coord.latitude(), coord.longitude(), zone, northp, easting, northing);
157 }
catch (
const GeographicLib::GeographicErr &e) {
158 qCDebug(QGCGeoLog) << e.what();
163bool convertUTMToGeo(
double easting,
double northing,
int zone,
bool southhemi, QGeoCoordinate &coord)
167 GeographicLib::UTMUPS::Reverse(zone, !southhemi, easting, northing, lat, lon);
168 coord.setLatitude(lat);
169 coord.setLongitude(lon);
171 }
catch (
const GeographicLib::GeographicErr &e) {
172 qCDebug(QGCGeoLog) << e.what();
187 GeographicLib::UTMUPS::Forward(coord.latitude(), coord.longitude(), zone, northp, x, y);
190 GeographicLib::MGRS::Forward(zone, northp, x, y, coord.latitude(), 5, mgrs);
193 const QString qstr = QString::fromStdString(mgrs);
194 for (
int i = qstr.length() - 1; i >= 0; i--) {
195 if (!qstr.at(i).isDigit()) {
196 const int numLen = (qstr.length() - i - 1) / 2;
197 return qstr.left(i + 1) +
" " + qstr.mid(i + 1, numLen) +
" " + qstr.mid(i + 1 + numLen);
201 }
catch (
const GeographicLib::GeographicErr &e) {
202 qCDebug(QGCGeoLog) << e.what();
213 GeographicLib::MGRS::Reverse(mgrs.simplified().replace(
" ",
"").toStdString(), zone, northp, x, y, prec);
216 GeographicLib::UTMUPS::Reverse(zone, northp, x, y, lat, lon);
218 coord.setLatitude(lat);
219 coord.setLongitude(lon);
221 }
catch (
const GeographicLib::GeographicErr &e) {
222 qCDebug(QGCGeoLog) << e.what();
233 const QChar hemisphere = isLatitude ? ((value < 0) ? QLatin1Char(
'S') : QLatin1Char(
'N'))
234 : ((value < 0) ? QLatin1Char(
'W') : QLatin1Char(
'E'));
237 const qint64 hundredths = std::llround(std::abs(value) * 360000.0);
238 const qint64 degrees = hundredths / 360000;
239 const qint64 minutes = (hundredths % 360000) / 6000;
240 const double seconds =
static_cast<double>(hundredths % 6000) / 100.0;
242 return QStringLiteral(
"%1° %2' %3\" %4").arg(degrees).arg(minutes).arg(seconds, 0,
'f', 2).arg(hemisphere);
251 QString s = text.trimmed().toUpper();
257 const QChar positive = isLatitude ? QLatin1Char(
'N') : QLatin1Char(
'E');
258 const QChar negative = isLatitude ? QLatin1Char(
'S') : QLatin1Char(
'W');
260 if (s.back().isLetter()) {
261 hemisphere = s.back();
263 }
else if (s.front().isLetter()) {
264 hemisphere = s.front();
267 if (!hemisphere.isNull() && (hemisphere != positive) && (hemisphere != negative)) {
272 static const QRegularExpression marks(QStringLiteral(
"[°º'\"′″]"));
273 s.replace(marks, QStringLiteral(
" "));
274 static const QRegularExpression whitespace(QStringLiteral(
"\\s+"));
275 const QStringList fields = s.split(whitespace, Qt::SkipEmptyParts);
276 if (fields.isEmpty() || (fields.size() > 3)) {
280 double values[3] = {0.0, 0.0, 0.0};
281 for (qsizetype i = 0; i < fields.size(); i++) {
282 bool fieldOk =
false;
283 values[i] = QLocale::c().toDouble(fields.at(i), &fieldOk);
284 if (!fieldOk || !std::isfinite(values[i])) {
289 const bool negativeDegrees = fields.at(0).startsWith(QLatin1Char(
'-'));
290 const double minutes = values[1];
291 const double seconds = values[2];
292 if ((minutes < 0) || (minutes >= 60) || (seconds < 0) || (seconds >= 60)) {
296 if (((fields.size() > 1) && (values[0] != std::trunc(values[0]))) || ((fields.size() > 2) && (minutes != std::trunc(minutes)))) {
300 if (negativeDegrees && !hemisphere.isNull()) {
304 double degrees = std::abs(values[0]) + (minutes / 60.0) + (seconds / 3600.0);
305 if (negativeDegrees || (hemisphere == negative)) {
308 if (std::abs(degrees) > (isLatitude ? 90.0 : 180.0)) {
318bool convertDMSToGeo(
const QString &latitude,
const QString &longitude, QGeoCoordinate &coord)
324 if (!latOk || !lonOk) {
328 coord.setLatitude(lat);
329 coord.setLongitude(lon);
340 GeographicLib::Geodesic::WGS84().Inverse(from.latitude(), from.longitude(),
341 to.latitude(), to.longitude(), distance);
347 double distance, azimuth1, azimuth2;
348 GeographicLib::Geodesic::WGS84().Inverse(from.latitude(), from.longitude(),
349 to.latitude(), to.longitude(), distance, azimuth1, azimuth2);
352 if (azimuth1 < 0.0) {
361 GeographicLib::Geodesic::WGS84().Direct(from.latitude(), from.longitude(), azimuth, distance, lat, lon);
362 return QGeoCoordinate(lat, lon, from.altitude());
371 if (path.size() < 2) {
375 double totalLength = 0.0;
376 for (
int i = 1; i < path.size(); ++i) {
384 if (polygon.size() < 3) {
388 GeographicLib::PolygonArea poly(GeographicLib::Geodesic::WGS84());
389 for (
const QGeoCoordinate &coord : polygon) {
390 poly.AddPoint(coord.latitude(), coord.longitude());
393 double perimeter, area;
394 poly.Compute(
false,
true, perimeter, area);
402 if (polygon.size() < 2) {
406 GeographicLib::PolygonArea poly(GeographicLib::Geodesic::WGS84());
407 for (
const QGeoCoordinate &coord : polygon) {
408 poly.AddPoint(coord.latitude(), coord.longitude());
411 double perimeter, area;
412 poly.Compute(
false,
true, perimeter, area);
416QList<QGeoCoordinate>
interpolatePath(
const QGeoCoordinate &from,
const QGeoCoordinate &to,
int numPoints)
418 QList<QGeoCoordinate> result;
421 constexpr int kMaxPoints = 10000;
424 }
else if (numPoints > kMaxPoints) {
425 qCWarning(QGCGeoLog) <<
"interpolatePath: numPoints" << numPoints <<
"exceeds maximum, clamping to" << kMaxPoints;
426 numPoints = kMaxPoints;
430 for (
int i = 0; i < numPoints; ++i) {
437 const GeographicLib::GeodesicLine line = GeographicLib::Geodesic::WGS84().InverseLine(
438 from.latitude(), from.longitude(), to.latitude(), to.longitude());
440 const double totalDistance = line.Distance();
441 const double altDiff = to.altitude() - from.altitude();
443 for (
int i = 0; i < numPoints; ++i) {
444 const double fraction =
static_cast<double>(i) / (numPoints - 1);
445 const double distance = totalDistance * fraction;
448 line.Position(distance, lat, lon);
450 const double alt = from.altitude() + altDiff * fraction;
451 result.append(QGeoCoordinate(lat, lon, alt));
459 if (from == to || distance <= 0.0) {
463 const GeographicLib::GeodesicLine line = GeographicLib::Geodesic::WGS84().InverseLine(
464 from.latitude(), from.longitude(), to.latitude(), to.longitude());
466 const double totalDistance = line.Distance();
468 if (distance >= totalDistance) {
473 line.Position(distance, lat, lon);
476 const double fraction = distance / totalDistance;
477 const double alt = from.altitude() + (to.altitude() - from.altitude()) * fraction;
479 return QGeoCoordinate(lat, lon, alt);
Geographic coordinate conversion utilities using GeographicLib.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
QList< QGeoCoordinate > interpolatePath(const QGeoCoordinate &from, const QGeoCoordinate &to, int numPoints)
QVector3D convertEcefToEnu(const QVector3D &ecef, const QGeoCoordinate &ref)
void convertGeoToNed(const QGeoCoordinate &coord, const QGeoCoordinate &origin, double &x, double &y, double &z)
double polygonPerimeter(const QList< QGeoCoordinate > &polygon)
double geodesicAzimuth(const QGeoCoordinate &from, const QGeoCoordinate &to)
QVector3D convertGeodeticToEcef(const QGeoCoordinate &coord)
QGeoCoordinate geodesicDestination(const QGeoCoordinate &from, double azimuth, double distance)
double convertDMSToDegrees(const QString &text, bool isLatitude, bool *ok)
QGeoCoordinate convertEcefToGeodetic(const QVector3D &ecef)
QString convertGeoToMGRS(const QGeoCoordinate &coord)
double pathLength(const QList< QGeoCoordinate > &path)
double polygonArea(const QList< QGeoCoordinate > &polygon)
double geodesicDistance(const QGeoCoordinate &from, const QGeoCoordinate &to)
bool convertDMSToGeo(const QString &latitude, const QString &longitude, QGeoCoordinate &coord)
QVector3D convertEnuToEcef(const QVector3D &enu, const QGeoCoordinate &ref)
int convertGeoToUTM(const QGeoCoordinate &coord, double &easting, double &northing)
void convertNedToGeo(double x, double y, double z, const QGeoCoordinate &origin, QGeoCoordinate &coord)
QGeoCoordinate convertEnuToGps(const QVector3D &enu, const QGeoCoordinate &ref)
QVector3D convertGpsToEnu(const QGeoCoordinate &coord, const QGeoCoordinate &ref)
bool convertUTMToGeo(double easting, double northing, int zone, bool southhemi, QGeoCoordinate &coord)
QString convertDegreesToDMS(double value, bool isLatitude)
QGeoCoordinate interpolateAtDistance(const QGeoCoordinate &from, const QGeoCoordinate &to, double distance)
bool convertMGRSToGeo(const QString &mgrs, QGeoCoordinate &coord)