QGCMapUrlEngine.cpp 7.31 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
 *
 * 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
Gus Grubba's avatar
Gus Grubba committed
12
 *  @author Gus Grubba <gus@auterion.com>
13 14
 *  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
#include "QGCLoggingCategory.h"
QGC_LOGGING_CATEGORY(QGCMapUrlEngineLog, "QGCMapUrlEngineLog")

22
#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

Pierre TILAK's avatar
Pierre TILAK committed
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
    _providersTable["Google Street Map"] = new GoogleStreetMapProvider(this);
42 43
    _providersTable["Google Satellite"]  = new GoogleSatelliteMapProvider(this);
    _providersTable["Google Terrain"]    = new GoogleTerrainMapProvider(this);
Pierre TILAK's avatar
Pierre TILAK committed
44
    _providersTable["Google Hybrid"]    = new GoogleHybridMapProvider(this);
45
    _providersTable["Google Labels"]     = new GoogleTerrainMapProvider(this);
46
#endif
Pierre TILAK's avatar
Pierre TILAK committed
47 48 49 50

    _providersTable["Bing Road"]      = new BingRoadMapProvider(this);
    _providersTable["Bing Satellite"] = new BingSatelliteMapProvider(this);
    _providersTable["Bing Hybrid"]    = new BingHybridMapProvider(this);
Pierre TILAK's avatar
Pierre TILAK committed
51

52 53 54 55 56 57 58 59
    _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);
Pierre TILAK's avatar
Pierre TILAK committed
60 61 62 63 64 65 66 67 68 69

    _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);
70 71 72 73 74 75

    //_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);
76 77

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

80 81
void UrlFactory::registerProvider(QString name, MapProvider* provider) {
    _providersTable[name] = provider;
dogmaphobic's avatar
dogmaphobic committed
82 83 84
}

//-----------------------------------------------------------------------------
85 86 87 88 89 90 91
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 {
92
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
93 94
        return "";
    }
95
}
dogmaphobic's avatar
dogmaphobic committed
96 97

//-----------------------------------------------------------------------------
98 99 100 101
QString UrlFactory::getImageFormat(QString type, const QByteArray& image) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getImageFormat(image);
    } else {
102
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
103 104
        return "";
    }
dogmaphobic's avatar
dogmaphobic committed
105
}
106 107
QNetworkRequest UrlFactory::getTileURL(int id, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
108

109 110 111 112
    QString type = getTypeFromId(id);
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
    }
113 114

    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
115
    return QNetworkRequest(QUrl());
116
}
dogmaphobic's avatar
dogmaphobic committed
117 118

//-----------------------------------------------------------------------------
119 120
QNetworkRequest UrlFactory::getTileURL(QString type, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
121 122 123 124 125
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
    }
    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
    return QNetworkRequest(QUrl());
dogmaphobic's avatar
dogmaphobic committed
126 127 128
}

//-----------------------------------------------------------------------------
129 130 131
quint32 UrlFactory::averageSizeForType(QString type) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getAverageSize();
132
    } 
133
    qCDebug(QGCMapUrlEngineLog) << "UrlFactory::averageSizeForType " << type
134
        << " Not registered";
135

136 137 138 139 140
    //    case AirmapElevation:
    //        return AVERAGE_AIRMAP_ELEV_SIZE;
    //    default:
    //        break;
    //    }
141
    return AVERAGE_TILE_SIZE;
dogmaphobic's avatar
dogmaphobic committed
142
}
143

144
QString UrlFactory::getTypeFromId(int id) {
145 146 147

    QHashIterator<QString, MapProvider*> i(_providersTable);

148
    while (i.hasNext()) {
149
        i.next();
150
        if ((int)(qHash(i.key())>>1) == id) {
151 152 153
            return i.key();
        }
    }
154
    qCDebug(QGCMapUrlEngineLog) << "getTypeFromId : id not found" << id;
155 156 157
    return "";
}

158 159 160 161 162 163 164 165 166 167 168
MapProvider* UrlFactory::getMapProviderFromId(int id)
{
    QString type = getTypeFromId(id);
    if (!type.isEmpty()) {
        if (_providersTable.find(type) != _providersTable.end()) {
            return _providersTable[type];
        }
    }
    return nullptr;
}

169
// Todo : qHash produce a uint bigger than max(int)
170
// There is still a low probability for this to
171
// generate similar hash for different types
172
int UrlFactory::getIdFromType(QString type) { return (int)(qHash(type)>>1); }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

//-----------------------------------------------------------------------------
int
UrlFactory::long2tileX(QString mapType, double lon, int z)
{
    return _providersTable[mapType]->long2tileX(lon, z);
}

//-----------------------------------------------------------------------------
int
UrlFactory::lat2tileY(QString mapType, double lat, int z)
{
    return _providersTable[mapType]->lat2tileY(lat, z);
}

188 189 190 191 192 193 194

//-----------------------------------------------------------------------------
QGCTileSet
UrlFactory::getTileCount(int zoom, double topleftLon, double topleftLat, double bottomRightLon, double bottomRightLat, QString mapType)
{
	return _providersTable[mapType]->getTileCount(zoom, topleftLon, topleftLat, bottomRightLon, bottomRightLat);
}
195 196 197 198

bool UrlFactory::isElevation(int mapId){
    return _providersTable[getTypeFromId(mapId)]->_isElevationProvider();
}