QGCMapUrlEngine.cpp 7.77 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
dogmaphobic's avatar
dogmaphobic committed
9 10 11

/**
 *  @file
12 13 14
 *  @author Gus Grubba <gus@auterion.com>
 *  Original work: The OpenPilot Team, http://www.openpilot.org Copyright (C)
 * 2012.
dogmaphobic's avatar
dogmaphobic committed
15 16
 */

dogmaphobic's avatar
dogmaphobic committed
17 18
//#define DEBUG_GOOGLE_MAPS

19 20 21 22
#include "QGCLoggingCategory.h"
QGC_LOGGING_CATEGORY(QGCMapUrlEngineLog, "QGCMapUrlEngineLog")

#include "AppSettings.h"
23
#include "QGCApplication.h"
dogmaphobic's avatar
dogmaphobic committed
24
#include "QGCMapEngine.h"
25
#include "SettingsManager.h"
dogmaphobic's avatar
dogmaphobic committed
26

27 28

#include <QByteArray>
dogmaphobic's avatar
dogmaphobic committed
29
#include <QEventLoop>
30 31
#include <QNetworkReply>
#include <QRegExp>
dogmaphobic's avatar
dogmaphobic committed
32
#include <QString>
33
#include <QTimer>
Gus Grubba's avatar
Gus Grubba committed
34

dogmaphobic's avatar
dogmaphobic committed
35
//-----------------------------------------------------------------------------
36
UrlFactory::UrlFactory() : _timeout(5 * 1000) {
37

38 39
    // Warning : in _providersTable, keys needs to follow this format :
    // "Provider Type"
40
#ifndef QGC_NO_GOOGLE_MAPS
41 42 43 44 45
    _providersTable["Google Street Map"] = new GoogleStreetMapProvider(this);
    _providersTable["Google Satellite"]  = new GoogleSatelliteMapProvider(this);
    _providersTable["Google Terrain"]    = new GoogleTerrainMapProvider(this);
    _providersTable["Google Hybrid"]    = new GoogleHybridMapProvider(this);
    _providersTable["Google Labels"]     = new GoogleTerrainMapProvider(this);
46
#endif
dogmaphobic's avatar
dogmaphobic committed
47

48 49 50 51 52 53 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
    _providersTable["Bing Road"]      = new BingRoadMapProvider(this);
    _providersTable["Bing Satellite"] = new BingSatelliteMapProvider(this);
    _providersTable["Bing Hybrid"]    = new BingHybridMapProvider(this);

    _providersTable["Statkart Topo"] = new StatkartMapProvider(this);

    _providersTable["Eniro Topo"] = new EniroMapProvider(this);

    // To be add later on Token entry !
    //_providersTable["Esri World Street"] = new EsriWorldStreetMapProvider(this);
    //_providersTable["Esri World Satellite"] = new EsriWorldSatelliteMapProvider(this);
    //_providersTable["Esri Terrain"] = new EsriTerrainMapProvider(this);

    _providersTable["Mapbox Streets"]      = new MapboxStreetMapProvider(this);
    _providersTable["Mapbox Light"]        = new MapboxLightMapProvider(this);
    _providersTable["Mapbox Dark"]         = new MapboxDarkMapProvider(this);
    _providersTable["Mapbox Satellite"]    = new MapboxSatelliteMapProvider(this);
    _providersTable["Mapbox Hybrid"]       = new MapboxHybridMapProvider(this);
    _providersTable["Mapbox StreetsBasic"] = new MapboxStreetsBasicMapProvider(this);
    _providersTable["Mapbox Outdoors"]     = new MapboxOutdoorsMapProvider(this);
    _providersTable["Mapbox RunBikeHike"]  = new MapboxRunBikeHikeMapProvider(this);
    _providersTable["Mapbox HighContrast"] = new MapboxHighContrastMapProvider(this);
    _providersTable["Mapbox Custom"]       = new MapboxCustomMapProvider(this);

    //_providersTable["MapQuest Map"] = new MapQuestMapMapProvider(this);
    //_providersTable["MapQuest Sat"] = new MapQuestSatMapProvider(this);
    
    _providersTable["VWorld Street Map"] = new VWorldStreetMapProvider(this);
    _providersTable["VWorld Satellite Map"] = new VWorldSatMapProvider(this);

    _providersTable["Airmap Elevation"] = new AirmapElevationProvider(this);

    _providersTable["Japan-GSI Contour"] = new JapanStdMapProvider(this);
    _providersTable["Japan-GSI Seamless"] = new JapanSeamlessMapProvider(this);
    _providersTable["Japan-GSI Anaglyph"] = new JapanAnaglyphMapProvider(this);
    _providersTable["Japan-GSI Slope"] = new JapanSlopeMapProvider(this);
    _providersTable["Japan-GSI Relief"] = new JapanReliefMapProvider(this);
dogmaphobic's avatar
dogmaphobic committed
85 86
}

