QGeoTiledMappingManagerEngineQGC.cpp 11.7 KB
Newer Older
dogmaphobic's avatar
dogmaphobic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/****************************************************************************
**
** Copyright (C) 2013 Aaron McCarthy <mccarthy.aaron@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
** 2015.4.4
** Adapted for use with QGroundControl
**
** Gus Grubba <mavlink@grubba.com>
**
****************************************************************************/

dogmaphobic's avatar
dogmaphobic committed
47 48 49 50
#include "QGCMapEngine.h"
#include "QGeoTiledMappingManagerEngineQGC.h"
#include "QGeoTileFetcherQGC.h"

dogmaphobic's avatar
dogmaphobic committed
51 52 53 54 55 56
#include <QtLocation/private/qgeocameracapabilities_p.h>
#include <QtLocation/private/qgeomaptype_p.h>
#if QT_VERSION < 0x050500
#include <QtLocation/private/qgeotiledmapdata_p.h>
#else
#include <QtLocation/private/qgeotiledmap_p.h>
57 58 59 60 61
#if QT_VERSION >= 0x050600
#include <QtLocation/private/qgeofiletilecache_p.h>
#else
#include <QtLocation/private/qgeotilecache_p.h>
#endif
dogmaphobic's avatar
dogmaphobic committed
62
#endif
63

dogmaphobic's avatar
dogmaphobic committed
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
#include <QDir>
#include <QStandardPaths>

#if QT_VERSION >= 0x050500
//-----------------------------------------------------------------------------
QGeoTiledMapQGC::QGeoTiledMapQGC(QGeoTiledMappingManagerEngine *engine, QObject *parent)
    : QGeoTiledMap(engine, parent)
{

}
#endif

//-----------------------------------------------------------------------------
QGeoTiledMappingManagerEngineQGC::QGeoTiledMappingManagerEngineQGC(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
:   QGeoTiledMappingManagerEngine()
{

    QGeoCameraCapabilities cameraCaps;
    cameraCaps.setMinimumZoomLevel(2.0);
    cameraCaps.setMaximumZoomLevel(MAX_MAP_ZOOM);
    cameraCaps.setSupportsBearing(true);
    setCameraCapabilities(cameraCaps);

    setTileSize(QSize(256, 256));

    /*
     * Most of these don't seem kosher at all. This was based on original code from OpenPilot and heavily modified to be used in QGC.
     */


    //-- IMPORTANT
    //   Changes here must reflect those in QGCMapEngine.cpp

    QList<QGeoMapType> mapTypes;
98 99

#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
100 101 102
    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "Google Street Map",        "Google street map",            false,  false,  UrlFactory::GoogleMap);
    mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay,   "Google Satellite Map",     "Google satellite map",         false,  false,  UrlFactory::GoogleSatellite);
    mapTypes << QGeoMapType(QGeoMapType::TerrainMap,        "Google Terrain Map",       "Google terrain map",           false,  false,  UrlFactory::GoogleTerrain);
103
#endif
dogmaphobic's avatar
dogmaphobic committed
104 105 106 107 108 109 110 111 112 113 114 115 116

    /* TODO:
     *  Proper google hybrid maps requires collecting two separate bimaps and overlaying them.
     *
     * mapTypes << QGeoMapType(QGeoMapType::HybridMap,       "Google Hybrid Map",        "Google hybrid map",            false, false, UrlFactory::GoogleHybrid);
     *
     */

    // Bing
    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "Bing Street Map",          "Bing street map",              false,  false,  UrlFactory::BingMap);
    mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay,   "Bing Satellite Map",       "Bing satellite map",           false,  false,  UrlFactory::BingSatellite);
    mapTypes << QGeoMapType(QGeoMapType::HybridMap,         "Bing Hybrid Map",          "Bing hybrid map",              false,  false,  UrlFactory::BingHybrid);

117 118 119
    // Statkart
    mapTypes << QGeoMapType(QGeoMapType::TerrainMap,             "Statkart Topo2",           "Statkart Topo2",               false,  false,  UrlFactory::StatkartTopo);

dogmaphobic's avatar
dogmaphobic committed
120 121 122 123 124
    /* See: https://wiki.openstreetmap.org/wiki/Tile_usage_policy
    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "Open Street Map",          "Open Street map",              false, false, UrlFactory::OpenStreetMap);
    */

    // MapQuest
125
    /*
dogmaphobic's avatar
dogmaphobic committed
126 127
    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "MapQuest Street Map",      "MapQuest street map",          false,  false,  UrlFactory::MapQuestMap);
    mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay,   "MapQuest Satellite Map",   "MapQuest satellite map",       false,  false,  UrlFactory::MapQuestSat);
128
    */
