QGCMapUrlEngine.cpp 22.7 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * 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 12 13 14 15 16


/**
 *  @file
 *  @author Gus Grubba <mavlink@grubba.com>
 *  Original work: The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
 */

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

19
#include "QGCApplication.h"
dogmaphobic's avatar
dogmaphobic committed
20
#include "QGCMapEngine.h"
21 22
#include "AppSettings.h"
#include "SettingsManager.h"
dogmaphobic's avatar
dogmaphobic committed
23

dogmaphobic's avatar
dogmaphobic committed
24 25 26 27 28 29 30
#include <QRegExp>
#include <QNetworkReply>
#include <QEventLoop>
#include <QTimer>
#include <QString>
#include <QByteArray>

dogmaphobic's avatar
dogmaphobic committed
31 32 33 34 35
#if defined(DEBUG_GOOGLE_MAPS)
#include <QFile>
#include <QStandardPaths>
#endif

Gus Grubba's avatar
Gus Grubba committed
36 37 38 39
static const unsigned char pngSignature[]   = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00};
static const unsigned char jpegSignature[]  = {0xFF, 0xD8, 0xFF, 0x00};
static const unsigned char gifSignature[]   = {0x47, 0x49, 0x46, 0x38, 0x00};

dogmaphobic's avatar
dogmaphobic committed
40 41 42
//-----------------------------------------------------------------------------
UrlFactory::UrlFactory()
    : _timeout(5 * 1000)
43
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
44
    , _googleVersionRetrieved(false)
45
    , _googleReply(nullptr)
46
#endif
dogmaphobic's avatar
dogmaphobic committed
47 48 49 50 51
{
    QStringList langs = QLocale::system().uiLanguages();
    if (langs.length() > 0) {
        _language = langs[0];
    }
52

53
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
54
    // Google version strings
Don Gagne's avatar
Don Gagne committed
55 56
    _versionGoogleMap            = "m@354000000";
    _versionGoogleSatellite      = "692";
dogmaphobic's avatar
dogmaphobic committed
57
    _versionGoogleLabels         = "h@336";
Don Gagne's avatar
Don Gagne committed
58
    _versionGoogleTerrain        = "t@354,r@354000000";
Phisten's avatar
Phisten committed
59
    _versionGoogleHybrid         = "y";
dogmaphobic's avatar
dogmaphobic committed
60
    _secGoogleWord               = "Galileo";
61
#endif
dogmaphobic's avatar
dogmaphobic committed
62 63 64 65 66 67 68
    // BingMaps
    _versionBingMaps             = "563";
}

//-----------------------------------------------------------------------------
UrlFactory::~UrlFactory()
{
69
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
70 71
    if(_googleReply)
        _googleReply->deleteLater();
72
#endif
dogmaphobic's avatar
dogmaphobic committed
73 74 75 76 77 78 79 80 81 82
}