87 88 89
void UrlFactory::registerProvider(QString name, MapProvider* provider) {
    _providersTable[name] = provider;
}
dogmaphobic's avatar
dogmaphobic committed
90 91

//-----------------------------------------------------------------------------
92 93 94 95 96 97 98 99 100
UrlFactory::~UrlFactory() {}

QString UrlFactory::getImageFormat(int id, const QByteArray& image) {
    QString type = getTypeFromId(id);
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[getTypeFromId(id)]->getImageFormat(image);
    } else {
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
        return "";
dogmaphobic's avatar
dogmaphobic committed
101 102 103 104
    }
}

//-----------------------------------------------------------------------------
105 106 107 108 109 110
QString UrlFactory::getImageFormat(QString type, const QByteArray& image) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getImageFormat(image);
    } else {
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
        return "";
111
    }
112 113 114 115 116 117 118
}
QNetworkRequest UrlFactory::getTileURL(int id, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {

    QString type = getTypeFromId(id);
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
dogmaphobic's avatar
dogmaphobic committed
119
    }
120 121 122

    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
    return QNetworkRequest(QUrl());
dogmaphobic's avatar
dogmaphobic committed
123 124 125
}

//-----------------------------------------------------------------------------
126 127 128 129
QNetworkRequest UrlFactory::getTileURL(QString type, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
dogmaphobic's avatar
dogmaphobic committed
130
    }
131 132
    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
    return QNetworkRequest(QUrl());
dogmaphobic's avatar
dogmaphobic committed
133 134 135
}

//-----------------------------------------------------------------------------
136 137 138 139 140 141 142 143 144 145 146 147 148 149
quint32 UrlFactory::averageSizeForType(QString type) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getAverageSize();
    } 
    qCDebug(QGCMapUrlEngineLog) << "UrlFactory::averageSizeForType " << type
        << " Not registered";

    //    case AirmapElevation:
    //        return AVERAGE_AIRMAP_ELEV_SIZE;
    //    default:
    //        break;
    //    }
    return AVERAGE_TILE_SIZE;
}
stmoon's avatar
stmoon committed
150

151
QString UrlFactory::getTypeFromId(int id) {
stmoon's avatar
stmoon committed
152

153 154 155 156 157 158
    QHashIterator<QString, MapProvider*> i(_providersTable);

    while (i.hasNext()) {
        i.next();
        if ((int)(qHash(i.key())>>1) == id) {
            return i.key();
stmoon's avatar
stmoon committed
159 160
        }
    }
161 162
    qCDebug(QGCMapUrlEngineLog) << "getTypeFromId : id not found" << id;
    return "";
dogmaphobic's avatar
dogmaphobic committed
163 164
}

165
MapProvider* UrlFactory::getMapProviderFromId(int id)
dogmaphobic's avatar
dogmaphobic committed
166
{
167 168 169 170
    QString type = getTypeFromId(id);
    if (!type.isEmpty()) {
        if (_providersTable.find(type) != _providersTable.end()) {
            return _providersTable[type];
dogmaphobic's avatar
dogmaphobic committed
171 172
        }
    }
173
    return nullptr;
dogmaphobic's avatar
dogmaphobic committed
174 175
}

176 177 178 179 180
// Todo : qHash produce a uint bigger than max(int)
// There is still a low probability for this to
// generate similar hash for different types
int UrlFactory::getIdFromType(QString type) { return (int)(qHash(type)>>1); }

dogmaphobic's avatar
dogmaphobic committed
181 182
//-----------------------------------------------------------------------------
int
183
UrlFactory::long2tileX(QString mapType, double lon, int z)
dogmaphobic's avatar
dogmaphobic committed
184
{
185
    return _providersTable[mapType]->long2tileX(lon, z);
dogmaphobic's avatar
dogmaphobic committed
186 187 188
}

//-----------------------------------------------------------------------------
189 190
int
UrlFactory::lat2tileY(QString mapType, double lat, int z)
dogmaphobic's avatar
dogmaphobic committed
191
{
192
    return _providersTable[mapType]->lat2tileY(lat, z);
dogmaphobic's avatar
dogmaphobic committed
193 194 195 196
}


//-----------------------------------------------------------------------------
197 198
QGCTileSet
UrlFactory::getTileCount(int zoom, double topleftLon, double topleftLat, double bottomRightLon, double bottomRightLat, QString mapType)
dogmaphobic's avatar
dogmaphobic committed
199
{
200
	return _providersTable[mapType]->getTileCount(zoom, topleftLon, topleftLat, bottomRightLon, bottomRightLat);
dogmaphobic's avatar
dogmaphobic committed
201 202
}

203 204
bool UrlFactory::isElevation(int mapId){
    return _providersTable[getTypeFromId(mapId)]->_isElevationProvider();
dogmaphobic's avatar
dogmaphobic committed
205
}