22 , _ignoreCenterUpdates (false)
23 , _interactive (false)
32 , _ignoreCenterUpdates (false)
33 , _interactive (false)
45void QGCMapPolygon::_init(
void)
49 connect(&_polygonModel, &QmlObjectListModel::modelReset,
this, [
this]() {
64 QVariantList vertices = other.
path();
65 QList<QGeoCoordinate> rgCoord;
66 for (
const QVariant& vertexVar: vertices) {
67 rgCoord.append(vertexVar.value<QGeoCoordinate>());
79 while (_polygonPath.count() > 1) {
80 _polygonPath.takeLast();
94 _polygonModel.clearAndDeleteContents();
103 _polygonPath[vertexIndex] = QVariant::fromValue(coordinate);
106 if (!_deferredPathChanged) {
107 _deferredPathChanged =
true;
110 QTimer::singleShot(0,
this, [
this]() {
112 _deferredPathChanged =
false;
115 QTimer::singleShot(0,
this, [
this]() {
117 _deferredPathChanged =
false;
127 if (_dirty !=
dirty) {
130 _polygonModel.setDirty(
false);
136QGeoCoordinate QGCMapPolygon::_coordFromPointF(
const QPointF& point)
const
138 QGeoCoordinate coord;
140 if (_polygonPath.count() > 0) {
141 QGeoCoordinate tangentOrigin = _polygonPath[0].value<QGeoCoordinate>();
148QPointF QGCMapPolygon::_pointFFromCoord(
const QGeoCoordinate& coordinate)
const
150 if (_polygonPath.count() > 0) {
152 QGeoCoordinate tangentOrigin = _polygonPath[0].value<QGeoCoordinate>();
155 return QPointF(x, -y);
161QPolygonF QGCMapPolygon::_toPolygonF(
void)
const
165 if (_polygonPath.count() > 2) {
166 for (
int i=0; i<_polygonPath.count(); i++) {
167 polygon.append(_pointFFromCoord(_polygonPath[i].value<QGeoCoordinate>()));
176 if (_polygonPath.count() > 2) {
177 return _toPolygonF().containsPoint(_pointFFromCoord(coordinate), Qt::OddEvenFill);
185 _polygonPath.clear();
186 _polygonModel.clearAndDeleteContents();
187 for(
const QGeoCoordinate& coord:
path) {
188 _polygonPath.append(QVariant::fromValue(coord));
200 _polygonModel.clearAndDeleteContents();
201 for (
int i=0; i<_polygonPath.count(); i++) {
202 _polygonModel.append(
new QGCQGeoCoordinate(_polygonPath[i].value<QGeoCoordinate>(),
this));
211 QJsonValue jsonValue;
235 for (
int i=0; i<_polygonPath.count(); i++) {
236 _polygonModel.append(
new QGCQGeoCoordinate(_polygonPath[i].value<QGeoCoordinate>(),
this));
247 QList<QGeoCoordinate> coords;
249 for (
int i=0; i<_polygonPath.count(); i++) {
250 coords.append(_polygonPath[i].value<QGeoCoordinate>());
258 int nextIndex = vertexIndex + 1;
259 if (nextIndex > _polygonPath.length() - 1) {
263 QGeoCoordinate firstVertex = _polygonPath[vertexIndex].value<QGeoCoordinate>();
264 QGeoCoordinate nextVertex = _polygonPath[nextIndex].value<QGeoCoordinate>();
266 double distance = firstVertex.distanceTo(nextVertex);
267 double azimuth = firstVertex.azimuthTo(nextVertex);
268 QGeoCoordinate newVertex = firstVertex.atDistanceAndAzimuth(distance / 2, azimuth);
270 if (nextIndex == 0) {
274 _polygonPath.insert(nextIndex, QVariant::fromValue(newVertex));
276 if (0 <= _selectedVertexIndex && vertexIndex < _selectedVertexIndex) {
284 _polygonPath.append(QVariant::fromValue(coordinate));
286 if (!_deferredPathChanged) {
288 _deferredPathChanged =
true;
289 QTimer::singleShot(0,
this, [
this]() {
295 _deferredPathChanged =
false;
302 QList<QObject*> objects;
305 for (
const QGeoCoordinate& coordinate: coordinates) {
307 _polygonPath.append(QVariant::fromValue(coordinate));
309 _polygonModel.append(objects);
321 QList<QGeoCoordinate> rgCoords;
322 for (
const QVariant& varCoord: varCoords) {
323 rgCoords.append(varCoord.value<QGeoCoordinate>());
328void QGCMapPolygon::_polygonModelDirtyChanged(
bool dirty)
337 if (vertexIndex < 0 || vertexIndex >= _polygonPath.length()) {
338 qCWarning(QGCMapPolygonLog) <<
"Call to removePolygonCoordinate with bad vertexIndex:count" << vertexIndex << _polygonPath.length();
342 if (_polygonPath.length() <= 3) {
347 QObject* coordObj = _polygonModel.removeAt(vertexIndex);
348 coordObj->deleteLater();
349 if(vertexIndex == _selectedVertexIndex) {
351 }
else if (vertexIndex < _selectedVertexIndex) {
355 _polygonPath.removeAt(vertexIndex);
359void QGCMapPolygon::_polygonModelCountChanged(
int count)
364void QGCMapPolygon::_updateCenter(
void)
366 if (!_ignoreCenterUpdates) {
369 if (_polygonPath.count() > 2) {
370 QPolygonF polygonF = _toPolygonF();
371 const int n = polygonF.count();
374 double signedArea = 0;
378 for (
int i = 0; i < n; i++) {
379 const int j = (i + 1) % n;
380 const double cross = polygonF[i].x() * polygonF[j].y() - polygonF[j].x() * polygonF[i].y();
382 cx += (polygonF[i].x() + polygonF[j].x()) * cross;
383 cy += (polygonF[i].y() + polygonF[j].y()) * cross;
386 if (qAbs(signedArea) < 1e-6) {
389 for (
int i = 0; i < n; i++) {
392 center = _coordFromPointF(QPointF(avg.x() / n, avg.y() / n));
395 cx /= (6.0 * signedArea);
396 cy /= (6.0 * signedArea);
397 center = _coordFromPointF(QPointF(cx, cy));
409 if (newCenter != _center) {
410 _ignoreCenterUpdates =
true;
413 double distance = _center.distanceTo(newCenter);
414 double azimuth = _center.azimuthTo(newCenter);
416 for (
int i=0; i<
count(); i++) {
417 QGeoCoordinate oldVertex = _polygonPath[i].value<QGeoCoordinate>();
418 QGeoCoordinate newVertex = oldVertex.atDistanceAndAzimuth(distance, azimuth);
422 _ignoreCenterUpdates =
false;
427 if (!_deferredPathChanged) {
428 _deferredPathChanged =
true;
429 QTimer::singleShot(0,
this, [
this]() {
432 _deferredPathChanged =
false;
436 if (!_deferredPathChanged) {
437 _deferredPathChanged =
true;
438 QTimer::singleShot(0,
this, [
this]() {
441 _deferredPathChanged =
false;
486 if (vertex >= 0 && vertex < _polygonPath.count()) {
487 return _polygonPath[vertex].value<QGeoCoordinate>();
489 qCWarning(QGCMapPolygonLog) <<
"QGCMapPolygon::vertexCoordinate bad vertex requested:count" << vertex << _polygonPath.count();
490 return QGeoCoordinate();
501 for (
int i=0; i<_polygonModel.count(); i++) {
523 const QList<QPointF> nedVertices =
nedPolygon();
525 if (offsetPolygons.size() != 1) {
526 qCWarning(QGCMapPolygonLog) <<
"Polygon offset by" << distance <<
"produced" << offsetPolygons.size()
531 QList<QGeoCoordinate> newPolygon;
533 newPolygon.reserve(offsetPolygons.first().size());
534 for (
const QPointF& vertex : offsetPolygons.first()) {
535 QGeoCoordinate coordinate;
537 newPolygon.append(coordinate);
552 QList<QList<QGeoCoordinate>> polygons;
557 if (polygons.isEmpty()) {
561 const QList<QGeoCoordinate>& rgCoords = polygons.first();
575 if (_polygonPath.count() < 3) {
579 double coveredArea = 0.0;
581 for (
int i=0; i<nedVertices.count(); i++) {
583 coveredArea += nedVertices[i - 1].x() * nedVertices[i].y() - nedVertices[i].x() * nedVertices[i -1].y();
585 coveredArea += nedVertices.last().x() * nedVertices[i].y() - nedVertices[i].x() * nedVertices.last().y();
588 return 0.5 * fabs(coveredArea);
594 for (
int i=0; i<_polygonPath.count(); i++) {
595 QGeoCoordinate coord1 = _polygonPath[i].value<QGeoCoordinate>();
596 QGeoCoordinate coord2 = (i == _polygonPath.count() - 1) ? _polygonPath[0].value<QGeoCoordinate>() : _polygonPath[i+1].value<QGeoCoordinate>();
598 sum += (coord2.longitude() - coord1.longitude()) * (coord2.latitude() + coord1.latitude());
605 if (_polygonPath.count() <= 2) {
612 QList<QGeoCoordinate> rgReversed;
613 for (
const QVariant& varCoord: _polygonPath) {
614 rgReversed.prepend(varCoord.value<QGeoCoordinate>());
626 _polygonModel.beginResetModel();
631 _polygonModel.endResetModel();
638 <!-- specific to Polygon -->
639 <extrude>0</extrude> <!--
boolean -->
640 <tessellate>0</tessellate> <!--
boolean -->
641 <altitudeMode>clampToGround</altitudeMode>
642 <!-- kml:altitudeModeEnum: clampToGround, relativeToGround, or absolute -->
643 <!-- or, substitute gx:altitudeMode: clampToSeaFloor, relativeToSeaFloor -->
646 <coordinates>...</coordinates> <!-- lon,lat[,alt] -->
651 <coordinates>...</coordinates> <!-- lon,lat[,alt] -->
657 QDomElement polygonElement = domDocument.createElement(
"Polygon");
659 domDocument.
addTextElement(polygonElement,
"altitudeMode",
"clampToGround");
661 QDomElement outerBoundaryIsElement = domDocument.createElement(
"outerBoundaryIs");
662 QDomElement linearRingElement = domDocument.createElement(
"LinearRing");
664 outerBoundaryIsElement.appendChild(linearRingElement);
665 polygonElement.appendChild(outerBoundaryIsElement);
668 for (
const QVariant& varCoord : _polygonPath) {
669 coordString += QStringLiteral(
"%1\n").arg(domDocument.
kmlCoordString(varCoord.value<QGeoCoordinate>()));
671 coordString += QStringLiteral(
"%1\n").arg(domDocument.
kmlCoordString(_polygonPath.first().value<QGeoCoordinate>()));
672 domDocument.
addTextElement(linearRingElement,
"coordinates", coordString);
674 return polygonElement;
694 if(index == _selectedVertexIndex)
return;
696 if(-1 <= index && index <
count()) {
697 _selectedVertexIndex = index;
699 qCWarning(QGCMapPolygonLog) << QString(
"QGCMapPolygon: Selected vertex index (%1) is out of bounds! "
700 "Polygon vertices indexes range is [%2..%3].").arg(index).arg(0).arg(
count()-1);
701 _selectedVertexIndex = -1;
Geographic coordinate conversion utilities using GeographicLib.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Used to convert a Plan to a KML document.
static QString kmlCoordString(const QGeoCoordinate &coord)
void addTextElement(QDomElement &parentElement, const QString &name, const QString &value)
void dirtyChanged(bool dirty)
void countChanged(int count)
void interactiveChanged(bool interactive)
~QGCMapPolygon() override
void dirtyChanged(bool dirty)
void selectVertex(int index)
Q_INVOKABLE bool loadKMLOrSHPFile(const QString &file)
bool isEmptyChanged(void)
Q_INVOKABLE bool offset(double distance)
Q_INVOKABLE void adjustVertex(int vertexIndex, const QGeoCoordinate coordinate)
double area(void) const
Returns the area of the polygon in meters squared.
void setCenter(QGeoCoordinate newCenter)
Q_INVOKABLE void splitPolygonSegment(int vertexIndex)
Splits the segment comprised of vertextIndex -> vertexIndex + 1.
bool traceMode(void) const
void vertexDragChanged(bool vertexDrag)
QGeoCoordinate center(void) const
void setTraceMode(bool traceMode)
QList< QGeoCoordinate > coordinateList(void) const
Returns the path in a list of QGeoCoordinate's format.
void setVertexDrag(bool vertexDrag)
void dragPathChanged(void)
void setDirty(bool dirty)
bool centerDrag(void) const
void selectedVertexChanged(int index)
void traceModeChanged(bool traceMode)
bool showAltColor(void) const
Q_INVOKABLE bool containsCoordinate(const QGeoCoordinate &coordinate) const
Returns true if the specified coordinate is within the polygon.
Q_INVOKABLE QGeoCoordinate vertexCoordinate(int vertex) const
Returns the QGeoCoordinate for the vertex specified.
Q_INVOKABLE void removeVertex(int vertexIndex)
void setPath(const QList< QGeoCoordinate > &path)
QVariantList path(void) const
void showAltColorChanged(bool showAltColor)
void dragCenterChanged(QGeoCoordinate center)
const QGCMapPolygon & operator=(const QGCMapPolygon &other)
Q_INVOKABLE void verifyClockwiseWinding(void)
Adjust polygon winding order to be clockwise (if needed).
void setInteractive(bool interactive)
Q_INVOKABLE void beginReset(void)
bool interactive(void) const
void saveToJson(QJsonObject &json)
QList< QPointF > nedPolygon(void) const
Convert polygon to NED and return (D is ignored).
Q_INVOKABLE void endReset(void)
QDomElement kmlPolygonElement(KMLDomDocument &domDocument)
static constexpr const char * jsonPolygonKey
void centerChanged(QGeoCoordinate center)
Q_INVOKABLE void clear(void)
bool isValidChanged(void)
void setShowAltColor(bool showAltColor)
Q_INVOKABLE void appendVertex(const QGeoCoordinate &coordinate)
bool vertexDrag(void) const
void countChanged(int count)
bool loadFromJson(const QJsonObject &json, bool required, QString &errorString)
Q_INVOKABLE void appendVertices(const QVariantList &varCoords)
void setCenterDrag(bool centerDrag)
QGCMapPolygon(QObject *parent=nullptr)
void centerDragChanged(bool centerDrag)
This is a QGeoCoordinate within a QObject such that it can be used on a QmlObjectListModel.
static bool loadPolygonsFromFile(const QString &file, QList< QList< QGeoCoordinate > > &polygons, QString &errorString, double filterMeters=kDefaultVertexFilterMeters)
bool loadGeoCoordinateArray(const QJsonValue &jsonValue, bool altitudeRequired, QVariantList &rgVarPoints, QString &errorString)
Loads a list of QGeoCoordinates (QGC plan format) from a json array.
void saveGeoCoordinateArray(const QVariantList &rgVarPoints, bool writeAltitude, QJsonValue &jsonValue)
Saves a list of QGeoCoordinates (QGC plan format) to a json array.
bool validateRequiredKeys(const QJsonObject &jsonObject, const QStringList &keys, QString &errorString)
Validates that all listed keys are present in the object.
void convertGeoToNed(const QGeoCoordinate &coord, const QGeoCoordinate &origin, double &x, double &y, double &z)
void convertNedToGeo(double x, double y, double z, const QGeoCoordinate &origin, QGeoCoordinate &coord)
QList< QPolygonF > offsetPolygon(const QPolygonF &polygon, double distance)
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.