summaryrefslogtreecommitdiffstats
path: root/src/plugins/geoservices/esri/geotiledmappingmanagerengine_esri.cpp
blob: 6cfa3884f2dc1a70a29c03d76b7b98776e2f84a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
// Copyright (C) 2013-2018 Esri <contracts@esri.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "geotiledmappingmanagerengine_esri.h"
#include "geotiledmap_esri.h"
#include "geotilefetcher_esri.h"

#include <QtLocation/private/qgeocameracapabilities_p.h>
#include <QtLocation/private/qgeomaptype_p.h>
#include <QtLocation/private/qgeotiledmap_p.h>
#include <QtLocation/private/qgeofiletilecache_p.h>

#include <QFileInfo>
#include <QDir>
#include <QUrl>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>

QT_BEGIN_NAMESPACE

static const QString kPrefixEsri(QStringLiteral("esri."));
static const QString kParamUserAgent(kPrefixEsri + QStringLiteral("useragent"));
static const QString kParamToken(kPrefixEsri + QStringLiteral("token"));
static const QString kPrefixMapping(kPrefixEsri + QStringLiteral("mapping."));
static const QString kParamMinimumZoomLevel(kPrefixMapping + QStringLiteral("minimumZoomLevel"));
static const QString kParamMaximumZoomLevel(kPrefixMapping + QStringLiteral("maximumZoomLevel"));

static const QString kPropMapSources(QStringLiteral("mapSources"));
static const QString kPropStyle(QStringLiteral("style"));
static const QString kPropName(QStringLiteral("name"));
static const QString kPropDescription(QStringLiteral("description"));
static const QString kPropMobile(QStringLiteral("mobile"));
static const QString kPropNight(QStringLiteral("night"));
static const QString kPropUrl(QStringLiteral("url"));
static const QString kPropMapId(QStringLiteral("mapId"));
static const QString kPropCopyright(QStringLiteral("copyrightText"));

GeoTiledMappingManagerEngineEsri::GeoTiledMappingManagerEngineEsri(const QVariantMap &parameters,
                                                                   QGeoServiceProvider::Error *error,
                                                                   QString *errorString) :
    QGeoTiledMappingManagerEngine()
{
    QGeoCameraCapabilities cameraCaps;

    double minimumZoomLevel = 0;
    double maximumZoomLevel = 19;

    if (parameters.contains(kParamMinimumZoomLevel))
        minimumZoomLevel = parameters[kParamMinimumZoomLevel].toDouble();

    if (parameters.contains(kParamMaximumZoomLevel))
        maximumZoomLevel = parameters[kParamMaximumZoomLevel].toDouble();

    cameraCaps.setMinimumZoomLevel(minimumZoomLevel);
    cameraCaps.setMaximumZoomLevel(maximumZoomLevel);
    cameraCaps.setSupportsBearing(true);
    cameraCaps.setSupportsTilting(true);
    cameraCaps.setMinimumTilt(0);
    cameraCaps.setMaximumTilt(80);
    cameraCaps.setMinimumFieldOfView(20.0);
    cameraCaps.setMaximumFieldOfView(120.0);
    cameraCaps.setOverzoomEnabled(true);
    setCameraCapabilities(cameraCaps);

    setTileSize(QSize(256, 256));

    if (!initializeMapSources(error, errorString, cameraCaps))
        return;

    QList<QGeoMapType> mapTypes;

    for (GeoMapSource *mapSource : m_mapSources) {
        mapTypes << QGeoMapType(
                        mapSource->style(),
                        mapSource->name(),
                        mapSource->description(),
                        mapSource->mobile(),
                        mapSource->night(),
                        mapSource->mapId(),
                        "esri",
                        cameraCaps);
    }

    setSupportedMapTypes(mapTypes);

    GeoTileFetcherEsri *tileFetcher = new GeoTileFetcherEsri(this);

    if (parameters.contains(kParamUserAgent))
        tileFetcher->setUserAgent(parameters.value(kParamUserAgent).toString().toLatin1());

    if (parameters.contains(kParamToken))
        tileFetcher->setToken(parameters.value(kParamToken).toString());

    setTileFetcher(tileFetcher);

    /* TILE CACHE */
    QString cacheDirectory;
    if (parameters.contains(QStringLiteral("esri.mapping.cache.directory"))) {
        cacheDirectory = parameters.value(QStringLiteral("esri.mapping.cache.directory")).toString();
    } else {
        // managerName() is not yet set, we have to hardcode the plugin name below
        cacheDirectory = QAbstractGeoTileCache::baseLocationCacheDirectory() + QLatin1String("esri");
    }
    QGeoFileTileCache *tileCache = new QGeoFileTileCache(cacheDirectory);

    /*
     * Disk cache setup -- defaults to ByteSize (old behavior)
     */
    if (parameters.contains(QStringLiteral("esri.mapping.cache.disk.cost_strategy"))) {
        QString cacheStrategy = parameters.value(QStringLiteral("esri.mapping.cache.disk.cost_strategy")).toString().toLower();
        if (cacheStrategy == QLatin1String("bytesize"))
            tileCache->setCostStrategyDisk(QGeoFileTileCache::ByteSize);
        else
            tileCache->setCostStrategyDisk(QGeoFileTileCache::Unitary);
    } else {
        tileCache->setCostStrategyDisk(QGeoFileTileCache::ByteSize);
    }
    if (parameters.contains(QStringLiteral("esri.mapping.cache.disk.size"))) {
        bool ok = false;
        int cacheSize = parameters.value(QStringLiteral("esri.mapping.cache.disk.size")).toString().toInt(&ok);
        if (ok)
            tileCache->setMaxDiskUsage(cacheSize);
    }

    /*
     * Memory cache setup -- defaults to ByteSize (old behavior)
     */
    if (parameters.contains(QStringLiteral("esri.mapping.cache.memory.cost_strategy"))) {
        QString cacheStrategy = parameters.value(QStringLiteral("esri.mapping.cache.memory.cost_strategy")).toString().toLower();
        if (cacheStrategy == QLatin1String("bytesize"))
            tileCache->setCostStrategyMemory(QGeoFileTileCache::ByteSize);
        else
            tileCache->setCostStrategyMemory(QGeoFileTileCache::Unitary);
    } else {
        tileCache->setCostStrategyMemory(QGeoFileTileCache::ByteSize);
    }
    if (parameters.contains(QStringLiteral("esri.mapping.cache.memory.size"))) {
        bool ok = false;
        int cacheSize = parameters.value(QStringLiteral("esri.mapping.cache.memory.size")).toString().toInt(&ok);
        if (ok)
            tileCache->setMaxMemoryUsage(cacheSize);
    }

    /*
     * Texture cache setup -- defaults to ByteSize (old behavior)
     */
    if (parameters.contains(QStringLiteral("esri.mapping.cache.texture.cost_strategy"))) {
        QString cacheStrategy = parameters.value(QStringLiteral("esri.mapping.cache.texture.cost_strategy")).toString().toLower();
        if (cacheStrategy == QLatin1String("bytesize"))
            tileCache->setCostStrategyTexture(QGeoFileTileCache::ByteSize);
        else
            tileCache->setCostStrategyTexture(QGeoFileTileCache::Unitary);
    } else {
        tileCache->setCostStrategyTexture(QGeoFileTileCache::ByteSize);
    }
    if (parameters.contains(QStringLiteral("esri.mapping.cache.texture.size"))) {
        bool ok = false;
        int cacheSize = parameters.value(QStringLiteral("esri.mapping.cache.texture.size")).toString().toInt(&ok);
        if (ok)
            tileCache->setExtraTextureUsage(cacheSize);
    }

    /* PREFETCHING */
    if (parameters.contains(QStringLiteral("esri.mapping.prefetching_style"))) {
        const QString prefetchingMode = parameters.value(QStringLiteral("esri.mapping.prefetching_style")).toString();
        if (prefetchingMode == QStringLiteral("TwoNeighbourLayers"))
            m_prefetchStyle = QGeoTiledMap::PrefetchTwoNeighbourLayers;
        else if (prefetchingMode == QStringLiteral("OneNeighbourLayer"))
            m_prefetchStyle = QGeoTiledMap::PrefetchNeighbourLayer;
        else if (prefetchingMode == QStringLiteral("NoPrefetching"))
            m_prefetchStyle = QGeoTiledMap::NoPrefetching;
    }

    setTileCache(tileCache);
    *error = QGeoServiceProvider::NoError;
    errorString->clear();
}

