AirMapRestrictionManager.cc 5.01 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
AirMapRestrictionManager::AirMapRestrictionManager(AirMapSharedState& shared)
    : _shared(shared)
23
    , _lastRadius(0.0)
24 25 26
{
}

27
//-----------------------------------------------------------------------------
28 29
void
AirMapRestrictionManager::setROI(const QGeoCoordinate& center, double radiusMeters)
30 31 32 33 34 35 36 37 38 39 40 41
{
    //-- If first time or we've moved more than ADVISORY_UPDATE_DISTANCE, ask for updates.
    if(!_lastRoiCenter.isValid() || _lastRoiCenter.distanceTo(center) > RESTRICTION_UPDATE_DISTANCE || _lastRadius != radiusMeters) {
        _lastRadius    = radiusMeters;
        _lastRoiCenter = center;
        _requestRestrictions(center, radiusMeters);
    }
}

//-----------------------------------------------------------------------------
void
AirMapRestrictionManager::_requestRestrictions(const QGeoCoordinate& center, double radiusMeters)
42 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;
    }
    qCDebug(AirMapManagerLog) << "setting ROI";
52 53
    _polygons.clear();
    _circles.clear();
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    _state = State::RetrieveItems;
    Airspaces::Search::Parameters params;
    params.geometry = Geometry::point(center.latitude(), center.longitude());
    params.buffer = radiusMeters;
    params.full = true;
    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();
72
                        _addPolygonToList(polygon);
73 74 75 76 77
                    }
                        break;
                    case Geometry::Type::multi_polygon: {
                        const Geometry::MultiPolygon& multiPolygon = geometry.details_for_multi_polygon();
                        for (const auto& polygon : multiPolygon) {
78
                            _addPolygonToList(polygon);
79 80 81 82 83
                        }
                    }
                        break;
                    case Geometry::Type::point: {
                        const Geometry::Point& point = geometry.details_for_point();
84
                        _circles.append(new AirspaceCircularRestriction(QGeoCoordinate(point.latitude, point.longitude), 0.));
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                        // TODO: radius???
                    }
                        break;
                    default:
                        qWarning() << "unsupported geometry type: "<<(int)geometry.type();
                        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;
    });
}

102
//-----------------------------------------------------------------------------
103
void
104
AirMapRestrictionManager::_addPolygonToList(const airmap::Geometry::Polygon& polygon)
105 106 107 108 109 110 111 112 113 114 115
{
    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));
    }
116
    _polygons.append(new AirspacePolygonRestriction(polygonArray));
117 118 119 120 121
    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();
    }
}