summaryrefslogtreecommitdiffstats
path: root/src/plugins/geoservices/nokia/qgeocodingmanagerengine_nokia.cpp
blob: 62e74a2dbcee8193745b39baa1f0787e1c9701c2 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
// Copyright (C) 2015 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qgeocodingmanagerengine_nokia.h"
#include "qgeocodereply_nokia.h"
#include "marclanguagecodes.h"
#include "qgeonetworkaccessmanager.h"
#include "qgeouriprovider.h"
#include "uri_constants.h"

#include <QtPositioning/QGeoAddress>
#include <QtPositioning/QGeoCoordinate>
#include <QtPositioning/QGeoCircle>
#include <QtPositioning/QGeoRectangle>
#include <QtPositioning/QGeoShape>

#include <QUrl>
#include <QMap>
#include <QStringList>

QT_BEGIN_NAMESPACE

QGeoCodingManagerEngineNokia::QGeoCodingManagerEngineNokia(
        QGeoNetworkAccessManager *networkManager,
        const QVariantMap &parameters,
        QGeoServiceProvider::Error *error,
        QString *errorString)
        : QGeoCodingManagerEngine(parameters)
        , m_networkManager(networkManager)
        , m_uriProvider(new QGeoUriProvider(this, parameters, QStringLiteral("here.geocoding.host"), GEOCODING_HOST))
        , m_reverseGeocodingUriProvider(new QGeoUriProvider(this, parameters, QStringLiteral("here.reversegeocoding.host"), REVERSE_GEOCODING_HOST))
{
    Q_ASSERT(networkManager);
    m_networkManager->setParent(this);

    if (parameters.contains(QStringLiteral("here.apiKey")))
        m_apiKey = parameters.value(QStringLiteral("here.apiKey")).toString();

    if (error)
        *error = QGeoServiceProvider::NoError;

    if (errorString)
        *errorString = "";
}

QGeoCodingManagerEngineNokia::~QGeoCodingManagerEngineNokia() {}

QString QGeoCodingManagerEngineNokia::getAuthenticationString() const
{
    QString authenticationString;

    if (!m_apiKey.isEmpty()) {
        authenticationString += "?apiKey=";
        authenticationString += m_apiKey;
    }

    return authenticationString;
}


QGeoCodeReply *QGeoCodingManagerEngineNokia::geocode(const QGeoAddress &address,
                                                     const QGeoShape &bounds)
{
    QString requestString = "https://";
    requestString += m_uriProvider->getCurrentHost();
    requestString += "/6.2/geocode.json";

    requestString += getAuthenticationString();
    requestString += "&gen=9";

    requestString += "&language=";
    requestString += languageToMarc(locale().language());

    bool manualBoundsRequired = false;
    if (bounds.type() == QGeoShape::UnknownType) {
        manualBoundsRequired = true;
    } else if (bounds.type() == QGeoShape::CircleType) {
        QGeoCircle circ(bounds);
        if (circ.isValid()) {
            requestString += "?prox=";
            requestString += trimDouble(circ.center().latitude());
            requestString += ",";
            requestString += trimDouble(circ.center().longitude());
            requestString += ",";
            requestString += trimDouble(circ.radius());
        }
    } else {
        QGeoRectangle rect = bounds.boundingGeoRectangle();
        if (rect.isValid()) {
            requestString += "&bbox=";
            requestString += trimDouble(rect.topLeft().latitude());
            requestString += ",";
            requestString += trimDouble(rect.topLeft().longitude());
            requestString += ";";
            requestString += trimDouble(rect.bottomRight().latitude());
            requestString += ",";
            requestString += trimDouble(rect.bottomRight().longitude());
        }
    }

    if (address.country().isEmpty()) {
        QStringList parts;

        if (!address.state().isEmpty())
            parts << address.state();

        if (!address.city().isEmpty())
            parts << address.city();

        if (!address.postalCode().isEmpty())
            parts << address.postalCode();

        if (!address.street().isEmpty())
            parts << address.street();

        requestString += "&searchtext=";
        requestString += parts.join("+").replace(' ', '+');
    } else {
        requestString += "&country=";
        requestString += address.country();

        if (!address.state().isEmpty()) {
            requestString += "&state=";
            requestString += address.state();
        }

        if (!address.city().isEmpty()) {
            requestString += "&city=";
            requestString += address.city();
        }

        if (!address.postalCode().isEmpty()) {
            requestString += "&postalcode=";
            requestString += address.postalCode();
        }

        if (!address.street().isEmpty()) {
            requestString += "&street=";
            requestString += address.street();
        }
    }

    return geocode(requestString, bounds, manualBoundsRequired);
}

