GeoArea.cc 3.08 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 43 44 45 46 47
}

bool GeoArea::loadFromJson(const QJsonObject &json, QString &errorString) {
  if (!this->QGCMapPolygon::loadFromJson(json, false /*no poly required*/,
                                         errorString)) {
    qWarning() << errorString;
    return false;
  }

  return true;
}

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

70 71
QString GeoArea::errorString() const { return this->_errorString; }

72 73 74
bool GeoArea::covers(const QGeoCoordinate &c) {
  if (GeoArea::isCorrect()) {
    auto origin = this->pathModel().value<QGCQGeoCoordinate *>(0)->coordinate();
75 76 77 78
    geometry::FPolygon polygonENU;
    geometry::areaToEnu(origin, this->pathModel(), polygonENU);
    geometry::FPoint cENU;
    geometry::toENU(origin, c, cENU);
79 80 81 82 83 84
    return bg::covered_by(cENU, polygonENU);
  } else {
    return false;
  }
}

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

87 88 89 90 91
void GeoArea::setErrorString(const QString &str) {
  this->_errorString = str;
  emit errorStringChanged();
}

92
void GeoArea::setErrorString(const std::string &str) {
93 94 95 96
  this->_errorString = str.c_str();
  emit errorStringChanged();
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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;
}