QGCMapUrlEngine.cpp 17.1 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

/**
 *  @file
 *  @author Gus Grubba <mavlink@grubba.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

    // BingMaps
    //_versionBingMaps             = "563";
40

41 42
    // Warning : in _providersTable, keys needs to follow this format :
    // "Provider Type"
43
#ifndef QGC_NO_GOOGLE_MAPS
44
    _providersTable["Google Street Map"] = new GoogleStreetMapProvider(this);
45 46 47
    _providersTable["Google Satellite"]  = new GoogleSatelliteMapProvider(this);
    _providersTable["Google Terrain"]    = new GoogleTerrainMapProvider(this);
    _providersTable["Google Labels"]     = new GoogleTerrainMapProvider(this);
48
#endif
Pierre TILAK's avatar
Pierre TILAK committed
49 50 51 52

    _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
53

54 55 56 57 58 59 60 61
    _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
62 63 64 65 66 67 68 69 70 71

    _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);
72 73
}

74 75
void UrlFactory::registerProvider(QString name, MapProvider* provider) {
    _providersTable[name] = provider;
dogmaphobic's avatar
dogmaphobic committed
76 77 78
}

//-----------------------------------------------------------------------------
79 80 81 82 83 84 85
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 {
86
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
87 88
        return "";
    }
89
}
dogmaphobic's avatar
dogmaphobic committed
90 91

//-----------------------------------------------------------------------------
92 93 94 95
QString UrlFactory::getImageFormat(QString type, const QByteArray& image) {
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getImageFormat(image);
    } else {
96
        qCDebug(QGCMapUrlEngineLog) << "getImageFormat : Map not registered :" << type;
97 98 99 100
        return "";
    }
    // QString format;
    // if(image.size() > 2)
101 102 103
    //{
    //    if (image.startsWith(reinterpret_cast<const char*>(pngSignature)))
    //        format = "png";
104 105
    //    else if (image.startsWith(reinterpret_cast<const
    //    char*>(jpegSignature)))
106
    //        format = "jpg";
107 108
    //    else if (image.startsWith(reinterpret_cast<const
    //    char*>(gifSignature)))
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    //        format = "gif";
    //    else {
    //        switch (type) {
    //            case StatkartTopo:
    //                format = "png";
    //                break;
    //            case EniroTopo:
    //                format = "png";
    //                break;
    //            /*
    //            case MapQuestMap:
    //            case MapQuestSat:
    //            case OpenStreetMap:
    //            */
    //            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:
    //            case GoogleSatellite:
    //            case BingSatellite:
    //            case BingHybrid:
    //                format = "jpg";
    //                break;
    //            case AirmapElevation:
    //                format = "bin";
    //                break;
    //            case VWorldStreet :
    //                format = "png";
    //                break;
    //            case VWorldSatellite :
    //                format = "jpg";
    //                break;
    //            default:
152 153
    //                qWarning("UrlFactory::getImageFormat() Unknown map id %d",
    //                type); break;
154 155 156
    //        }
    //    }
    //}
157
    // return format;
dogmaphobic's avatar
dogmaphobic committed
158
}
159 160
QNetworkRequest UrlFactory::getTileURL(int id, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
161

162 163 164 165
    QString type = getTypeFromId(id);
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
    }
166 167

    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
168
    return QNetworkRequest(QUrl());
169
}
dogmaphobic's avatar
dogmaphobic committed
170 171

//-----------------------------------------------------------------------------
172 173
QNetworkRequest UrlFactory::getTileURL(QString type, int x, int y, int zoom,
                                       QNetworkAccessManager* networkManager) {
174 175 176 177 178
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getTileURL(x, y, zoom, networkManager);
    }
    qCDebug(QGCMapUrlEngineLog) << "getTileURL : map not registered :" << type;
    return QNetworkRequest(QUrl());
179
    ////-- Build URL
180 181 182
    // QNetworkRequest request;
    // QString url = _getURL(type, x, y, zoom, networkManager);
    // if(url.isEmpty()) {
183 184
    //    return request;
    //}
185 186 187
    // request.setUrl(QUrl(url));
    // request.setRawHeader("Accept", "*/*");
    // switch (type) {
188 189 190 191 192 193 194 195 196 197 198 199 200
    //    case StatkartTopo:
    //        request.setRawHeader("Referrer", "https://www.norgeskart.no/");
    //        break;
    //    case EniroTopo:
    //        request.setRawHeader("Referrer", "https://www.eniro.se/");
    //        break;
    //    /*
    //    case OpenStreetMapSurfer:
    //    case OpenStreetMapSurferTerrain:
    //        request.setRawHeader("Referrer", "http://www.mapsurfer.net/");
    //        break;
    //    case OpenStreetMap:
    //    case OpenStreetOsm:
201 202
    //        request.setRawHeader("Referrer",
    //        "https://www.openstreetmap.org/"); break;
203 204 205 206 207
    //    */

    //    case EsriWorldStreet:
    //    case EsriWorldSatellite:
    //    case EsriTerrain: {
208 209 210 211
    //            QByteArray token =
    //            qgcApp()->toolbox()->settingsManager()->appSettings()->esriToken()->rawValue().toString().toLatin1();
    //            request.setRawHeader("User-Agent", QByteArrayLiteral("Qt
    //            Location based application"));
212 213 214 215 216 217 218 219 220 221 222
    //            request.setRawHeader("User-Token", token);
    //        }
    //        return request;

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

    //    default:
    //        break;
    //}
223 224
    // request.setRawHeader("User-Agent", _userAgent);
    // return request;
dogmaphobic's avatar
dogmaphobic committed
225 226 227
}

//-----------------------------------------------------------------------------
228
#if 0
dogmaphobic's avatar
dogmaphobic committed
229
QString
230
UrlFactory::_getURL(QString type, int x, int y, int zoom, QNetworkAccessManager* networkManager)
dogmaphobic's avatar
dogmaphobic committed
231 232
{
    switch (type) {
Pierre TILAK's avatar
Pierre TILAK committed
233
    (networkManager);
234 235
    case StatkartTopo:
    {
236
        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);
237 238
    }
    break;
239 240 241 242
    case EniroTopo:
    {
    }
    break;
dogmaphobic's avatar
dogmaphobic committed
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 277 278 279
    /*
    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:
    {
    }
    break;
    case BingSatellite:
    {
    }
    break;
    case BingHybrid:
    {
    }
Gus Grubba's avatar
Gus Grubba committed
280
    /*
dogmaphobic's avatar
dogmaphobic committed
281 282 283 284 285 286 287 288 289 290 291 292
    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
293 294
    */
    case EsriWorldStreet:
dogmaphobic's avatar
dogmaphobic committed
295

Gus Grubba's avatar
Gus Grubba committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309
    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
310
    {
311
        QString mapBoxToken = qgcApp()->toolbox()->settingsManager()->appSettings()->mapboxToken()->rawValue().toString();
dogmaphobic's avatar
dogmaphobic committed
312 313 314
        if(!mapBoxToken.isEmpty()) {
            QString server = "https://api.mapbox.com/v4/";
            switch(type) {
Gus Grubba's avatar
Gus Grubba committed
315
                case MapboxStreets:
dogmaphobic's avatar
dogmaphobic committed
316 317
                    server += "mapbox.streets";
                    break;
Gus Grubba's avatar
Gus Grubba committed
318
                case MapboxLight:
dogmaphobic's avatar
dogmaphobic committed
319 320
                    server += "mapbox.light";
                    break;
Gus Grubba's avatar
Gus Grubba committed
321
                case MapboxDark:
dogmaphobic's avatar
dogmaphobic committed
322 323
                    server += "mapbox.dark";
                    break;
Gus Grubba's avatar
Gus Grubba committed
324
                case MapboxSatellite:
dogmaphobic's avatar
dogmaphobic committed
325 326
                    server += "mapbox.satellite";
                    break;
Gus Grubba's avatar
Gus Grubba committed
327
                case MapboxHybrid:
dogmaphobic's avatar
dogmaphobic committed
328 329
                    server += "mapbox.streets-satellite";
                    break;
Gus Grubba's avatar
Gus Grubba committed
330
                case MapboxWheatPaste:
dogmaphobic's avatar
dogmaphobic committed
331 332
                    server += "mapbox.wheatpaste";
                    break;
Gus Grubba's avatar
Gus Grubba committed
333
                case MapboxStreetsBasic:
dogmaphobic's avatar
dogmaphobic committed
334 335
                    server += "mapbox.streets-basic";
                    break;
Gus Grubba's avatar
Gus Grubba committed
336
                case MapboxComic:
dogmaphobic's avatar
dogmaphobic committed
337 338
                    server += "mapbox.comic";
                    break;
Gus Grubba's avatar
Gus Grubba committed
339
                case MapboxOutdoors:
dogmaphobic's avatar
dogmaphobic committed
340 341
                    server += "mapbox.outdoors";
                    break;
Gus Grubba's avatar
Gus Grubba committed
342
                case MapboxRunBikeHike:
dogmaphobic's avatar
dogmaphobic committed
343 344
                    server += "mapbox.run-bike-hike";
                    break;
Gus Grubba's avatar
Gus Grubba committed
345
                case MapboxPencil:
dogmaphobic's avatar
dogmaphobic committed
346 347
                    server += "mapbox.pencil";
                    break;
Gus Grubba's avatar
Gus Grubba committed
348
                case MapboxPirates:
dogmaphobic's avatar
dogmaphobic committed
349 350
                    server += "mapbox.pirates";
                    break;
Gus Grubba's avatar
Gus Grubba committed
351
                case MapboxEmerald:
dogmaphobic's avatar
dogmaphobic committed
352 353
                    server += "mapbox.emerald";
                    break;
Gus Grubba's avatar
Gus Grubba committed
354
                case MapboxHighContrast:
dogmaphobic's avatar
dogmaphobic committed
355 356 357
                    server += "mapbox.high-contrast";
                    break;
                default:
358
                    return {};
dogmaphobic's avatar
dogmaphobic committed
359 360 361 362 363 364
            }
            server += QString("/%1/%2/%3.jpg80?access_token=%4").arg(zoom).arg(x).arg(y).arg(mapBoxToken);
            return server;
        }
    }
    break;
365 366
    case AirmapElevation:
    {
Andreas Bircher's avatar
Andreas Bircher committed
367 368 369 370
        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);
371 372
    }
    break;