//-----------------------------------------------------------------------------
QString
UrlFactory::getImageFormat(MapType type, const QByteArray& image)
{
    QString format;
    if(image.size() > 2)
    {
Gus Grubba's avatar
Gus Grubba committed
83
        if (image.startsWith(reinterpret_cast<const char*>(pngSignature)))
dogmaphobic's avatar
dogmaphobic committed
84
            format = "png";
Gus Grubba's avatar
Gus Grubba committed
85 86 87 88
        else if (image.startsWith(reinterpret_cast<const char*>(jpegSignature)))
            format = "jpg";
        else if (image.startsWith(reinterpret_cast<const char*>(gifSignature)))
            format = "gif";
dogmaphobic's avatar
dogmaphobic committed
89 90 91 92 93 94 95
        else {
            switch (type) {
                case GoogleMap:
                case GoogleLabels:
                case GoogleTerrain:
                case GoogleHybrid:
                case BingMap:
96
                case StatkartTopo:
dogmaphobic's avatar
dogmaphobic committed
97 98
                    format = "png";
                    break;
99 100 101
                case EniroTopo:
                    format = "png";
                    break;
Gus Grubba's avatar
Gus Grubba committed
102
                /*
dogmaphobic's avatar
dogmaphobic committed
103 104
                case MapQuestMap:
                case MapQuestSat:
Gus Grubba's avatar
Gus Grubba committed
105 106
                case OpenStreetMap:
                */
Gus Grubba's avatar
Gus Grubba committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
                case MapboxStreets:
                case MapboxLight:
                case MapboxDark:
                case MapboxSatellite:
                case MapboxHybrid:
                case MapboxWheatPaste:
                case MapboxStreetsBasic:
                case MapboxComic:
                case MapboxOutdoors:
                case MapboxRunBikeHike:
                case MapboxPencil:
                case MapboxPirates:
                case MapboxEmerald:
                case MapboxHighContrast:
dogmaphobic's avatar
dogmaphobic committed
121 122 123 124 125
                case GoogleSatellite:
                case BingSatellite:
                case BingHybrid:
                    format = "jpg";
                    break;
126
                case AirmapElevation:
127
                    format = "bin";
128
                    break;
stmoon's avatar
stmoon committed
129 130 131 132 133 134
                case VWorldStreet :
                    format = "png";
                    break;
                case VWorldSatellite :
                    format = "jpg";
                    break;
dogmaphobic's avatar
dogmaphobic committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                default:
                    qWarning("UrlFactory::getImageFormat() Unknown map id %d", type);
                    break;
            }
        }
    }
    return format;
}

//-----------------------------------------------------------------------------
QNetworkRequest
UrlFactory::getTileURL(MapType type, int x, int y, int zoom, QNetworkAccessManager* networkManager)
{
    //-- Build URL
    QNetworkRequest request;
    QString url = _getURL(type, x, y, zoom, networkManager);
151
    if(url.isEmpty()) {
dogmaphobic's avatar
dogmaphobic committed
152
        return request;
153
    }
dogmaphobic's avatar
dogmaphobic committed
154 155 156
    request.setUrl(QUrl(url));
    request.setRawHeader("Accept", "*/*");
    switch (type) {
157
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
158 159 160 161 162
        case GoogleMap:
        case GoogleSatellite:
        case GoogleLabels:
        case GoogleTerrain:
        case GoogleHybrid:
dogmaphobic's avatar
dogmaphobic committed
163
            request.setRawHeader("Referrer", "https://www.google.com/maps/preview");
dogmaphobic's avatar
dogmaphobic committed
164
            break;
165
#endif
dogmaphobic's avatar
dogmaphobic committed
166 167 168
        case BingHybrid:
        case BingMap:
        case BingSatellite:
dogmaphobic's avatar
dogmaphobic committed
169
            request.setRawHeader("Referrer", "https://www.bing.com/maps/");
dogmaphobic's avatar
dogmaphobic committed
170
            break;
171 172 173
        case StatkartTopo:
            request.setRawHeader("Referrer", "https://www.norgeskart.no/");
            break;
174 175 176
        case EniroTopo:
            request.setRawHeader("Referrer", "https://www.eniro.se/");
            break;
dogmaphobic's avatar
dogmaphobic committed
177 178 179 180 181 182 183 184 185 186
        /*
        case OpenStreetMapSurfer:
        case OpenStreetMapSurferTerrain:
            request.setRawHeader("Referrer", "http://www.mapsurfer.net/");
            break;
        case OpenStreetMap:
        case OpenStreetOsm:
            request.setRawHeader("Referrer", "https://www.openstreetmap.org/");
            break;
        */
Gus Grubba's avatar
Gus Grubba committed
187 188 189 190

        case EsriWorldStreet:
        case EsriWorldSatellite:
        case EsriTerrain: {
191
                QByteArray token = qgcApp()->toolbox()->settingsManager()->appSettings()->esriToken()->rawValue().toString().toLatin1();
Gus Grubba's avatar
Gus Grubba committed
192 193 194 195 196
                request.setRawHeader("User-Agent", QByteArrayLiteral("Qt Location based application"));
                request.setRawHeader("User-Token", token);
            }
            return request;

197 198 199 200
        case AirmapElevation:
            request.setRawHeader("Referrer", "https://api.airmap.com/");
            break;

dogmaphobic's avatar
dogmaphobic committed
201 202 203
        default:
            break;
    }
Gus Grubba's avatar
Gus Grubba committed
204
    request.setRawHeader("User-Agent", _userAgent);
dogmaphobic's avatar
dogmaphobic committed
205 206 207 208
    return request;
}

