Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgeotiledmappingmanagerenginemapbox.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 Canonical Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QtLocation/private/qgeocameracapabilities_p.h>
8#include <QtLocation/private/qgeomaptype_p.h>
9#include <QtLocation/private/qgeotiledmap_p.h>
11typedef QGeoTiledMap Map;
12
13QT_BEGIN_NAMESPACE
14
15QGeoTiledMappingManagerEngineMapbox::QGeoTiledMappingManagerEngineMapbox(const QVariantMap &parameters, QGeoServiceProvider::Error *error, QString *errorString)
16: QGeoTiledMappingManagerEngine()
17{
18 QGeoCameraCapabilities cameraCaps;
19 cameraCaps.setMinimumZoomLevel(0.0);
20 cameraCaps.setMaximumZoomLevel(19.0);
21 cameraCaps.setSupportsBearing(true);
22 cameraCaps.setSupportsTilting(true);
23 cameraCaps.setMinimumTilt(0);
24 cameraCaps.setMaximumTilt(80);
25 cameraCaps.setMinimumFieldOfView(20.0);
26 cameraCaps.setMaximumFieldOfView(120.0);
27 cameraCaps.setOverzoomEnabled(true);
28 setCameraCapabilities(cameraCaps);
29
30 setTileSize(QSize(256, 256));
31
32 const QByteArray pluginName = "mapbox";
33 QList<QGeoMapType> mapTypes;
34 // as index 0 to retain compatibility with the current API, that expects the passed map_id to be on by default.
35 if (parameters.contains(QStringLiteral("mapbox.mapping.map_id"))) {
36 const QString name = parameters.value(QStringLiteral("mapbox.mapping.map_id")).toString();
37 mapTypes << QGeoMapType(QGeoMapType::CustomMap, name, name, false, false, mapTypes.size() + 1, pluginName, cameraCaps);
38 } else if (parameters.contains(QStringLiteral("mapbox.map_id"))) { //deprecated
39 const QString name = parameters.value(QStringLiteral("mapbox.map_id")).toString();
40 mapTypes << QGeoMapType(QGeoMapType::CustomMap, name, name, false, false, mapTypes.size() + 1, pluginName, cameraCaps);
41 }
42
43 // As of 2016.06.15, valid mapbox map_ids are documented at https://www.mapbox.com/api-documentation/#maps
44 //: Noun describing map type 'Street map'
45 mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox.streets"), tr("Street"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
46 //: Noun describing type of a map using light colors (weak contrast)
47 mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox.light"), tr("Light"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
48 //: Noun describing type of a map using dark colors
49 mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox.dark"), tr("Dark"), false, true, mapTypes.size() + 1, pluginName, cameraCaps);
50 //: Noun describing type of a map created by satellite
51 mapTypes << QGeoMapType(QGeoMapType::SatelliteMapDay, QStringLiteral("mapbox.satellite"), tr("Satellite"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
52 //: Noun describing type of a street map created by satellite
53 mapTypes << QGeoMapType(QGeoMapType::HybridMap, QStringLiteral("mapbox.streets-satellite"), tr("Streets Satellite"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
54 //: Noun describing type of a map using wheat paste colors
55 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.wheatpaste"), tr("Wheatpaste"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
56 //: Noun describing type of a basic street map
57 mapTypes << QGeoMapType(QGeoMapType::StreetMap, QStringLiteral("mapbox.streets-basic"), tr("Streets Basic"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
58 //: Noun describing type of a map using cartoon-style fonts
59 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.comic"), tr("Comic"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
60 //: Noun describing type of a map for outdoor activities
61 mapTypes << QGeoMapType(QGeoMapType::PedestrianMap, QStringLiteral("mapbox.outdoors"), tr("Outdoors"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
62 //: Noun describing type of a map for sports
63 mapTypes << QGeoMapType(QGeoMapType::CycleMap, QStringLiteral("mapbox.run-bike-hike"), tr("Run Bike Hike"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
64 //: Noun describing type of a map drawn by pencil
65 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.pencil"), tr("Pencil"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
66 //: Noun describing type of a treasure map with pirate boat watermark
67 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.pirates"), tr("Pirates"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
68 //: Noun describing type of a map using emerald colors
69 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.emerald"), tr("Emerald"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
70 //: Noun describing type of a map with high contrast
71 mapTypes << QGeoMapType(QGeoMapType::CustomMap, QStringLiteral("mapbox.high-contrast"), tr("High Contrast"), false, false, mapTypes.size() + 1, pluginName, cameraCaps);
72
73 // New way to specify multiple customized map_ids via additional_map_ids
74 if (parameters.contains(QStringLiteral("mapbox.mapping.additional_map_ids"))) {
75 const QString ids = parameters.value(QStringLiteral("mapbox.mapping.additional_map_ids")).toString();
76 const QStringList idList = ids.split(',', Qt::SkipEmptyParts);
77
78 for (const QString &name: idList) {
79 if (!name.isEmpty())
80 mapTypes << QGeoMapType(QGeoMapType::CustomMap, name, name, false, false, mapTypes.size() + 1, pluginName, cameraCaps);
81 }
82 }
83
84 QList<QString> mapIds;
85 for (const auto &mapType : std::as_const(mapTypes))
86 mapIds.push_back(mapType.name());
87
88 setSupportedMapTypes(mapTypes);
89
90 int scaleFactor = 1;
91 if (parameters.contains(QStringLiteral("mapbox.mapping.highdpi_tiles"))) {
92 const QString param = parameters.value(QStringLiteral("mapbox.mapping.highdpi_tiles")).toString().toLower();
93 if (param == "true")
94 scaleFactor = 2;
95 }
96
97 QGeoTileFetcherMapbox *tileFetcher = new QGeoTileFetcherMapbox(scaleFactor, this);
98 tileFetcher->setMapIds(mapIds);
99
100 if (parameters.contains(QStringLiteral("useragent"))) {
101 const QByteArray ua = parameters.value(QStringLiteral("useragent")).toString().toLatin1();
102 tileFetcher->setUserAgent(ua);
103 }
104 if (parameters.contains(QStringLiteral("mapbox.mapping.format"))) {
105 const QString format = parameters.value(QStringLiteral("mapbox.mapping.format")).toString();
106 tileFetcher->setFormat(format);
107 } else if (parameters.contains(QStringLiteral("mapbox.format"))) { //deprecated
108 const QString format = parameters.value(QStringLiteral("mapbox.format")).toString();
109 tileFetcher->setFormat(format);
110 }
111 if (parameters.contains(QStringLiteral("mapbox.access_token"))) {
112 const QString token = parameters.value(QStringLiteral("mapbox.access_token")).toString();
113 tileFetcher->setAccessToken(token);
114 }
115
116 setTileFetcher(tileFetcher);
117
118 // TODO: do this in a plugin-neutral way so that other tiled map plugins
119 // don't need this boilerplate or hardcode plugin name
120
121 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.directory"))) {
122 m_cacheDirectory = parameters.value(QStringLiteral("mapbox.mapping.cache.directory")).toString();
123 } else {
124 // managerName() is not yet set, we have to hardcode the plugin name below
125 m_cacheDirectory = QAbstractGeoTileCache::baseLocationCacheDirectory() + QLatin1String(pluginName);
126 }
127
128 QGeoFileTileCache *tileCache = new QGeoFileTileCacheMapbox(mapTypes, scaleFactor, m_cacheDirectory);
129
130 /*
131 * Disk cache setup -- defaults to Unitary since:
132 *
133 * The Mapbox free plan allows for 6000 tiles to be stored for offline uses,
134 * As of 2016.06.15, according to https://www.mapbox.com/help/mobile-offline/ .
135 * Thus defaulting to Unitary strategy, and setting 6000 tiles as default cache disk size
136 */
137 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.disk.cost_strategy"))) {
138 QString cacheStrategy = parameters.value(QStringLiteral("mapbox.mapping.cache.disk.cost_strategy")).toString().toLower();
139 if (cacheStrategy == QLatin1String("bytesize"))
140 tileCache->setCostStrategyDisk(QGeoFileTileCache::ByteSize);
141 else
142 tileCache->setCostStrategyDisk(QGeoFileTileCache::Unitary);
143 } else {
144 tileCache->setCostStrategyDisk(QGeoFileTileCache::Unitary);
145 }
146 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.disk.size"))) {
147 bool ok = false;
148 int cacheSize = parameters.value(QStringLiteral("mapbox.mapping.cache.disk.size")).toString().toInt(&ok);
149 if (ok)
150 tileCache->setMaxDiskUsage(cacheSize);
151 } else {
152 if (tileCache->costStrategyDisk() == QGeoFileTileCache::Unitary)
153 tileCache->setMaxDiskUsage(6000); // The maximum allowed with the free tier
154 }
155
156 /*
157 * Memory cache setup -- defaults to ByteSize (old behavior)
158 */
159 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.memory.cost_strategy"))) {
160 QString cacheStrategy = parameters.value(QStringLiteral("mapbox.mapping.cache.memory.cost_strategy")).toString().toLower();
161 if (cacheStrategy == QLatin1String("bytesize"))
162 tileCache->setCostStrategyMemory(QGeoFileTileCache::ByteSize);
163 else
164 tileCache->setCostStrategyMemory(QGeoFileTileCache::Unitary);
165 } else {
166 tileCache->setCostStrategyMemory(QGeoFileTileCache::ByteSize);
167 }
168 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.memory.size"))) {
169 bool ok = false;
170 int cacheSize = parameters.value(QStringLiteral("mapbox.mapping.cache.memory.size")).toString().toInt(&ok);
171 if (ok)
172 tileCache->setMaxMemoryUsage(cacheSize);
173 }
174
175 /*
176 * Texture cache setup -- defaults to ByteSize (old behavior)
177 */
178 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.texture.cost_strategy"))) {
179 QString cacheStrategy = parameters.value(QStringLiteral("mapbox.mapping.cache.texture.cost_strategy")).toString().toLower();
180 if (cacheStrategy == QLatin1String("bytesize"))
181 tileCache->setCostStrategyTexture(QGeoFileTileCache::ByteSize);
182 else
183 tileCache->setCostStrategyTexture(QGeoFileTileCache::Unitary);
184 } else {
185 tileCache->setCostStrategyTexture(QGeoFileTileCache::ByteSize);
186 }
187 if (parameters.contains(QStringLiteral("mapbox.mapping.cache.texture.size"))) {
188 bool ok = false;
189 int cacheSize = parameters.value(QStringLiteral("mapbox.mapping.cache.texture.size")).toString().toInt(&ok);
190 if (ok)
191 tileCache->setExtraTextureUsage(cacheSize);
192 }
193
194 /* PREFETCHING */
195 if (parameters.contains(QStringLiteral("mapbox.mapping.prefetching_style"))) {
196 const QString prefetchingMode = parameters.value(QStringLiteral("mapbox.mapping.prefetching_style")).toString();
197 if (prefetchingMode == QStringLiteral("TwoNeighbourLayers"))
198 m_prefetchStyle = QGeoTiledMap::PrefetchTwoNeighbourLayers;
199 else if (prefetchingMode == QStringLiteral("OneNeighbourLayer"))
200 m_prefetchStyle = QGeoTiledMap::PrefetchNeighbourLayer;
201 else if (prefetchingMode == QStringLiteral("NoPrefetching"))
202 m_prefetchStyle = QGeoTiledMap::NoPrefetching;
203 }
204
205 setTileCache(tileCache);
206
207 *error = QGeoServiceProvider::NoError;
208 errorString->clear();
209}
210
214
216{
217 QGeoTiledMap *map = new Map(this, 0);
218 map->setPrefetchStyle(m_prefetchStyle);
219 return map;
220}
221
222QT_END_NAMESPACE