GeoArea.cc 3.27 KB
Newer Older
1 2
#include "GeoArea.h"

3
#include "geometry.h"
4 5 6 7

#include "QGCLoggingCategory.h"
#include "QGCQGeoCoordinate.h"

8 9
#include <QDebug>

10 11
QGC_LOGGING_CATEGORY(GeoAreaLog, "GeoAreaLog")

12
const char *GeoArea::nameString = "GeoArea";
13
const char *GeoArea::areaTypeKey = "AreaType";
14 15 16 17 18 19
const char *GeoArea::settingsGroup = "GeoArea";

// Constructors
GeoArea::GeoArea(QObject *parent) : QGCMapPolygon(parent) { init(); }

GeoArea::GeoArea(const GeoArea &other, QObject *parent)
20
    : QGCMapPolygon(other, parent) {
21
  init();
22
  _errorString = other._errorString;
23 24 25 26 27
}

GeoArea &GeoArea::operator=(const GeoArea &other) {
  QGCMapPolygon::operator=(other);

28 29
  _errorString = other._errorString;

30 31 32
  return *this;
}

33
bool GeoArea::saveToJson(QJsonObject &json) {
34
  this->QGCMapPolygon::saveToJson(json);
35
  return true;
36 37 38 39 40 41 42
}

bool GeoArea::loadFromJson(const QJsonObject &json, QString &errorString) {
  if (!this->QGCMapPolygon::loadFromJson(json, false /*no poly required*/,
                                         errorString)) {
    qWarning() << errorString;
    return false;
43 44 45 46 47 48 49 50
  } else {

    // set altitude to 0
    for (int i = 0; i < this->count(); ++i) {
      auto vertex = this->vertexCoordinate(i);
      vertex.setAltitude(0);
      this->adjustVertex(i, vertex);
    }
51 52 53 54
  }
  return true;
}

55 56 57
bool GeoArea::isCorrect() {
  if (this->pathModel().count() >= 3) {
    auto origin = this->pathModel().value<QGCQGeoCoordinate *>(0)->coordinate();
58 59
    geometry::FPolygon polygonENU;
    geometry::areaToEnu(origin, this->pathModel(), polygonENU);
60 61 62 63
    std::string msg;
    if (bg::is_valid(polygonENU, msg)) {
      return true;
    } else {
64 65 66
      qCWarning(GeoAreaLog) << "isCorrect(): " << msg.c_str();
      qCWarning(GeoAreaLog) << "isCorrect(): "
                            << "origin: " << origin;
67 68
      std::stringstream ss;
      ss << bg::wkt(polygonENU);
69 70
      qCWarning(GeoAreaLog) << "isCorrect(): "
                            << "polygonENU: " << ss.str().c_str();
71
      setErrorString(this->objectName() + tr(" must be a simple polygon."));
72 73
    }
  }
74 75 76
  return false;
}

77 78
QString GeoArea::errorString() const { return this->_errorString; }

79 80 81
bool GeoArea::covers(const QGeoCoordinate &c) {
  if (GeoArea::isCorrect()) {
    auto origin = this->pathModel().value<QGCQGeoCoordinate *>(0)->coordinate();
82 83 84 85
    geometry::FPolygon polygonENU;
    geometry::areaToEnu(origin, this->pathModel(), polygonENU);
    geometry::FPoint cENU;
    geometry::toENU(origin, c, cENU);
86 87 88 89 90 91
    return bg::covered_by(cENU, polygonENU);
  } else {
    return false;
  }
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
92
void GeoArea::init() { this->setObjectName(nameString); }
93

94 95 96 97 98
void GeoArea::setErrorString(const QString &str) {
  this->_errorString = str;
  emit errorStringChanged();
}

99
void GeoArea::setErrorString(const std::string &str) {
100 101 102 103
  this->_errorString = str.c_str();
  emit errorStringChanged();
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
bool copyAreaList(const QmlObjectListModel &from, QmlObjectListModel &to,
                  QObject *parent) {
  // Check if elements are valid.
  for (int i = 0; i < from.count(); ++i) {
    auto obj = from[i];
    auto area = qobject_cast<const GeoArea *>(obj);
    if (area == nullptr) {
      return false;
    }
  }

  // Clone elements.
  for (int i = 0; i < from.count(); ++i) {
    auto obj = from[i];
    auto area = qobject_cast<const GeoArea *>(obj);
    to.append(area->clone(parent));
  }

  return true;
}