//-----------------------------------------------------------------------------
209
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
210 211 212 213 214 215 216 217 218 219 220
void
UrlFactory::_getSecGoogleWords(int x, int y, QString &sec1, QString &sec2)
{
    sec1 = ""; // after &x=...
    sec2 = ""; // after &zoom=...
    int seclen = ((x * 3) + y) % 8;
    sec2 = _secGoogleWord.left(seclen);
    if (y >= 10000 && y < 100000) {
        sec1 = "&s=";
    }
}
221
#endif
dogmaphobic's avatar
dogmaphobic committed
222 223 224 225 226 227

//-----------------------------------------------------------------------------
QString
UrlFactory::_getURL(MapType type, int x, int y, int zoom, QNetworkAccessManager* networkManager)
{
    switch (type) {
228 229 230
#ifdef QGC_NO_GOOGLE_MAPS
    Q_UNUSED(networkManager);
#else
dogmaphobic's avatar
dogmaphobic committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    case GoogleMap:
    {
        // http://mt1.google.com/vt/lyrs=m
        QString server  = "mt";
        QString request = "vt";
        QString sec1    = ""; // after &x=...
        QString sec2    = ""; // after &zoom=...
        _getSecGoogleWords(x, y, sec1, sec2);
        _tryCorrectGoogleVersions(networkManager);
        return QString("http://%1%2.google.com/%3/lyrs=%4&hl=%5&x=%6%7&y=%8&z=%9&s=%10").arg(server).arg(_getServerNum(x, y, 4)).arg(request).arg(_versionGoogleMap).arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2);
    }
    break;
    case GoogleSatellite:
    {
        // http://mt1.google.com/vt/lyrs=s
        QString server  = "khm";
        QString request = "kh";
        QString sec1    = ""; // after &x=...
        QString sec2    = ""; // after &zoom=...
        _getSecGoogleWords(x, y, sec1, sec2);
        _tryCorrectGoogleVersions(networkManager);
        return QString("http://%1%2.google.com/%3/v=%4&hl=%5&x=%6%7&y=%8&z=%9&s=%10").arg(server).arg(_getServerNum(x, y, 4)).arg(request).arg(_versionGoogleSatellite).arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2);
    }
    break;
    case GoogleLabels:
    {
        QString server  = "mts";
        QString request = "vt";
        QString sec1    = ""; // after &x=...
        QString sec2    = ""; // after &zoom=...
        _getSecGoogleWords(x, y, sec1, sec2);
        _tryCorrectGoogleVersions(networkManager);
        return QString("http://%1%2.google.com/%3/lyrs=%4&hl=%5&x=%6%7&y=%8&z=%9&s=%10").arg(server).arg(_getServerNum(x, y, 4)).arg(request).arg(_versionGoogleLabels).arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2);
    }
    break;
    case GoogleTerrain:
    {
        QString server  = "mt";
        QString request = "vt";
        QString sec1    = ""; // after &x=...
        QString sec2    = ""; // after &zoom=...
        _getSecGoogleWords(x, y, sec1, sec2);
        _tryCorrectGoogleVersions(networkManager);
        return QString("http://%1%2.google.com/%3/v=%4&hl=%5&x=%6%7&y=%8&z=%9&s=%10").arg(server).arg(_getServerNum(x, y, 4)).arg(request).arg(_versionGoogleTerrain).arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2);
    }
    break;
Phisten's avatar
Phisten committed
277 278 279 280 281 282 283 284 285 286 287
    case GoogleHybrid:
    {
        QString server  = "mt";
        QString request = "vt";
        QString sec1    = ""; // after &x=...
        QString sec2    = ""; // after &zoom=...
        _getSecGoogleWords(x, y, sec1, sec2);
        _tryCorrectGoogleVersions(networkManager);
        return QString("http://%1%2.google.com/%3/lyrs=%4&hl=%5&x=%6%7&y=%8&z=%9&s=%10").arg(server).arg(_getServerNum(x, y, 4)).arg(request).arg(_versionGoogleHybrid).arg(_language).arg(x).arg(sec1).arg(y).arg(zoom).arg(sec2);
    }
    break;