GeoTiledMappingManagerEngineEsri::~GeoTiledMappingManagerEngineEsri()
{
    qDeleteAll(m_mapSources);
}

QGeoMap *GeoTiledMappingManagerEngineEsri::createMap()
{
    QGeoTiledMap *map = new GeoTiledMapEsri(this);
    map->setPrefetchStyle(m_prefetchStyle);
    return map;
}

// ${z} = Zoom
// ${x} = X
// ${y} = Y
// ${token} = Token

// template =
// 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{{z}}/{{y}}/{{x}}.png'

bool GeoTiledMappingManagerEngineEsri::initializeMapSources(QGeoServiceProvider::Error *error,
                                                            QString *errorString,
                                                            const QGeoCameraCapabilities &cameraCaps)
{
    QFile mapsFile(":/esri/maps.json");

    if (!mapsFile.open(QIODevice::ReadOnly)) {
        *error = QGeoServiceProvider::NotSupportedError;
        *errorString = Q_FUNC_INFO + QStringLiteral("Unable to open: ") + mapsFile.fileName();

        return false;
    }

    QByteArray mapsData = mapsFile.readAll();
    mapsFile.close();

    QJsonParseError parseError;

    QJsonDocument mapsDocument = QJsonDocument::fromJson(mapsData, &parseError);

    if (!mapsDocument.isObject()) {
        *error = QGeoServiceProvider::NotSupportedError;
        *errorString = QString("%1JSON error: %2, offset: %3, details: %4")
                            .arg(Q_FUNC_INFO)
                            .arg((int)parseError.error)
                            .arg(parseError.offset)
                            .arg(parseError.errorString());
        return false;
    }

    QVariantMap maps = mapsDocument.object().toVariantMap();

    QVariantList mapSources = maps["mapSources"].toList();

    for (const QVariant &mapSourceElement : mapSources) {
        QVariantMap mapSource = mapSourceElement.toMap();

        int mapId = m_mapSources.count() + 1;

        m_mapSources << new GeoMapSource(
                            GeoMapSource::mapStyle(mapSource[kPropStyle].toString()),
                            mapSource[kPropName].toString(),
                            mapSource[kPropDescription].toString(),
                            mapSource[kPropMobile].toBool(),
                            mapSource[kPropMapId].toBool(),
                            mapId,
                            GeoMapSource::toFormat(mapSource[kPropUrl].toString()),
                            mapSource[kPropCopyright].toString(),
                            cameraCaps
                            );
    }

    return true;
}

GeoMapSource *GeoTiledMappingManagerEngineEsri::mapSource(int mapId) const
{
    for (GeoMapSource *mapSource : mapSources()) {
        if (mapSource->mapId() == mapId)
            return mapSource;
    }

    return nullptr;
}

QT_END_NAMESPACE