dogmaphobic's avatar
dogmaphobic committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    /*
     * These are OK as you need your own token for accessing it. Out-of-the box, QGC does not even offer these unless you enter a proper MapBox token.
     */

    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "MapBox Street Map",        "MapBox Street Map",            false,  false,  UrlFactory::MapBoxStreets);
    mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay,   "MapBox Satellite Map",     "MapBox Satellite Map",         false,  false,  UrlFactory::MapBoxSatellite);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox High Contrast Map", "MapBox High Contrast Map",     false,  false,  UrlFactory::MapBoxHighContrast);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Light Map",         "MapBox Light Map",             false,  false,  UrlFactory::MapBoxLight);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Dark Map",          "MapBox Dark Map",              false,  false,  UrlFactory::MapBoxDark);
    mapTypes << QGeoMapType(QGeoMapType::HybridMap,         "MapBox Hybrid Map",        "MapBox Hybrid Map",            false,  false,  UrlFactory::MapBoxHybrid);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Wheat Paste Map",   "MapBox Wheat Paste Map",       false,  false,  UrlFactory::MapBoxWheatPaste);
    mapTypes << QGeoMapType(QGeoMapType::StreetMap,         "MapBox Streets Basic Map", "MapBox Streets Basic Map",     false,  false,  UrlFactory::MapBoxStreetsBasic);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Comic Map",         "MapBox Comic Map",             false,  false,  UrlFactory::MapBoxComic);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Outdoors Map",      "MapBox Outdoors Map",          false,  false,  UrlFactory::MapBoxOutdoors);
    mapTypes << QGeoMapType(QGeoMapType::CycleMap,          "MapBox Run, Byke and Hike Map",   "MapBox Run, Byke and Hike Map",     false,  false,  UrlFactory::MapBoxRunBikeHike);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Pencil Map",        "MapBox Pencil Map",            false,  false,  UrlFactory::MapBoxPencil);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Pirates Map",       "MapBox Pirates Map",           false,  false,  UrlFactory::MapBoxPirates);
    mapTypes << QGeoMapType(QGeoMapType::CustomMap,         "MapBox Emerald Map",       "MapBox Emerald Map",           false,  false,  UrlFactory::MapBoxEmerald);

    setSupportedMapTypes(mapTypes);

    //-- Users (QML code) can define a different user agent
    if (parameters.contains(QStringLiteral("useragent"))) {
        getQGCMapEngine()->setUserAgent(parameters.value(QStringLiteral("useragent")).toString().toLatin1());
    }

#if QT_VERSION >= 0x050500
    _setCache(parameters);
#endif

    setTileFetcher(new QGeoTileFetcherQGC(this));

    *error = QGeoServiceProvider::NoError;
    errorString->clear();

#if QT_VERSION >= 0x050500
    if (parameters.contains(QStringLiteral("mapping.copyright")))
        m_customCopyright = parameters.value(QStringLiteral("mapping.copyright")).toString().toLatin1();
#endif
}

//-----------------------------------------------------------------------------
QGeoTiledMappingManagerEngineQGC::~QGeoTiledMappingManagerEngineQGC()
{
}

#if QT_VERSION < 0x050500

//-----------------------------------------------------------------------------
QGeoMapData *QGeoTiledMappingManagerEngineQGC::createMapData()
{
    return new QGeoTiledMapData(this, 0);
}

#else

//-----------------------------------------------------------------------------
QGeoMap*
QGeoTiledMappingManagerEngineQGC::createMap()
{
    return new QGeoTiledMapQGC(this);
}

//-----------------------------------------------------------------------------
QString
QGeoTiledMappingManagerEngineQGC::customCopyright() const
{
    return m_customCopyright;
}

#endif

#if QT_VERSION >= 0x050500
//-----------------------------------------------------------------------------
void
QGeoTiledMappingManagerEngineQGC::_setCache(const QVariantMap &parameters)
{
    QString cacheDir;
    if (parameters.contains(QStringLiteral("mapping.cache.directory")))
        cacheDir = parameters.value(QStringLiteral("mapping.cache.directory")).toString();
    else {
211 212 213 214 215 216
        cacheDir = getQGCMapEngine()->getCachePath();
        if(!QFileInfo(cacheDir).exists()) {
            if(!QDir::root().mkpath(cacheDir)) {
                qWarning() << "Could not create mapping disk cache directory: " << cacheDir;
                cacheDir = QDir::homePath() + QLatin1String("/.qgcmapscache/");
            }
dogmaphobic's avatar
dogmaphobic committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        }
    }
    if(!QFileInfo(cacheDir).exists()) {
        if(!QDir::root().mkpath(cacheDir)) {
            qWarning() << "Could not create mapping disk cache directory: " << cacheDir;
            cacheDir.clear();
        }
    }
    //-- Memory Cache
    uint32_t memLimit = 0;
    if (parameters.contains(QStringLiteral("mapping.cache.memory.size"))) {
      bool ok = false;
      memLimit = parameters.value(QStringLiteral("mapping.cache.memory.size")).toString().toUInt(&ok);
      if (!ok)
          memLimit = 0;
    }
    if(!memLimit)
    {
        //-- Value saved in MB
        memLimit = getQGCMapEngine()->getMaxMemCache() * (1024 * 1024);
    }
    //-- It won't work with less than 1M of memory cache
    if(memLimit < 1024 * 1024)
        memLimit = 1024 * 1024;
241 242 243 244
    //-- On the other hand, Qt uses signed 32-bit integers. Limit to 1G to round it down (you don't need more than that).
    if(memLimit > 1024 * 1024 * 1024)
        memLimit = 1024 * 1024 * 1024;
    //-- Disable Qt's disk cache (sort of)
245 246 247 248
#if QT_VERSION >= 0x050600
    QAbstractGeoTileCache *pTileCache = new QGeoFileTileCache(cacheDir);
    setTileCache(pTileCache);
#else
dogmaphobic's avatar
dogmaphobic committed
249
    QGeoTileCache* pTileCache = createTileCacheWithDir(cacheDir);
250
#endif
dogmaphobic's avatar
dogmaphobic committed
251 252
    if(pTileCache)
    {
253
        //-- We're basically telling it to use 100k of disk for cache. It doesn't like
254
        //   values smaller than that and I could not find a way to make it NOT cache.
255
        //   We handle our own disk caching elsewhere.
Gus Grubba's avatar
Gus Grubba committed
256
        pTileCache->setMaxDiskUsage(1024 * 100);
dogmaphobic's avatar
dogmaphobic committed
257 258 259 260
        pTileCache->setMaxMemoryUsage(memLimit);
    }
}
#endif