288
#endif
289 290
    case StatkartTopo:
    {
291
        return QString("http://opencache.statkart.no/gatekeeper/gk/gk.open_gmaps?layers=topo4&zoom=%1&x=%2&y=%3").arg(zoom).arg(x).arg(y);
292 293
    }
    break;
294 295 296 297 298
    case EniroTopo:
    {
    	return QString("http://map.eniro.com/geowebcache/service/tms1.0.0/map/%1/%2/%3.png").arg(zoom).arg(x).arg((1<<zoom)-1-y);
    }
    break;
dogmaphobic's avatar
dogmaphobic committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    /*
    case OpenStreetMap:
    {
        char letter = "abc"[_getServerNum(x, y, 3)];
        return QString("https://%1.tile.openstreetmap.org/%2/%3/%4.png").arg(letter).arg(zoom).arg(x).arg(y);
    }
    break;
    case OpenStreetOsm:
    {
        char letter = "abc"[_getServerNum(x, y, 3)];
        return QString("http://%1.tah.openstreetmap.org/Tiles/tile/%2/%3/%4.png").arg(letter).arg(zoom).arg(x).arg(y);
    }
    break;
    case OpenStreetMapSurfer:
    {
        // http://tiles1.mapsurfer.net/tms_r.ashx?x=37378&y=20826&z=16
        return QString("http://tiles1.mapsurfer.net/tms_r.ashx?x=%1&y=%2&z=%3").arg(x).arg(y).arg(zoom);
    }
    break;
    case OpenStreetMapSurferTerrain:
    {
        // http://tiles2.mapsurfer.net/tms_t.ashx?x=9346&y=5209&z=14
        return QString("http://tiles2.mapsurfer.net/tms_t.ashx?x=%1&y=%2&z=%3").arg(x).arg(y).arg(zoom);
    }
    break;
    */
    case BingMap:
    {
        QString key = _tileXYToQuadKey(x, y, zoom);
        return QString("http://ecn.t%1.tiles.virtualearth.net/tiles/r%2.png?g=%3&mkt=%4").arg(_getServerNum(x, y, 4)).arg(key).arg(_versionBingMaps).arg(_language);
    }
    break;
    case BingSatellite:
    {
        QString key = _tileXYToQuadKey(x, y, zoom);
        return QString("http://ecn.t%1.tiles.virtualearth.net/tiles/a%2.jpeg?g=%3&mkt=%4").arg(_getServerNum(x, y, 4)).arg(key).arg(_versionBingMaps).arg(_language);
    }
    break;
    case BingHybrid:
    {
        QString key = _tileXYToQuadKey(x, y, zoom);
        return QString("http://ecn.t%1.tiles.virtualearth.net/tiles/h%2.jpeg?g=%3&mkt=%4").arg(_getServerNum(x, y, 4)).arg(key).arg(_versionBingMaps).arg(_language);
    }
Gus Grubba's avatar
Gus Grubba committed
342
    /*
dogmaphobic's avatar
dogmaphobic committed
343 344 345 346 347 348 349 350 351 352 353 354
    case MapQuestMap:
    {
        char letter = "1234"[_getServerNum(x, y, 4)];
        return QString("http://otile%1.mqcdn.com/tiles/1.0.0/map/%2/%3/%4.jpg").arg(letter).arg(zoom).arg(x).arg(y);
    }
    break;
    case MapQuestSat:
    {
        char letter = "1234"[_getServerNum(x, y, 4)];
        return QString("http://otile%1.mqcdn.com/tiles/1.0.0/sat/%2/%3/%4.jpg").arg(letter).arg(zoom).arg(x).arg(y);
    }
    break;
Gus Grubba's avatar
Gus Grubba committed
355 356 357 358 359 360 361
    */
    case EsriWorldStreet:
        return QString("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/%1/%2/%3").arg(zoom).arg(y).arg(x);
    case EsriWorldSatellite:
        return QString("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/%1/%2/%3").arg(zoom).arg(y).arg(x);
    case EsriTerrain:
        return QString("http://server.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer/tile/%1/%2/%3").arg(zoom).arg(y).arg(x);