QGeoCodeReply *QGeoCodingManagerEngineNokia::geocode(const QString &address,
                                                     int limit,
                                                     int offset,
                                                     const QGeoShape &bounds)
{
    QString requestString = "https://";
    requestString += m_uriProvider->getCurrentHost();
    requestString += "/6.2/geocode.json";

    requestString += getAuthenticationString();
    requestString += "&gen=9";

    requestString += "&language=";
    requestString += languageToMarc(locale().language());

    requestString += "&searchtext=";
    requestString += QString(address).replace(' ', '+');

    if (limit > 0) {
        requestString += "&maxresults=";
        requestString += QString::number(limit);
    }
    if (offset > 0) {
        // We cannot do this precisely, since HERE doesn't allow
        // precise result-set offset to be supplied; instead, it
        // returns "pages" of results at a time.
        // So, we tell HERE which page of results we want, and the
        // client has to filter out duplicates if they changed
        // the limit param since the last call.
        requestString += "&pageinformation=";
        requestString += QString::number(offset/limit);
    }

    bool manualBoundsRequired = false;
    if (bounds.type() == QGeoShape::RectangleType) {
        QGeoRectangle rect(bounds);
        if (rect.isValid()) {
            requestString += "&bbox=";
            requestString += trimDouble(rect.topLeft().latitude());
            requestString += ",";
            requestString += trimDouble(rect.topLeft().longitude());
            requestString += ";";
            requestString += trimDouble(rect.bottomRight().latitude());
            requestString += ",";
            requestString += trimDouble(rect.bottomRight().longitude());
        }
    } else if (bounds.type() == QGeoShape::CircleType) {
        QGeoCircle circ(bounds);
        if (circ.isValid()) {
            requestString += "?prox=";
            requestString += trimDouble(circ.center().latitude());
            requestString += ",";
            requestString += trimDouble(circ.center().longitude());
            requestString += ",";
            requestString += trimDouble(circ.radius());
        }
    } else {
        manualBoundsRequired = true;
    }

    return geocode(requestString, bounds, manualBoundsRequired, limit, offset);
}

QGeoCodeReply *QGeoCodingManagerEngineNokia::geocode(QString requestString,
                                                     const QGeoShape &bounds,
                                                     bool manualBoundsRequired,
                                                     int limit,
                                                     int offset)
{
    QGeoCodeReplyNokia *reply = new QGeoCodeReplyNokia(
                m_networkManager->get(QNetworkRequest(QUrl(requestString))),
                limit, offset, bounds, manualBoundsRequired, this);

    connect(reply, &QGeoCodeReplyNokia::finished,
            this, &QGeoCodingManagerEngineNokia::placesFinished);

    connect(reply, &QGeoCodeReplyNokia::errorOccurred,
            this, &QGeoCodingManagerEngineNokia::placesError);

    return reply;
}

QGeoCodeReply *QGeoCodingManagerEngineNokia::reverseGeocode(const QGeoCoordinate &coordinate,
                                                            const QGeoShape &bounds)
{
    QString requestString = "https://";
    requestString += m_reverseGeocodingUriProvider->getCurrentHost();
    requestString += "/6.2/reversegeocode.json";

    requestString += getAuthenticationString();
    requestString += "&gen=9";

    requestString += "&mode=retrieveAddresses";

    requestString += "&prox=";
    requestString += trimDouble(coordinate.latitude());
    requestString += ",";
    requestString += trimDouble(coordinate.longitude());

    bool manualBoundsRequired = false;
    if (bounds.type() == QGeoShape::CircleType) {
        QGeoCircle circ(bounds);
        if (circ.isValid() && circ.center() == coordinate) {
            requestString += ",";
            requestString += trimDouble(circ.radius());
        } else {
            manualBoundsRequired = true;
        }
    } else {
        manualBoundsRequired = true;
    }

    requestString += "&language=";
    requestString += languageToMarc(locale().language());

    return geocode(requestString, bounds, manualBoundsRequired);
}

QString QGeoCodingManagerEngineNokia::trimDouble(double degree, int decimalDigits)
{
    QString sDegree = QString::number(degree, 'g', decimalDigits);

    int index = sDegree.indexOf('.');

    if (index == -1)
        return sDegree;
    else
        return QString::number(degree, 'g', decimalDigits + index);
}

void QGeoCodingManagerEngineNokia::placesFinished()
{
    QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());

    if (!reply)
        return;

    if (receivers(SIGNAL(finished(QGeoCodeReply*))) == 0) {
        reply->deleteLater();
        return;
    }

    emit finished(reply);
}

void QGeoCodingManagerEngineNokia::placesError(QGeoCodeReply::Error error, const QString &errorString)
{
    QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());

    if (!reply)
        return;

    if (receivers(SIGNAL(errorOccurred(QGeoCodeReply*,QGeoCodeReply::Error,QString))) == 0) {
        reply->deleteLater();
        return;
    }

    emit errorOccurred(reply, error, errorString);
}

QString QGeoCodingManagerEngineNokia::languageToMarc(QLocale::Language language)
{
    uint offset = 3 * (uint(language));
    if (language == QLocale::C || offset + 3 > sizeof(marc_language_code_list))
        return QLatin1String("eng");

    const unsigned char *c = marc_language_code_list + offset;
    if (c[0] == 0)
        return QLatin1String("eng");

    QString code(3, Qt::Uninitialized);
    code[0] = ushort(c[0]);
    code[1] = ushort(c[1]);
    code[2] = ushort(c[2]);

    return code;
}

QT_END_NAMESPACE