MapFitFunctions.qml 6.65 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 11
import QtQuick          2.3
import QtPositioning    5.3
12

13 14
import QGroundControl           1.0
import QGroundControl.FlightMap 1.0
15 16 17 18 19

/// Set of functions for fitting the map viewpoer to a specific constraint
Item {
    property var    map
    property bool   usePlannedHomePosition      ///< true: planned home position used for calculations, false: vehicle home position use for calculations
Gus Grubba's avatar
Gus Grubba committed
20
    property var    planMasterController
21 22 23 24

    property var    _missionController:     planMasterController.missionController
    property var    _geoFenceController:    planMasterController.geoFenceController
    property var    _rallyPointController:  planMasterController.rallyPointController
25 26 27 28 29

    function fitHomePosition() {
        var homePosition = QtPositioning.coordinate()
        var activeVehicle = QGroundControl.multiVehicleManager.activeVehicle
        if (usePlannedHomePosition) {
30
            homePosition = _missionController.visualItems.get(0).coordinate
31 32 33 34 35 36 37 38 39 40 41 42 43
        } else if (activeVehicle) {
            homePosition = activeVehicle.homePosition
        }
        return homePosition
    }

    /// Normalize latitude to range: 0 to 180, S to N
    function normalizeLat(lat) {
        return lat + 90.0
    }

    /// Normalize longitude to range: 0 to 360, W to E
    function normalizeLon(lon) {
DoinLakeFlyer's avatar
DoinLakeFlyer committed
44
        return lon + 180.0
45 46 47 48 49
    }

    /// Fits the visible region of the map to inclues all of the specified coordinates. If no coordinates
    /// are specified the map will center to fitHomePosition()
    function fitMapViewportToAllCoordinates(coordList) {
50
        var mapFitViewport = Qt.rect(0, 0, map.width, map.height)
51
        if (coordList.length === 0) {
Don Gagne's avatar
Don Gagne committed
52 53 54 55
            var homeCoord = fitHomePosition()
            if (homeCoord.isValid) {
                map.center = homeCoord
            }
56 57 58 59 60 61 62 63
            return
        }

        // Create the normalized lat/lon corners for the coordinate bounding rect from the list of coordinates
        var north = normalizeLat(coordList[0].latitude)
        var south = north
        var east = normalizeLon(coordList[0].longitude)
        var west = east
64
        for (var i = 1; i < coordList.length; i++) {
DoinLakeFlyer's avatar
DoinLakeFlyer committed
65 66
            var lat = coordList[i].latitude
            var lon = coordList[i].longitude
DoinLakeFlyer's avatar
DoinLakeFlyer committed
67
            if (isNaN(lat) || lat == 0 || isNaN(lon) || lon == 0) {
DoinLakeFlyer's avatar
DoinLakeFlyer committed
68 69 70 71
                // Be careful of invalid coords which can happen when items are not yet complete
                continue
            }
            lat = normalizeLat(lat)
DoinLakeFlyer's avatar
DoinLakeFlyer committed
72
            lon = normalizeLon(lon)
73 74
            north = Math.max(north, lat)
            south = Math.min(south, lat)
75 76
            east  = Math.max(east,  lon)
            west  = Math.min(west,  lon)
77 78 79
        }

        // Expand the coordinate bounding rect to make room for the tools around the edge of the map
DoinLakeFlyer's avatar
DoinLakeFlyer committed
80 81
        var latDegreesPerPixel = (north - south) / mapFitViewport.height
        var lonDegreesPerPixel = (east  - west)  / mapFitViewport.width
82 83
        north = Math.min(north + (mapFitViewport.y * latDegreesPerPixel), 180)
        south = Math.max(south - ((map.height - mapFitViewport.bottom) * latDegreesPerPixel), 0)
84 85 86 87 88 89 90 91
        west  = Math.max(west  - (mapFitViewport.x * lonDegreesPerPixel), 0)
        east  = Math.min(east  + ((map.width - mapFitViewport.right) * lonDegreesPerPixel), 360)

        // Back off on zoom level
        east  = Math.min(east  * 1.0000075, 360)
        north = Math.min(north * 1.0000075, 180)
        west  = west  * 0.9999925
        south = south * 0.9999925
92

Gus Grubba's avatar
Gus Grubba committed
93
        // Fit the map region to the new bounding rect
94
        var topLeftCoord      = QtPositioning.coordinate(north - 90.0, west - 180.0)
95 96 97 98 99
        var bottomRightCoord  = QtPositioning.coordinate(south - 90.0, east - 180.0)
        map.setVisibleRegion(QtPositioning.rectangle(topLeftCoord, bottomRightCoord))
    }

    function addMissionItemCoordsForFit(coordList) {
100
        for (var i = 1; i < _missionController.visualItems.count; i++) {
101
            var missionItem = _missionController.visualItems.get(i)
102
            if (missionItem.specifiesCoordinate && !missionItem.isStandaloneCoordinate) {
103 104 105 106 107 108
                if(missionItem.boundingCube.isValid()) {
                    coordList.push(missionItem.boundingCube.pointNW)
                    coordList.push(missionItem.boundingCube.pointSE)
                } else {
                    coordList.push(missionItem.coordinate)
                }
109 110 111 112 113
            }
        }
    }

    function fitMapViewportToMissionItems() {
114
        if (!_missionController.visualItems) {
115 116 117
            // Being called prior to controller.start
            return
        }
118 119 120 121 122 123
        var coordList = [ ]
        addMissionItemCoordsForFit(coordList)
        fitMapViewportToAllCoordinates(coordList)
    }

    function addFenceItemCoordsForFit(coordList) {
124
        var i
125
        var homePosition = fitHomePosition()
126
        if (homePosition.isValid && _geoFenceController.circleEnabled) {
127
            var azimuthList = [ 0, 180, 90, 270 ]
128
            for (i = 0; i < azimuthList.length; i++) {
129
                var edgeCoordinate = homePosition.atDistanceAndAzimuth(_geoFenceController.circleRadius, azimuthList[i])
130 131 132
                coordList.push(edgeCoordinate)
            }
        }
133
        if (_geoFenceController.polygonEnabled && _geoFenceController.mapPolygon.path.count > 2) {
134
            for (i = 0; i < _geoFenceController.mapPolygon.path.count; i++) {
135
                coordList.push(_geoFenceController.mapPolygon.path[i])
136 137 138 139 140 141 142 143 144 145 146
            }
        }
    }

    function fitMapViewportToFenceItems() {
        var coordList = [ ]
        addFenceItemCoordsForFit(coordList)
        fitMapViewportToAllCoordinates(coordList)
    }

    function addRallyItemCoordsForFit(coordList) {
147 148
        for (var i=0; i<_rallyPointController.points.count; i++) {
            coordList.push(_rallyPointController.points.get(i).coordinate)
149 150 151 152 153 154 155 156 157 158
        }
    }

    function fitMapViewportToRallyItems() {
        var coordList = [ ]
        addRallyItemCoordsForFit(coordList)
        fitMapViewportToAllCoordinates(coordList)
    }

    function fitMapViewportToAllItems() {
159
        if (!_missionController.visualItems) {
160 161 162
            // Being called prior to controller.start
            return
        }
163 164 165 166 167 168 169
        var coordList = [ ]
        addMissionItemCoordsForFit(coordList)
        addFenceItemCoordsForFit(coordList)
        addRallyItemCoordsForFit(coordList)
        fitMapViewportToAllCoordinates(coordList)
    }
} // Item