dogmaphobic's avatar
dogmaphobic committed
362

Gus Grubba's avatar
Gus Grubba committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376
    case MapboxStreets:
    case MapboxLight:
    case MapboxDark:
    case MapboxSatellite:
    case MapboxHybrid:
    case MapboxWheatPaste:
    case MapboxStreetsBasic:
    case MapboxComic:
    case MapboxOutdoors:
    case MapboxRunBikeHike:
    case MapboxPencil:
    case MapboxPirates:
    case MapboxEmerald:
    case MapboxHighContrast:
dogmaphobic's avatar
dogmaphobic committed
377
    {
378
        QString mapBoxToken = qgcApp()->toolbox()->settingsManager()->appSettings()->mapboxToken()->rawValue().toString();
dogmaphobic's avatar
dogmaphobic committed
379 380 381
        if(!mapBoxToken.isEmpty()) {
            QString server = "https://api.mapbox.com/v4/";
            switch(type) {
Gus Grubba's avatar
Gus Grubba committed
382
                case MapboxStreets:
dogmaphobic's avatar
dogmaphobic committed
383 384
                    server += "mapbox.streets";
                    break;
Gus Grubba's avatar
Gus Grubba committed
385
                case MapboxLight:
dogmaphobic's avatar
dogmaphobic committed
386 387
                    server += "mapbox.light";
                    break;
Gus Grubba's avatar
Gus Grubba committed
388
                case MapboxDark:
dogmaphobic's avatar
dogmaphobic committed
389 390
                    server += "mapbox.dark";
                    break;
Gus Grubba's avatar
Gus Grubba committed
391
                case MapboxSatellite:
dogmaphobic's avatar
dogmaphobic committed
392 393
                    server += "mapbox.satellite";
                    break;
Gus Grubba's avatar
Gus Grubba committed
394
                case MapboxHybrid:
dogmaphobic's avatar
dogmaphobic committed
395 396
                    server += "mapbox.streets-satellite";
                    break;
Gus Grubba's avatar
Gus Grubba committed
397
                case MapboxWheatPaste:
dogmaphobic's avatar
dogmaphobic committed
398 399
                    server += "mapbox.wheatpaste";
                    break;
Gus Grubba's avatar
Gus Grubba committed
400
                case MapboxStreetsBasic:
dogmaphobic's avatar
dogmaphobic committed
401 402
                    server += "mapbox.streets-basic";
                    break;
Gus Grubba's avatar
Gus Grubba committed
403
                case MapboxComic:
dogmaphobic's avatar
dogmaphobic committed
404 405
                    server += "mapbox.comic";
                    break;
Gus Grubba's avatar
Gus Grubba committed
406
                case MapboxOutdoors:
dogmaphobic's avatar
dogmaphobic committed
407 408
                    server += "mapbox.outdoors";
                    break;
Gus Grubba's avatar
Gus Grubba committed
409
                case MapboxRunBikeHike:
dogmaphobic's avatar
dogmaphobic committed
410 411
                    server += "mapbox.run-bike-hike";
                    break;
Gus Grubba's avatar
Gus Grubba committed
412
                case MapboxPencil:
dogmaphobic's avatar
dogmaphobic committed
413 414
                    server += "mapbox.pencil";
                    break;
Gus Grubba's avatar
Gus Grubba committed
415
                case MapboxPirates:
dogmaphobic's avatar
dogmaphobic committed
416 417
                    server += "mapbox.pirates";
                    break;
Gus Grubba's avatar
Gus Grubba committed
418
                case MapboxEmerald:
dogmaphobic's avatar
dogmaphobic committed
419 420
                    server += "mapbox.emerald";
                    break;
Gus Grubba's avatar
Gus Grubba committed
421
                case MapboxHighContrast:
dogmaphobic's avatar
dogmaphobic committed
422 423 424
                    server += "mapbox.high-contrast";
                    break;
                default:
425
                    return {};
dogmaphobic's avatar
dogmaphobic committed
426 427 428 429 430 431
            }
            server += QString("/%1/%2/%3.jpg80?access_token=%4").arg(zoom).arg(x).arg(y).arg(mapBoxToken);
            return server;
        }
    }
    break;
