AirMapRestrictionManager.cc 10.2 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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
#include "AirspaceRestriction.h"

14 15 16
#include "QGCApplication.h"
#include "SettingsManager.h"

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

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

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

27
//-----------------------------------------------------------------------------
28
void
29
AirMapRestrictionManager::setROI(const QGCGeoBoundingCube& roi, bool reset)
30
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    if(qgcApp()->toolbox()->settingsManager()->airMapSettings()->enableAirspace()->rawValue().toBool()) {
        //-- If first time or we've moved more than RESTRICTION_UPDATE_DISTANCE, ask for updates.
        if(reset ||
          (!_lastROI.isValid() || _lastROI.pointNW.distanceTo(roi.pointNW) > RESTRICTION_UPDATE_DISTANCE || _lastROI.pointSE.distanceTo(roi.pointSE) > RESTRICTION_UPDATE_DISTANCE) ||
           (_polygons.count() == 0 && _circles.count() == 0)) {
            //-- Limit area of interest
            qCDebug(AirMapManagerLog) << "ROI Area:" << roi.area() << "km^2";
            if(roi.area() < qgcApp()->toolbox()->airspaceManager()->maxAreaOfInterest()) {
                _lastROI = roi;
                _requestRestrictions(roi);
            } else {
                _polygons.clear();
                _circles.clear();
            }
45
        }
46 47 48
    }
}

