AirMapRestrictionManager.cc 5.51 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10
#include "AirMapRestrictionManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
Gus Grubba's avatar
Gus Grubba committed
12 13 14 15
#include "AirspaceRestriction.h"

#include "airmap/airspaces.h"

16 17
#define RESTRICTION_UPDATE_DISTANCE    500     //-- 500m threshold for updates

Gus Grubba's avatar
Gus Grubba committed
18
using namespace airmap;
19

20
//-----------------------------------------------------------------------------
21 22 23 24 25
AirMapRestrictionManager::AirMapRestrictionManager(AirMapSharedState& shared)
    : _shared(shared)
{
}

26
//-----------------------------------------------------------------------------
27
void
28
AirMapRestrictionManager::setROI(const QGCGeoBoundingCube& roi, bool reset)
29
{
30
    //-- If first time or we've moved more than RESTRICTION_UPDATE_DISTANCE, ask for updates.
31
    if(reset || (!_lastROI.isValid() || _lastROI.pointNW.distanceTo(roi.pointNW) > RESTRICTION_UPDATE_DISTANCE || _lastROI.pointSE.distanceTo(roi.pointSE) > RESTRICTION_UPDATE_DISTANCE)) {
32 33 34 35 36
        //-- No more than 40000 km^2
        if(roi.area() < 40000.0) {
            _lastROI = roi;
            _requestRestrictions(roi);
        }
37 38 39 40 41
    }
}

//-----------------------------------------------------------------------------
void
42
AirMapRestrictionManager::_requestRestrictions(const QGCGeoBoundingCube& roi)
43 44 45 46 47 48 49 50 51
{
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Airspace";
        return;
    }
    if (_state != State::Idle) {
        qCWarning(AirMapManagerLog) << "AirMapRestrictionManager::updateROI: state not idle";
        return;
    }
52
    qCDebug(AirMapManagerLog) << "Restrictions Request (ROI Changed)";
53 54
    _polygons.clear();
    _circles.clear();
55 56 57
    _state = State::RetrieveItems;
    Airspaces::Search::Parameters params;
    params.full = true;
58 59 60 61 62 63 64 65 66 67
    params.date_time = Clock::universal_time();
    //-- Geometry: Polygon
    Geometry::Polygon polygon;
    for (const auto& qcoord : roi.polygon2D()) {
        Geometry::Coordinate coord;
        coord.latitude  = qcoord.latitude();
        coord.longitude = qcoord.longitude();
        polygon.outer_ring.coordinates.push_back(coord);
    }
    params.geometry = Geometry(polygon);
68 69 70 71 72 73 74 75 76 77 78 79 80
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->airspaces().search(params,
            [this, isAlive](const Airspaces::Search::Result& result) {
        if (!isAlive.lock()) return;
        if (_state != State::RetrieveItems) return;
        if (result) {
            const std::vector<Airspace>& airspaces = result.value();
            qCDebug(AirMapManagerLog)<<"Successful search. Items:" << airspaces.size();
            for (const auto& airspace : airspaces) {
                const Geometry& geometry = airspace.geometry();
                switch(geometry.type()) {
                    case Geometry::Type::polygon: {
                        const Geometry::Polygon& polygon = geometry.details_for_polygon();
81
                        _addPolygonToList(polygon);
82 83 84 85 86
                    }
                        break;
                    case Geometry::Type::multi_polygon: {
                        const Geometry::MultiPolygon& multiPolygon = geometry.details_for_multi_polygon();
                        for (const auto& polygon : multiPolygon) {
87
                            _addPolygonToList(polygon);
88 89 90 91 92
                        }
                    }
                        break;
                    case Geometry::Type::point: {
                        const Geometry::Point& point = geometry.details_for_point();
93
                        _circles.append(new AirspaceCircularRestriction(QGeoCoordinate(point.latitude, point.longitude), 0.));
94 95 96
                        // TODO: radius???
                    }
                        break;
97 98 99 100
                    case Geometry::Type::invalid: {
                        qWarning() << "Invalid geometry type";
                    }
                        break;
101
                    default:
102
                        qWarning() << "Unsupported geometry type: " << static_cast<int>(geometry.type());
103 104 105 106 107 108 109 110 111 112 113 114
                        break;
                }
            }
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
            emit error("Failed to retrieve Geofences",
                    QString::fromStdString(result.error().message()), description);
        }
        _state = State::Idle;
    });
}

115
//-----------------------------------------------------------------------------
116
void
117
AirMapRestrictionManager::_addPolygonToList(const airmap::Geometry::Polygon& polygon)
118 119 120 121 122 123 124 125 126 127 128
{
    QVariantList polygonArray;
    for (const auto& vertex : polygon.outer_ring.coordinates) {
        QGeoCoordinate coord;
        if (vertex.altitude) {
            coord = QGeoCoordinate(vertex.latitude, vertex.longitude, vertex.altitude.get());
        } else {
            coord = QGeoCoordinate(vertex.latitude, vertex.longitude);
        }
        polygonArray.append(QVariant::fromValue(coord));
    }
129
    _polygons.append(new AirspacePolygonRestriction(polygonArray));
130 131 132 133 134
    if (polygon.inner_rings.size() > 0) {
        // no need to support those (they are rare, and in most cases, there's a more restrictive polygon filling the hole)
        qCDebug(AirMapManagerLog) << "Polygon with holes. Size: "<<polygon.inner_rings.size();
    }
}