432 433
    case AirmapElevation:
    {
Andreas Bircher's avatar
Andreas Bircher committed
434 435 436 437
        return QString("https://api.airmap.com/elevation/v1/ele/carpet?points=%1,%2,%3,%4").arg(static_cast<double>(y)*QGCMapEngine::srtm1TileSize - 90.0).arg(
                                                                                                static_cast<double>(x)*QGCMapEngine::srtm1TileSize - 180.0).arg(
                                                                                                static_cast<double>(y + 1)*QGCMapEngine::srtm1TileSize - 90.0).arg(
                                                                                                static_cast<double>(x + 1)*QGCMapEngine::srtm1TileSize - 180.0);
438 439
    }
    break;
dogmaphobic's avatar
dogmaphobic committed
440

stmoon's avatar
stmoon committed
441 442 443 444 445 446 447 448
    case VWorldStreet :
    {
        int gap = zoom - 6;
        int x_min = 53 * pow(2, gap);
        int x_max = 55 * pow(2, gap) + (2*gap - 1);
        int y_min = 22 * pow(2, gap);
        int y_max = 26 * pow(2, gap) + (2*gap - 1);

449
        if ( zoom > 19 ) {
450
            return {};
451 452
        }
        else if ( zoom > 5 && x >= x_min && x <= x_max && y >= y_min && y <= y_max ) {
stmoon's avatar
stmoon committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
            return QString("http://xdworld.vworld.kr:8080/2d/Base/service/%1/%2/%3.png").arg(zoom).arg(x).arg(y);
        }
        else {
            QString key = _tileXYToQuadKey(x, y, zoom);
            return QString("http://ecn.t%1.tiles.virtualearth.net/tiles/r%2.png?g=%3&mkt=%4").arg(_getServerNum(x, y, 4)).arg(key).arg(_versionBingMaps).arg(_language);
        }


    }
        break;

    case VWorldSatellite :
    {
        int gap = zoom - 6;
        int x_min = 53 * pow(2, gap);
        int x_max = 55 * pow(2, gap) + (2*gap - 1);
        int y_min = 22 * pow(2, gap);
        int y_max = 26 * pow(2, gap) + (2*gap - 1);

472
        if ( zoom > 19 ) {
473
            return {};
474 475
        }
        else if ( zoom > 5 && x >= x_min && x <= x_max && y >= y_min && y <= y_max ) {
stmoon's avatar
stmoon committed
476 477 478 479 480 481 482 483
            return QString("http://xdworld.vworld.kr:8080/2d/Satellite/service/%1/%2/%3.jpeg").arg(zoom).arg(x).arg(y);
        }
        else {
            QString key = _tileXYToQuadKey(x, y, zoom);
            return QString("http://ecn.t%1.tiles.virtualearth.net/tiles/a%2.jpeg?g=%3&mkt=%4").arg(_getServerNum(x, y, 4)).arg(key).arg(_versionBingMaps).arg(_language);
        }
    }
        break;
dogmaphobic's avatar
dogmaphobic committed
484 485 486 487
    default:
        qWarning("Unknown map id %d\n", type);
        break;
    }
488
    return {};
dogmaphobic's avatar
dogmaphobic committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}

//-----------------------------------------------------------------------------
QString
UrlFactory::_tileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
{
    QString quadKey;
    for (int i = levelOfDetail; i > 0; i--) {
        char digit = '0';
        int mask   = 1 << (i - 1);
        if ((tileX & mask) != 0) {
            digit++;
        }
        if ((tileY & mask) != 0) {
            digit++;
            digit++;
        }
        quadKey.append(digit);
    }
    return quadKey;
}

//-----------------------------------------------------------------------------
int
UrlFactory::_getServerNum(int x, int y, int max)
{
    return (x + 2 * y) % max;
}