49 50

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
51 52
void
AirMapRestrictionManager::_getColor(const Airspace& airspace, QColor& color, QColor& lineColor, float& lineWidth)
53
{
Gus Grubba's avatar
Gus Grubba committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    if(airspace.type() == Airspace::Type::airport) {
        color       = QColor(246,165,23,50);
        lineColor   = QColor(246,165,23,255);
        lineWidth   = 2.0f;
    } else if(airspace.type() == Airspace::Type::controlled_airspace) {
        QString classification = QString::fromStdString(airspace.details_for_controlled_airspace().airspace_classification).toUpper();
        if(classification == "B") {
            color       = QColor(31,160,211,25);
            lineColor   = QColor(31,160,211,255);
            lineWidth   = 1.5f;
        } else if(classification == "C") {
            color       = QColor(155,108,157,25);
            lineColor   = QColor(155,108,157,255);
            lineWidth   = 1.5f;
        } else if(classification == "D") {
            color       = QColor(26,116,179,25);
            lineColor   = QColor(26,116,179,255);
            lineWidth   = 1.0f;
        } else if(classification == "E") {
            color       = QColor(155,108,157,25);
            lineColor   = QColor(155,108,157,255);
            lineWidth   = 1.0f;
        } else {
            //-- Don't know it
            qCWarning(AirMapManagerLog) << "Unknown airspace classification:" << QString::fromStdString(airspace.details_for_controlled_airspace().airspace_classification);
            color       = QColor(255,230,0,25);
            lineColor   = QColor(255,230,0,255);
            lineWidth   = 0.5f;
        }
    } else if(airspace.type() == Airspace::Type::special_use_airspace) {
        color       = QColor(27,90,207,38);
        lineColor   = QColor(27,90,207,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::tfr) {
        color       = QColor(244,67,54,38);
        lineColor   = QColor(244,67,54,255);
        lineWidth   = 2.0f;
    } else if(airspace.type() == Airspace::Type::wildfire) {
        color       = QColor(244,67,54,50);
        lineColor   = QColor(244,67,54,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::park) {
        color       = QColor(224,18,18,25);
        lineColor   = QColor(224,18,18,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::power_plant) {
        color       = QColor(246,165,23,25);
        lineColor   = QColor(246,165,23,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::heliport) {
        color       = QColor(246,165,23,20);
        lineColor   = QColor(246,165,23,100);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::prison) {
        color       = QColor(246,165,23,38);
        lineColor   = QColor(246,165,23,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::school) {
        color       = QColor(246,165,23,38);
        lineColor   = QColor(246,165,23,255);
        lineWidth   = 1.0f;
     } else if(airspace.type() == Airspace::Type::hospital) {
        color       = QColor(246,165,23,38);
        lineColor   = QColor(246,165,23,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::fire) {
        color       = QColor(244,67,54,50);
        lineColor   = QColor(244,67,54,255);
        lineWidth   = 1.0f;
    } else if(airspace.type() == Airspace::Type::emergency) {
        color       = QColor(246,113,23,77);
        lineColor   = QColor(246,113,23,255);
        lineWidth   = 1.0f;
    } else {
        //-- Don't know it
        qCWarning(AirMapManagerLog) << "Unknown airspace type:" << static_cast<int>(airspace.type());
        color       = QColor(255,0,230,25);
        lineColor   = QColor(255,0,230,255);
        lineWidth   = 0.5f;
    }
134 135
}

136 137
//-----------------------------------------------------------------------------
void
138
AirMapRestrictionManager::_requestRestrictions(const QGCGeoBoundingCube& roi)
139 140 141 142 143 144 145 146 147
{
    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;
    }
148
    qCDebug(AirMapManagerLog) << "Restrictions Request (ROI Changed)";
149 150
    _polygons.clear();
    _circles.clear();
151 152
    _state = State::RetrieveItems;
    Airspaces::Search::Parameters params;
153
    params.full = false;
154 155 156 157 158 159 160 161 162 163
    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);
164 165 166 167 168 169 170 171 172
    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) {
Gus Grubba's avatar
Gus Grubba committed
173 174 175 176
                QColor color;
                QColor lineColor;
                float  lineWidth;
                _getColor(airspace, color, lineColor, lineWidth);
177 178 179 180
                const Geometry& geometry = airspace.geometry();
                switch(geometry.type()) {
                    case Geometry::Type::polygon: {
                        const Geometry::Polygon& polygon = geometry.details_for_polygon();
Gus Grubba's avatar
Gus Grubba committed
181
                        _addPolygonToList(polygon, QString::fromStdString(airspace.id()), color, lineColor, lineWidth);
182 183 184 185 186
                    }
                        break;
                    case Geometry::Type::multi_polygon: {
                        const Geometry::MultiPolygon& multiPolygon = geometry.details_for_multi_polygon();
                        for (const auto& polygon : multiPolygon) {
Gus Grubba's avatar
Gus Grubba committed
187
                            _addPolygonToList(polygon, QString::fromStdString(airspace.id()), color, lineColor, lineWidth);
188 189 190 191 192
                        }
                    }
                        break;
                    case Geometry::Type::point: {
                        const Geometry::Point& point = geometry.details_for_point();
Gus Grubba's avatar
Gus Grubba committed
193
                        _circles.append(new AirspaceCircularRestriction(QGeoCoordinate(point.latitude, point.longitude), 0., QString::fromStdString(airspace.id()), color, lineColor, lineWidth));
194 195 196
                        // TODO: radius???
                    }
                        break;
197 198 199 200
                    case Geometry::Type::invalid: {
                        qWarning() << "Invalid geometry type";
                    }
                        break;
201
                    default:
202
                        qWarning() << "Unsupported geometry type: " << static_cast<int>(geometry.type());
203 204 205 206 207 208 209 210 211 212 213 214
                        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;
    });
}

215
//-----------------------------------------------------------------------------
216
void
Gus Grubba's avatar
Gus Grubba committed
217
AirMapRestrictionManager::_addPolygonToList(const airmap::Geometry::Polygon& polygon, const QString advisoryID, const QColor color, const QColor lineColor, float lineWidth)
218 219 220 221 222 223 224 225 226 227 228
{
    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));
    }
Gus Grubba's avatar
Gus Grubba committed
229
    _polygons.append(new AirspacePolygonRestriction(polygonArray, advisoryID, color, lineColor, lineWidth));
230 231 232 233 234
    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();
    }
}