GeoArea.cc 3.34 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
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 51 52 53 54 55 56 57 58 59 60 61
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();
62
      setErrorString(this->objectName() + tr(" must be a simple polygon."));
63 64
    }
  }
65 66 67
  return false;
}

68 69
QString GeoArea::errorString() const { return this->_errorString; }

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

void GeoArea::init() {
84
  this->setObjectName(nameString);
85 86 87 88 89 90 91 92 93 94 95 96
  //  connect(this, &GeoArea::pathChanged, [this] {
  //    if (this->objectName() != "Tile") {
  //      qDebug() << this->objectName() << " path: " << this->path() << "\n";
  //    }
  //  });
  //  connect(this, &GeoArea::centerChanged, [this] {
  //    if (this->objectName() != "Tile") {
  //      qDebug() << this->objectName() << " center: " << this->center() <<
  //      "\n";
  //    }
  //  });
}
97

98 99 100 101 102 103 104 105 106 107
void GeoArea::setErrorString(const QString &str) {
  this->_errorString = str;
  emit errorStringChanged();
}

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

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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;
}