//-----------------------------------------------------------------------------
519
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
520 521 522 523 524 525 526
void
UrlFactory::_networkReplyError(QNetworkReply::NetworkError error)
{
    qWarning() << "Could not connect to google maps. Error:" << error;
    if(_googleReply)
    {
        _googleReply->deleteLater();
527
        _googleReply = nullptr;
dogmaphobic's avatar
dogmaphobic committed
528 529
    }
}
530
#endif
dogmaphobic's avatar
dogmaphobic committed
531
//-----------------------------------------------------------------------------
532
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
533 534 535
void
UrlFactory::_replyDestroyed()
{
536
    _googleReply = nullptr;
dogmaphobic's avatar
dogmaphobic committed
537
}
538
#endif
dogmaphobic's avatar
dogmaphobic committed
539 540

//-----------------------------------------------------------------------------
541
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
542 543 544 545 546 547 548 549
void
UrlFactory::_googleVersionCompleted()
{
    if (!_googleReply || (_googleReply->error() != QNetworkReply::NoError)) {
        qDebug() << "Error collecting Google maps version info";
        return;
    }
    QString html = QString(_googleReply->readAll());
dogmaphobic's avatar
dogmaphobic committed
550 551 552 553 554 555 556 557 558

#if defined(DEBUG_GOOGLE_MAPS)
    QString filename = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    filename += "/google.output";
    QFile file(filename);
    if (file.open(QIODevice::ReadWrite))
    {
        QTextStream stream( &file );
        stream << html << endl;
dogmaphobic's avatar
dogmaphobic committed
559
    }
dogmaphobic's avatar
dogmaphobic committed
560 561 562
#endif

    QRegExp reg("\"*https?://mt\\D?\\d..*/vt\\?lyrs=m@(\\d*)", Qt::CaseInsensitive);
dogmaphobic's avatar
dogmaphobic committed
563 564
    if (reg.indexIn(html) != -1) {
        QStringList gc = reg.capturedTexts();
dogmaphobic's avatar
dogmaphobic committed
565
        _versionGoogleMap = QString("m@%1").arg(gc[1]);
dogmaphobic's avatar
dogmaphobic committed
566
    }
dogmaphobic's avatar
dogmaphobic committed
567
    reg = QRegExp("\"*https?://khm\\D?\\d.googleapis.com/kh\\?v=(\\d*)", Qt::CaseInsensitive);
dogmaphobic's avatar
dogmaphobic committed
568 569 570 571
    if (reg.indexIn(html) != -1) {
        QStringList gc = reg.capturedTexts();
        _versionGoogleSatellite = gc[1];
    }
dogmaphobic's avatar
dogmaphobic committed
572
    reg = QRegExp("\"*https?://mt\\D?\\d..*/vt\\?lyrs=t@(\\d*),r@(\\d*)", Qt::CaseInsensitive);
dogmaphobic's avatar
dogmaphobic committed
573 574 575 576 577
    if (reg.indexIn(html) != -1) {
        QStringList gc = reg.capturedTexts();
        _versionGoogleTerrain = QString("t@%1,r@%2").arg(gc[1]).arg(gc[2]);
    }
    _googleReply->deleteLater();
578
    _googleReply = nullptr;
dogmaphobic's avatar
dogmaphobic committed
579
}
580
#endif
dogmaphobic's avatar
dogmaphobic committed
581 582

