summaryrefslogtreecommitdiff
path: root/src/plugins/geoservices/esri/placemanagerengine_esri.cpp
blob: 525ec7859f53cbe3d9be2c350190ad8207c7c6bb (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/****************************************************************************
**
** Copyright (C) 2013-2018 Esri <contracts@esri.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "placemanagerengine_esri.h"
#include "placesearchreply_esri.h"
#include "placecategoriesreply_esri.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

#include <QtCore/QUrlQuery>

QT_BEGIN_NAMESPACE

// https://developers.arcgis.com/rest/geocode/api-reference/geocoding-find-address-candidates.htm
// https://developers.arcgis.com/rest/geocode/api-reference/geocoding-category-filtering.htm
// https://developers.arcgis.com/rest/geocode/api-reference/geocoding-service-output.htm

static const QString kCategoriesKey(QStringLiteral("categories"));
static const QString kSingleLineKey(QStringLiteral("singleLine"));
static const QString kLocationKey(QStringLiteral("location"));
static const QString kNameKey(QStringLiteral("name"));
static const QString kOutFieldsKey(QStringLiteral("outFields"));
static const QString kCandidateFieldsKey(QStringLiteral("candidateFields"));
static const QString kCountriesKey(QStringLiteral("detailedCountries"));
static const QString kLocalizedNamesKey(QStringLiteral("localizedNames"));
static const QString kMaxLocationsKey(QStringLiteral("maxLocations"));

static const QUrl kUrlGeocodeServer("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer?f=pjson");
static const QUrl kUrlFindAddressCandidates("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates");

PlaceManagerEngineEsri::PlaceManagerEngineEsri(const QVariantMap &parameters, QGeoServiceProvider::Error *error,
                                               QString *errorString) :
    QPlaceManagerEngine(parameters),
    m_networkManager(new QNetworkAccessManager(this))
{
    *error = QGeoServiceProvider::NoError;
    errorString->clear();
}

PlaceManagerEngineEsri::~PlaceManagerEngineEsri()
{
}

QList<QLocale> PlaceManagerEngineEsri::locales() const
{
    return m_locales;
}

void PlaceManagerEngineEsri::setLocales(const QList<QLocale> &locales)
{
    m_locales = locales;
}

/***** Search *****/

QPlaceSearchReply *PlaceManagerEngineEsri::search(const QPlaceSearchRequest &request)
{
    bool unsupported = false;

    // Only public visibility supported
    unsupported |= request.visibilityScope() != QLocation::UnspecifiedVisibility &&
            request.visibilityScope() != QLocation::PublicVisibility;
    unsupported |= request.searchTerm().isEmpty() && request.categories().isEmpty();

    if (unsupported)
        return QPlaceManagerEngine::search(request);

    QUrlQuery queryItems;
    queryItems.addQueryItem(QStringLiteral("f"), QStringLiteral("json"));

    const QGeoCoordinate center = request.searchArea().center();
    if (center.isValid())
    {
        const QString location = QString("%1,%2").arg(center.longitude()).arg(center.latitude());
        queryItems.addQueryItem(kLocationKey, location);
    }

    const QGeoRectangle boundingBox = request.searchArea().boundingGeoRectangle();
    if (!boundingBox.isEmpty())
    {
        const QString searchExtent = QString("%1,%2,%3,%4")
                .arg(boundingBox.topLeft().longitude())
                .arg(boundingBox.topLeft().latitude())
                .arg(boundingBox.bottomRight().longitude())
                .arg(boundingBox.bottomRight().latitude());
        queryItems.addQueryItem(QStringLiteral("searchExtent"), searchExtent);
    }

    if (!request.searchTerm().isEmpty())
        queryItems.addQueryItem(kSingleLineKey, request.searchTerm());

    QStringList categories;
    if (!request.categories().isEmpty())
    {
        foreach (const QPlaceCategory &placeCategory, request.categories())
            categories.append(placeCategory.categoryId());
        queryItems.addQueryItem("category", categories.join(","));
    }

    if (request.limit() > 0)
        queryItems.addQueryItem(kMaxLocationsKey, QString::number(request.limit()));

    queryItems.addQueryItem(kOutFieldsKey, QStringLiteral("*"));

    QUrl requestUrl(kUrlFindAddressCandidates);
    requestUrl.setQuery(queryItems);

    QNetworkRequest networkRequest(requestUrl);
    networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
    QNetworkReply *networkReply = m_networkManager->get(networkRequest);

    PlaceSearchReplyEsri *reply = new PlaceSearchReplyEsri(request, networkReply, m_candidateFieldsLocale, m_countriesLocale, this);
    connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    connect(reply, SIGNAL(error(QPlaceReply::Error,QString)), this, SLOT(replyError(QPlaceReply::Error,QString)));

    return reply;
}

void PlaceManagerEngineEsri::replyFinished()
{
    QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
    if (reply)
        emit finished(reply);
}

void PlaceManagerEngineEsri::replyError(QPlaceReply::Error errorCode, const QString &errorString)
{
    QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
    if (reply)
        emit error(reply, errorCode, errorString);
}

/***** Categories *****/

QPlaceReply *PlaceManagerEngineEsri::initializeCategories()
{
    initializeGeocodeServer();

    PlaceCategoriesReplyEsri *reply = new PlaceCategoriesReplyEsri(this);
    connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
    connect(reply, SIGNAL(error(QPlaceReply::Error,QString)), this, SLOT(replyError(QPlaceReply::Error,QString)));

    // TODO delayed finished() emission
    if (!m_categories.isEmpty())
        reply->emitFinished();

    m_pendingCategoriesReply.append(reply);
    return reply;
}

void PlaceManagerEngineEsri::parseCategories(const QJsonArray &jsonArray, const QString &parentCategoryId)
{
    foreach (const QJsonValue &jsonValue, jsonArray)
    {
        if (!jsonValue.isObject())
            continue;

        const QJsonObject jsonCategory = jsonValue.toObject();
        const QString key = jsonCategory.value(kNameKey).toString();
        const QString localeName = localizedName(jsonCategory);

        if (key.isEmpty())
            continue;

        QPlaceCategory category;
        category.setCategoryId(key);
        category.setName(localeName.isEmpty() ? key : localeName); // localizedNames
        m_categories.insert(key, category);
        m_subcategories[parentCategoryId].append(key);
        m_parentCategory.insert(key, parentCategoryId);
        emit categoryAdded(category, parentCategoryId);

        if (jsonCategory.contains(kCategoriesKey))
        {
            const QJsonArray jsonArray = jsonCategory.value(kCategoriesKey).toArray();
            parseCategories(jsonArray, key);
        }
    }
}

QString PlaceManagerEngineEsri::parentCategoryId(const QString &categoryId) const
{
    return m_parentCategory.value(categoryId);
}

QStringList PlaceManagerEngineEsri::childCategoryIds(const QString &categoryId) const
{
    return m_subcategories.value(categoryId);
}

QPlaceCategory PlaceManagerEngineEsri::category(const QString &categoryId) const
{
    return m_categories.value(categoryId);
}

QList<QPlaceCategory> PlaceManagerEngineEsri::childCategories(const QString &parentId) const
{
    QList<QPlaceCategory> categories;
    foreach (const QString &id, m_subcategories.value(parentId))
        categories.append(m_categories.value(id));
    return categories;
}

void PlaceManagerEngineEsri::finishCategories()
{
    foreach (PlaceCategoriesReplyEsri *reply, m_pendingCategoriesReply)
        reply->emitFinished();
    m_pendingCategoriesReply.clear();
}

void PlaceManagerEngineEsri::errorCaterogies(const QString &error)
{
    foreach (PlaceCategoriesReplyEsri *reply, m_pendingCategoriesReply)
        reply->setError(QPlaceReply::CommunicationError, error);
}

/***** GeocodeServer *****/

void PlaceManagerEngineEsri::initializeGeocodeServer()
{
    // Only fetch categories once
    if (m_categories.isEmpty() && !m_geocodeServerReply)
    {
        m_geocodeServerReply = m_networkManager->get(QNetworkRequest(kUrlGeocodeServer));
        connect(m_geocodeServerReply, SIGNAL(finished()), this, SLOT(geocodeServerReplyFinished()));
        connect(m_geocodeServerReply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(geocodeServerReplyError()));
    }
}

QString PlaceManagerEngineEsri::localizedName(const QJsonObject &jsonObject)
{
    const QJsonObject localizedNames = jsonObject.value(kLocalizedNamesKey).toObject();

    foreach (const QLocale &locale, m_locales)
    {
        const QString localeStr = locale.name();
        if (localizedNames.contains(localeStr))
        {
            return localizedNames.value(localeStr).toString();
        }

        const QString shortLocale = localeStr.left(2);
        if (localizedNames.contains(shortLocale))
        {
            return localizedNames.value(shortLocale).toString();
        }
    }
    return QString();
}

void PlaceManagerEngineEsri::parseCandidateFields(const QJsonArray &jsonArray)
{
    foreach (const QJsonValue &jsonValue, jsonArray)
    {
        if (!jsonValue.isObject())
            continue;

        const QJsonObject jsonCandidateField = jsonValue.toObject();
        if (!jsonCandidateField.contains(kLocalizedNamesKey))
            continue;

        const QString key = jsonCandidateField.value(kNameKey).toString();
        m_candidateFieldsLocale.insert(key, localizedName(jsonCandidateField));
    }
}

void PlaceManagerEngineEsri::parseCountries(const QJsonArray &jsonArray)
{
    foreach (const QJsonValue &jsonValue, jsonArray)
    {
        if (!jsonValue.isObject())
            continue;

        const QJsonObject jsonCountry = jsonValue.toObject();
        if (!jsonCountry.contains(kLocalizedNamesKey))
            continue;

        const QString key = jsonCountry.value(kNameKey).toString();
        m_countriesLocale.insert(key, localizedName(jsonCountry));
    }
}

void PlaceManagerEngineEsri::geocodeServerReplyFinished()
{
    if (!m_geocodeServerReply)
        return;

    QJsonDocument document = QJsonDocument::fromJson(m_geocodeServerReply->readAll());
    if (!document.isObject())
    {
        errorCaterogies(m_geocodeServerReply->errorString());
        return;
    }

    QJsonObject jsonObject = document.object();

    // parse categories
    if (jsonObject.contains(kCategoriesKey))
    {
        const QJsonArray jsonArray = jsonObject.value(kCategoriesKey).toArray();
        parseCategories(jsonArray, QString());
    }

    // parse candidateFields
    if (jsonObject.contains(kCandidateFieldsKey))
    {
        const QJsonArray jsonArray = jsonObject.value(kCandidateFieldsKey).toArray();
        parseCandidateFields(jsonArray);
    }

    // parse countries
    if (jsonObject.contains(kCountriesKey))
    {
        const QJsonArray jsonArray = jsonObject.value(kCountriesKey).toArray();
        parseCountries(jsonArray);
    }

    finishCategories();

    m_geocodeServerReply->deleteLater();
}

void PlaceManagerEngineEsri::geocodeServerReplyError()
{
    if (m_categories.isEmpty() && !m_geocodeServerReply)
        return;

    errorCaterogies(m_geocodeServerReply->errorString());
}

QT_END_NAMESPACE