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
qgeotilefetcher_nokia.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
10#include "uri_constants.h"
11
12#include <QtLocation/private/qgeotilespec_p.h>
13
14#include <QDebug>
15#include <QSize>
16#include <QDir>
17#include <QUrl>
18#include <QTime>
19
20#include <map>
21
22QT_BEGIN_NAMESPACE
23
24namespace
25{
27 {
28 if (size > 256)
29 return QStringLiteral("512");
30 else if (size > 128)
31 return QStringLiteral("256");
32 else
33 return QStringLiteral("128"); // 128 pixel tiles are deprecated.
34 }
35
36 bool isAerialType(const QString mapScheme)
37 {
38 return mapScheme.startsWith("satellite") || mapScheme.startsWith("hybrid") || mapScheme.startsWith("terrain");
39 }
40}
41QGeoTileFetcherNokia::QGeoTileFetcherNokia(const QVariantMap &parameters,
42 QGeoNetworkAccessManager *networkManager,
44 const QSize &tileSize,
45 int ppi)
46: QGeoTileFetcher(engine), m_engineNokia(engine), m_networkManager(networkManager), m_ppi(ppi), m_copyrightsReply(nullptr),
47 m_baseUriProvider(new QGeoUriProvider(this, parameters, QStringLiteral("here.mapping.host"), MAP_TILES_HOST)),
48 m_aerialUriProvider(new QGeoUriProvider(this, parameters, QStringLiteral("here.mapping.host.aerial"), MAP_TILES_HOST_AERIAL))
49{
50 Q_ASSERT(networkManager);
51 m_tileSize = qMax(tileSize.width(), tileSize.height());
52 m_networkManager->setParent(this);
53
54 m_apiKey = parameters.value(QStringLiteral("here.apiKey")).toString();
55}
56
60
61QGeoTiledMapReply *QGeoTileFetcherNokia::getTileImage(const QGeoTileSpec &spec)
62{
63 // TODO add error detection for if request.connectivityMode() != QGraphicsGeoMap::OnlineMode
64 int ppi = m_ppi;
65 if ((spec.mapId() == 2) || (spec.mapId() == 12) || (spec.mapId() == 21)) {
66 ppi = 72; // HiDpi apparently not supported for these maps
67 } else if ((spec.mapId() >= 7 && spec.mapId() <= 11)
68 || (spec.mapId() == 14)
69 || (spec.mapId() == 16)
70 || (spec.mapId() == 18)
71 || (spec.mapId() == 20)) {
72 ppi = 250; // LoDpi apparently not supported for these maps
73 }
74
75 QString rawRequest = getRequestString(spec, ppi);
76 if (rawRequest.isEmpty()) {
77 return new QGeoTiledMapReply(QGeoTiledMapReply::UnknownError,
78 tr("Mapping manager no longer exists"), this);
79 }
80
81 QNetworkRequest netRequest((QUrl(rawRequest))); // The extra pair of parens disambiguates this from a function declaration
82 netRequest.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
83
84 QNetworkReply *netReply = m_networkManager->get(netRequest);
85
86 QGeoTiledMapReply *mapReply = new QGeoMapReplyNokia(netReply, spec);
87
88 return mapReply;
89}
90
91QString QGeoTileFetcherNokia::getRequestString(const QGeoTileSpec &spec, int ppi) const
92{
93 if (!m_engineNokia)
94 return QString();
95
96 static const QString http("https://");
97 static const QString path("/maptile/2.1/maptile/newest/");
98 static const QChar slash('/');
99
100 QString requestString = http;
101
102 const QString mapScheme = m_engineNokia->getScheme(spec.mapId());
103 if (isAerialType(mapScheme))
104 requestString += m_aerialUriProvider->getCurrentHost();
105 else
106 requestString += m_baseUriProvider->getCurrentHost();
107
108 requestString += path;
109 requestString += mapScheme;
110 requestString += slash;
111 requestString += QString::number(spec.zoom());
112 requestString += slash;
113 requestString += QString::number(spec.x());
114 requestString += slash;
115 requestString += QString::number(spec.y());
116 requestString += slash;
117 requestString += ((ppi > 72)) ? sizeToStr(m_tileSize * 2) : sizeToStr(m_tileSize);
118 static const QString slashpng("/png8");
119 requestString += slashpng;
120
121 if (!m_apiKey.isEmpty()) { // TODO: remove the if
122 requestString += "?apiKey=";
123 requestString += m_apiKey;
124 }
125
126 requestString += "&ppi=" + QString::number(ppi);
127
128 requestString += "&lg=";
129 requestString += getLanguageString();
130 return requestString;
131}
132
133QString QGeoTileFetcherNokia::getLanguageString() const
134{
135 if (!m_engineNokia)
136 return QStringLiteral("ENG");
137
138 QLocale locale = m_engineNokia.data()->locale();
139
140 // English is the default, where no ln is specified. We hardcode the languages
141 // here even though the entire list is updated automagically from the server.
142 // The current languages are Arabic, Chinese, Simplified Chinese, English
143 // French, German, Italian, Polish, Russian and Spanish. The default is English.
144 // These are actually available from the same host under the URL: /maptiler/v2/info
145
146 switch (locale.language()) {
147 case QLocale::Arabic:
148 return QStringLiteral("ARA");
149 case QLocale::Chinese:
150 if (locale.script() == QLocale::TraditionalChineseScript)
151 return QStringLiteral("CHI");
152 else
153 return QStringLiteral("CHT");
154 case QLocale::Dutch:
155 return QStringLiteral("DUT");
156 case QLocale::French:
157 return QStringLiteral("FRE");
158 case QLocale::German:
159 return QStringLiteral("GER");
160 case QLocale::Gaelic:
161 return QStringLiteral("GLE");
162 case QLocale::Greek:
163 return QStringLiteral("GRE");
164 case QLocale::Hebrew:
165 return QStringLiteral("HEB");
166 case QLocale::Hindi:
167 return QStringLiteral("HIN");
168 case QLocale::Indonesian:
169 return QStringLiteral("IND");
170 case QLocale::Italian:
171 return QStringLiteral("ITA");
172 case QLocale::Persian:
173 return QStringLiteral("PER");
174 case QLocale::Polish:
175 return QStringLiteral("POL");
176 case QLocale::Portuguese:
177 return QStringLiteral("POR");
178 case QLocale::Russian:
179 return QStringLiteral("RUS");
180 case QLocale::Sinhala:
181 return QStringLiteral("SIN");
182 case QLocale::Spanish:
183 return QStringLiteral("SPA");
184 case QLocale::Thai:
185 return QStringLiteral("THA");
186 case QLocale::Turkish:
187 return QStringLiteral("TUR");
188 case QLocale::Ukrainian:
189 return QStringLiteral("UKR");
190 case QLocale::Urdu:
191 return QStringLiteral("URD");
192 case QLocale::Vietnamese:
193 return QStringLiteral("VIE");
194
195 default:
196 return QStringLiteral("ENG");
197 }
198 // No "lg" param means that we want English.
199}
200
202{
203 return m_apiKey;
204}
205
206void QGeoTileFetcherNokia::copyrightsFetched()
207{
208 if (m_engineNokia && m_copyrightsReply->error() == QNetworkReply::NoError) {
209 QMetaObject::invokeMethod(m_engineNokia.data(),
210 "loadCopyrightsDescriptorsFromJson",
211 Qt::QueuedConnection,
212 Q_ARG(QByteArray, m_copyrightsReply->readAll()));
213 }
214
215 m_copyrightsReply->deleteLater();
216}
217
219{
220 if (m_engineNokia && m_versionReply->error() == QNetworkReply::NoError) {
221 QMetaObject::invokeMethod(m_engineNokia.data(),
222 "parseNewVersionInfo",
223 Qt::QueuedConnection,
224 Q_ARG(QByteArray, m_versionReply->readAll()));
225 }
226
227 m_versionReply->deleteLater();
228}
229
231{
232 QString copyrightUrl = QStringLiteral("https://");
233
234 copyrightUrl += m_baseUriProvider->getCurrentHost();
235 copyrightUrl += QStringLiteral("/maptile/2.1/copyright/newest?output=json");
236
237 if (!apiKey().isEmpty()) {
238 copyrightUrl += QStringLiteral("&apiKey=");
239 copyrightUrl += apiKey();
240 }
241
242 QNetworkRequest netRequest((QUrl(copyrightUrl)));
243 m_copyrightsReply = m_networkManager->get(netRequest);
244 if (m_copyrightsReply->error() != QNetworkReply::NoError) {
245 qWarning() << __FUNCTION__ << m_copyrightsReply->errorString();
246 m_copyrightsReply->deleteLater();
247 return;
248 }
249
250 if (m_copyrightsReply->isFinished()) {
251 copyrightsFetched();
252 } else {
253 connect(m_copyrightsReply, &QNetworkReply::finished,
254 this, &QGeoTileFetcherNokia::copyrightsFetched);
255 }
256}
257
259{
260 QString versionUrl = QStringLiteral("https://");
261
262 versionUrl += m_baseUriProvider->getCurrentHost();
263 versionUrl += QStringLiteral("/maptile/2.1/version");
264
265 if (!apiKey().isEmpty()) {
266 versionUrl += QStringLiteral("?apiKey=");
267 versionUrl += apiKey();
268 }
269
270 QNetworkRequest netRequest((QUrl(versionUrl)));
271 m_versionReply = m_networkManager->get(netRequest);
272
273 if (m_versionReply->error() != QNetworkReply::NoError) {
274 qWarning() << __FUNCTION__ << m_versionReply->errorString();
275 m_versionReply->deleteLater();
276 return;
277 }
278
279 if (m_versionReply->isFinished())
281 else
282 connect(m_versionReply, &QNetworkReply::finished,
283 this, &QGeoTileFetcherNokia::versionFetched);
284}
285
286QT_END_NAMESPACE
QGeoTiledMapReply * getTileImage(const QGeoTileSpec &spec) override
bool isAerialType(const QString mapScheme)
QString sizeToStr(int size)