QGCMapUrlEngine.cpp 7.77 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 83 84

    _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);
85 86
}

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

//-----------------------------------------------------------------------------
92 93 94 95 96 97 98
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 {
99
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
100 101
        return "";
    }
102
}
dogmaphobic's avatar
dogmaphobic committed
103 104

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

116 117 118 119
    QString type = getTypeFromId(id);
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
    }
120 121

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

//-----------------------------------------------------------------------------
126 127
QNetworkRequest UrlFactory::getTileURL(QString type, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
128 129 130 131 132
    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
133 134 135
}

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

143 144 145 146 147
    //    case AirmapElevation:
    //        return AVERAGE_AIRMAP_ELEV_SIZE;
    //    default:
    //        break;
    //    }
148
    return AVERAGE_TILE_SIZE;
dogmaphobic's avatar
dogmaphobic committed
149
}
150

151
QString UrlFactory::getTypeFromId(int id) {
152 153 154

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

155
    while (i.hasNext()) {
156
        i.next();
157
        if ((int)(qHash(i.key())>>1) == id) {
158 159 160
            return i.key();
        }
    }
161
    qCDebug(QGCMapUrlEngineLog) << "getTypeFromId : id not found" << id;
162 163 164
    return "";
}

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

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

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

195 196 197 198 199 200 201

//-----------------------------------------------------------------------------
QGCTileSet
UrlFactory::getTileCount(int zoom, double topleftLon, double topleftLat, double bottomRightLon, double bottomRightLat, QString mapType)
{
	return _providersTable[mapType]->getTileCount(zoom, topleftLon, topleftLat, bottomRightLon, bottomRightLat);
}
202 203 204 205

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