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

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

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

8 9
#include <QDebug>

10 11
QGC_LOGGING_CATEGORY(GeoAreaLog, "GeoAreaLog")

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

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

GeoArea::GeoArea(const GeoArea &other, QObject *parent)
20
    : QGCMapPolygon(other, parent), _errorString(other._errorString) {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  init();
}

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

  return *this;
}

void GeoArea::saveToJson(QJsonObject &json) {
  this->QGCMapPolygon::saveToJson(json);
}

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

  return true;
}

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

64 65
QString GeoArea::errorString() const { return this->_errorString; }

66 67
void GeoArea::init() { this->setObjectName(wimaAreaName); }

68 69 70 71 72 73 74 75 76 77
void GeoArea::setErrorString(const QString &str) {
  this->_errorString = str;
  emit errorStringChanged();
}

void GeoArea::setErrorString(const string &str) {
  this->_errorString = str.c_str();
  emit errorStringChanged();
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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;
}