//-----------------------------------------------------------------------------
583
#ifndef QGC_NO_GOOGLE_MAPS
dogmaphobic's avatar
dogmaphobic committed
584
void
dogmaphobic's avatar
dogmaphobic committed
585
UrlFactory::_tryCorrectGoogleVersions(QNetworkAccessManager* networkManager)
dogmaphobic's avatar
dogmaphobic committed
586 587 588 589 590 591 592 593 594 595 596
{
    QMutexLocker locker(&_googleVersionMutex);
    if (_googleVersionRetrieved) {
        return;
    }
    _googleVersionRetrieved = true;
    if(networkManager)
    {
        QNetworkRequest qheader;
        QNetworkProxy proxy = networkManager->proxy();
        QNetworkProxy tProxy;
597
        tProxy.setType(QNetworkProxy::DefaultProxy);
dogmaphobic's avatar
dogmaphobic committed
598
        networkManager->setProxy(tProxy);
dogmaphobic's avatar
dogmaphobic committed
599 600 601 602
        QSslConfiguration conf = qheader.sslConfiguration();
        conf.setPeerVerifyMode(QSslSocket::VerifyNone);
        qheader.setSslConfiguration(conf);
        QString url = "http://maps.google.com/maps/api/js?v=3.2&sensor=false";
dogmaphobic's avatar
dogmaphobic committed
603 604 605 606 607 608 609 610 611 612 613 614
        qheader.setUrl(QUrl(url));
        QByteArray ua;
        ua.append(getQGCMapEngine()->userAgent());
        qheader.setRawHeader("User-Agent", ua);
        _googleReply = networkManager->get(qheader);
        connect(_googleReply, &QNetworkReply::finished, this, &UrlFactory::_googleVersionCompleted);
        connect(_googleReply, &QNetworkReply::destroyed, this, &UrlFactory::_replyDestroyed);
        connect(_googleReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
                this, &UrlFactory::_networkReplyError);
        networkManager->setProxy(proxy);
    }
}
615
#endif
dogmaphobic's avatar
dogmaphobic committed
616 617 618 619 620 621 622 623 624

#define AVERAGE_GOOGLE_STREET_MAP   4913
#define AVERAGE_GOOGLE_TERRAIN_MAP  19391
#define AVERAGE_BING_STREET_MAP     1297
#define AVERAGE_BING_SAT_MAP        19597
#define AVERAGE_GOOGLE_SAT_MAP      56887
#define AVERAGE_MAPBOX_SAT_MAP      15739
#define AVERAGE_MAPBOX_STREET_MAP   5648
#define AVERAGE_TILE_SIZE           13652
625
#define AVERAGE_AIRMAP_ELEV_SIZE    2786
dogmaphobic's avatar
dogmaphobic committed
626 627 628 629 630 631 632 633 634 635 636 637

//-----------------------------------------------------------------------------
quint32
UrlFactory::averageSizeForType(MapType type)
{
    switch (type) {
    case GoogleMap:
        return AVERAGE_GOOGLE_STREET_MAP;
    case BingMap:
        return AVERAGE_BING_STREET_MAP;
    case GoogleSatellite:
        return AVERAGE_GOOGLE_SAT_MAP;
Gus Grubba's avatar
Gus Grubba committed
638
    case MapboxSatellite:
dogmaphobic's avatar
dogmaphobic committed
639 640 641 642 643 644
        return AVERAGE_MAPBOX_SAT_MAP;
    case BingHybrid:
    case BingSatellite:
        return AVERAGE_BING_SAT_MAP;
    case GoogleTerrain:
        return AVERAGE_GOOGLE_TERRAIN_MAP;
Gus Grubba's avatar
Gus Grubba committed
645 646 647
    case MapboxStreets:
    case MapboxStreetsBasic:
    case MapboxRunBikeHike:
dogmaphobic's avatar
dogmaphobic committed
648
        return AVERAGE_MAPBOX_STREET_MAP;
649 650
    case AirmapElevation:
        return AVERAGE_AIRMAP_ELEV_SIZE;
dogmaphobic's avatar
dogmaphobic committed
651
    case GoogleLabels:
Gus Grubba's avatar
Gus Grubba committed
652 653 654 655
    case MapboxDark:
    case MapboxLight:
    case MapboxOutdoors:
    case MapboxPencil:
dogmaphobic's avatar
dogmaphobic committed
656 657
    case OpenStreetMap:
    case GoogleHybrid:
Gus Grubba's avatar
Gus Grubba committed
658 659 660 661 662 663
    case MapboxComic:
    case MapboxEmerald:
    case MapboxHighContrast:
    case MapboxHybrid:
    case MapboxPirates:
    case MapboxWheatPaste:
dogmaphobic's avatar
dogmaphobic committed
664 665 666 667 668
    default:
        break;
    }
    return AVERAGE_TILE_SIZE;
}