QGCMapUrlEngine.cpp 7.39 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
    _providersTable["Mapbox Custom"]       = new MapboxCustomMapProvider(this);
71 72 73 74 75 76

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

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

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

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

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

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

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

//-----------------------------------------------------------------------------
120 121
QNetworkRequest UrlFactory::getTileURL(QString type, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
122 123 124 125 126
    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
127 128 129
}

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

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

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

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

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

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

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

//-----------------------------------------------------------------------------
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);
}

189 190 191 192 193 194 195

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

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