dogmaphobic's avatar
dogmaphobic committed
373

stmoon's avatar
stmoon committed
374 375 376 377 378 379 380 381
    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);

382
        if ( zoom > 19 ) {
383
            return {};
384 385
        }
        else if ( zoom > 5 && x >= x_min && x <= x_max && y >= y_min && y <= y_max ) {
stmoon's avatar
stmoon committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
            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);

405
        if ( zoom > 19 ) {
406
            return {};
407 408
        }
        else if ( zoom > 5 && x >= x_min && x <= x_max && y >= y_min && y <= y_max ) {
stmoon's avatar
stmoon committed
409 410 411 412 413 414 415 416
            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
417 418 419 420
    default:
        qWarning("Unknown map id %d\n", type);
        break;
    }
421
    return {};
dogmaphobic's avatar
dogmaphobic committed
422 423 424 425
}


//-----------------------------------------------------------------------------
dogmaphobic's avatar
dogmaphobic committed
426

427
#endif
dogmaphobic's avatar
dogmaphobic committed
428 429

//-----------------------------------------------------------------------------
430 431 432 433
quint32 UrlFactory::averageSizeForType(QString type) {
    qDebug() << "UrlFactory::averageSizeForType for" << type;
    if (_providersTable.find(type) != _providersTable.end()) {
        return _providersTable[type]->getAverageSize();
434 435 436
    } 
    qDebug() << "UrlFactory::averageSizeForType " << type
        << " Not registered";
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

    //    switch (type) {
    //    case GoogleMap:
    //        return AVERAGE_GOOGLE_STREET_MAP;
    //    case BingMap:
    //        return AVERAGE_BING_STREET_MAP;
    //    case GoogleSatellite:
    //        return AVERAGE_GOOGLE_SAT_MAP;
    //    case MapboxSatellite:
    //        return AVERAGE_MAPBOX_SAT_MAP;
    //    case BingHybrid:
    //    case BingSatellite:
    //        return AVERAGE_BING_SAT_MAP;
    //    case GoogleTerrain:
    //        return AVERAGE_GOOGLE_TERRAIN_MAP;
    //    case MapboxStreets:
    //    case MapboxStreetsBasic:
    //    case MapboxRunBikeHike:
    //        return AVERAGE_MAPBOX_STREET_MAP;
    //    case AirmapElevation:
    //        return AVERAGE_AIRMAP_ELEV_SIZE;
    //    case GoogleLabels:
    //    case MapboxDark:
    //    case MapboxLight:
    //    case MapboxOutdoors:
    //    case MapboxPencil:
    //    case OpenStreetMap:
    //    case GoogleHybrid:
    //    case MapboxComic:
    //    case MapboxEmerald:
    //    case MapboxHighContrast:
    //    case MapboxHybrid:
    //    case MapboxPirates:
    //    case MapboxWheatPaste:
    //    default:
    //        break;
    //    }
474
    return AVERAGE_TILE_SIZE;
dogmaphobic's avatar
dogmaphobic committed
475
}
476

477
QString UrlFactory::getTypeFromId(int id) {
478 479 480

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

481
    while (i.hasNext()) {
482
        i.next();
483
        if (abs(qHash(i.key())) == id) {
484 485 486
            return i.key();
        }
    }
487
    qCDebug(QGCMapUrlEngineLog) << "getTypeFromId : id not found" << id;
488 489 490 491 492 493
    return "";
}

// Todo : qHash produce a uint bigger than max(int)
// There is still a low probability for abs to
// generate similar hash for different types
494
int UrlFactory::getIdFromType(QString type) { return abs(qHash(type)); }