/**************************************************************************** * * (c) 2009-2016 QGROUNDCONTROL PROJECT * * QGroundControl is licensed according to the terms in the file * COPYING.md in the root of the source code directory. * ****************************************************************************/ #include "QGCMapPolyline.h" #include "QGCGeo.h" #include "JsonHelper.h" #include "QGCQGeoCoordinate.h" #include "QGCApplication.h" #include #include #include #include #include #include const char* QGCMapPolyline::jsonPolylineKey = "polyline"; QGCMapPolyline::QGCMapPolyline(QObject* parent) : QObject (parent) , _dirty (false) , _interactive (false) { _init(); } QGCMapPolyline::QGCMapPolyline(const QGCMapPolyline& other, QObject* parent) : QObject (parent) , _dirty (false) , _interactive (false) { *this = other; _init(); } const QGCMapPolyline& QGCMapPolyline::operator=(const QGCMapPolyline& other) { clear(); QVariantList vertices = other.path(); for (int i=0; i()); } setDirty(true); return *this; } void QGCMapPolyline::_init(void) { connect(&_polylineModel, &QmlObjectListModel::dirtyChanged, this, &QGCMapPolyline::_polylineModelDirtyChanged); connect(&_polylineModel, &QmlObjectListModel::countChanged, this, &QGCMapPolyline::_polylineModelCountChanged); } void QGCMapPolyline::clear(void) { _polylinePath.clear(); emit pathChanged(); _polylineModel.clearAndDeleteContents(); emit cleared(); setDirty(true); } void QGCMapPolyline::adjustVertex(int vertexIndex, const QGeoCoordinate coordinate) { _polylinePath[vertexIndex] = QVariant::fromValue(coordinate); emit pathChanged(); _polylineModel.value(vertexIndex)->setCoordinate(coordinate); setDirty(true); } void QGCMapPolyline::setDirty(bool dirty) { if (_dirty != dirty) { _dirty = dirty; if (!dirty) { _polylineModel.setDirty(false); } emit dirtyChanged(dirty); } } QGeoCoordinate QGCMapPolyline::_coordFromPointF(const QPointF& point) const { QGeoCoordinate coord; if (_polylinePath.count() > 0) { QGeoCoordinate tangentOrigin = _polylinePath[0].value(); convertNedToGeo(-point.y(), point.x(), 0, tangentOrigin, &coord); } return coord; } QPointF QGCMapPolyline::_pointFFromCoord(const QGeoCoordinate& coordinate) const { if (_polylinePath.count() > 0) { double y, x, down; QGeoCoordinate tangentOrigin = _polylinePath[0].value(); convertGeoToNed(coordinate, tangentOrigin, &y, &x, &down); return QPointF(x, -y); } return QPointF(); } void QGCMapPolyline::setPath(const QList& path) { _polylinePath.clear(); _polylineModel.clearAndDeleteContents(); foreach (const QGeoCoordinate& coord, path) { _polylinePath.append(QVariant::fromValue(coord)); _polylineModel.append(new QGCQGeoCoordinate(coord, this)); } setDirty(true); emit pathChanged(); } void QGCMapPolyline::setPath(const QVariantList& path) { _polylinePath = path; _polylineModel.clearAndDeleteContents(); for (int i=0; i<_polylinePath.count(); i++) { _polylineModel.append(new QGCQGeoCoordinate(_polylinePath[i].value(), this)); } setDirty(true); emit pathChanged(); } void QGCMapPolyline::saveToJson(QJsonObject& json) { QJsonValue jsonValue; JsonHelper::saveGeoCoordinateArray(_polylinePath, false /* writeAltitude*/, jsonValue); json.insert(jsonPolylineKey, jsonValue); setDirty(false); } bool QGCMapPolyline::loadFromJson(const QJsonObject& json, bool required, QString& errorString) { errorString.clear(); clear(); if (required) { if (!JsonHelper::validateRequiredKeys(json, QStringList(jsonPolylineKey), errorString)) { return false; } } else if (!json.contains(jsonPolylineKey)) { return true; } if (!JsonHelper::loadGeoCoordinateArray(json[jsonPolylineKey], false /* altitudeRequired */, _polylinePath, errorString)) { return false; } for (int i=0; i<_polylinePath.count(); i++) { _polylineModel.append(new QGCQGeoCoordinate(_polylinePath[i].value(), this)); } setDirty(false); emit pathChanged(); return true; } QList QGCMapPolyline::coordinateList(void) const { QList coords; for (int i=0; i<_polylinePath.count(); i++) { coords.append(_polylinePath[i].value()); } return coords; } void QGCMapPolyline::splitSegment(int vertexIndex) { int nextIndex = vertexIndex + 1; if (nextIndex > _polylinePath.length() - 1) { return; } QGeoCoordinate firstVertex = _polylinePath[vertexIndex].value(); QGeoCoordinate nextVertex = _polylinePath[nextIndex].value(); double distance = firstVertex.distanceTo(nextVertex); double azimuth = firstVertex.azimuthTo(nextVertex); QGeoCoordinate newVertex = firstVertex.atDistanceAndAzimuth(distance / 2, azimuth); if (nextIndex == 0) { appendVertex(newVertex); } else { _polylineModel.insert(nextIndex, new QGCQGeoCoordinate(newVertex, this)); _polylinePath.insert(nextIndex, QVariant::fromValue(newVertex)); emit pathChanged(); } } void QGCMapPolyline::appendVertex(const QGeoCoordinate& coordinate) { _polylinePath.append(QVariant::fromValue(coordinate)); _polylineModel.append(new QGCQGeoCoordinate(coordinate, this)); emit pathChanged(); } void QGCMapPolyline::removeVertex(int vertexIndex) { if (vertexIndex < 0 || vertexIndex > _polylinePath.length() - 1) { qWarning() << "Call to removeVertex with bad vertexIndex:count" << vertexIndex << _polylinePath.length(); return; } if (_polylinePath.length() <= 2) { // Don't allow the user to trash the polyline return; } QObject* coordObj = _polylineModel.removeAt(vertexIndex); coordObj->deleteLater(); _polylinePath.removeAt(vertexIndex); emit pathChanged(); } void QGCMapPolyline::setInteractive(bool interactive) { if (_interactive != interactive) { _interactive = interactive; emit interactiveChanged(interactive); } } QGeoCoordinate QGCMapPolyline::vertexCoordinate(int vertex) const { if (vertex >= 0 && vertex < _polylinePath.count()) { return _polylinePath[vertex].value(); } else { qWarning() << "QGCMapPolyline::vertexCoordinate bad vertex requested"; return QGeoCoordinate(); } } QList QGCMapPolyline::nedPolyline(void) { QList nedPolyline; if (count() > 0) { QGeoCoordinate tangentOrigin = vertexCoordinate(0); for (int i=0; i<_polylinePath.count(); i++) { double y, x, down; QGeoCoordinate vertex = vertexCoordinate(i); if (i == 0) { // This avoids a nan calculation that comes out of convertGeoToNed x = y = 0; } else { convertGeoToNed(vertex, tangentOrigin, &y, &x, &down); } nedPolyline += QPointF(x, y); } } return nedPolyline; } QList QGCMapPolyline::offsetPolyline(double distance) { QList rgNewPolyline; // I'm sure there is some beautiful famous algorithm to do this, but here is a brute force method if (count() > 1) { // Convert the polygon to NED QList rgNedVertices = nedPolyline(); // Walk the edges, offsetting by the specified distance QList rgOffsetEdges; for (int i=0; ishowMessage(tr("File not found: %1").arg(kmlFile)); return false; } if (!file.open(QIODevice::ReadOnly)) { qgcApp()->showMessage(tr("Unable to open file: %1 error: $%2").arg(kmlFile).arg(file.errorString())); return false; } QDomDocument doc; QString errorMessage; int errorLine; if (!doc.setContent(&file, &errorMessage, &errorLine)) { qgcApp()->showMessage(tr("Unable to parse KML file: %1 error: %2 line: %3").arg(kmlFile).arg(errorMessage).arg(errorLine)); return false; } QDomNodeList rgNodes = doc.elementsByTagName("Polygon"); if (rgNodes.count() == 0) { qgcApp()->showMessage(tr("Unable to find Polygon node in KML")); return false; } QDomNode coordinatesNode = rgNodes.item(0).namedItem("outerBoundaryIs").namedItem("LinearRing").namedItem("coordinates"); if (coordinatesNode.isNull()) { qgcApp()->showMessage(tr("Internal error: Unable to find coordinates node in KML")); return false; } QString coordinatesString = coordinatesNode.toElement().text().simplified(); QStringList rgCoordinateStrings = coordinatesString.split(" "); QList rgCoords; for (int i=0; i rgReversed; for (int i=0; i(); QGeoCoordinate to = _polylinePath[i+1].value(); length += from.distanceTo(to); } return length; }