1/****************************************************************************
2**
3** Copyright (C) 2016 Aaron McCarthy <mccarthy.aaron@gmail.com>
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtFoo module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qplacesearchreplyosm.h"
41#include "qplacemanagerengineosm.h"
42
43#include <QtCore/QJsonDocument>
44#include <QtCore/QJsonArray>
45#include <QtCore/QJsonObject>
46#include <QtNetwork/QNetworkReply>
47#include <QtPositioning/QGeoCircle>
48#include <QtPositioning/QGeoRectangle>
49#include <QtLocation/QPlaceResult>
50#include <QtLocation/QPlaceSearchRequest>
51#include <QtLocation/private/qplacesearchrequest_p.h>
52
53QT_BEGIN_NAMESPACE
54
55QPlaceSearchReplyOsm::QPlaceSearchReplyOsm(const QPlaceSearchRequest &request,
56 QNetworkReply *reply, QPlaceManagerEngineOsm *parent)
57: QPlaceSearchReply(parent)
58{
59 Q_ASSERT(parent);
60 if (!reply) {
61 setError(errorCode: UnknownError, QStringLiteral("Null reply"));
62 return;
63 }
64 setRequest(request);
65
66 connect(sender: reply, SIGNAL(finished()), receiver: this, SLOT(replyFinished()));
67 connect(sender: reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
68 receiver: this, SLOT(networkError(QNetworkReply::NetworkError)));
69 connect(sender: this, signal: &QPlaceReply::aborted, receiver: reply, slot: &QNetworkReply::abort);
70 connect(sender: this, signal: &QObject::destroyed, receiver: reply, slot: &QObject::deleteLater);
71}
72
73QPlaceSearchReplyOsm::~QPlaceSearchReplyOsm()
74{
75}
76
77void QPlaceSearchReplyOsm::setError(QPlaceReply::Error errorCode, const QString &errorString)
78{
79 QPlaceReply::setError(error: errorCode, errorString);
80 emit error(error: errorCode, errorString);
81 setFinished(true);
82 emit finished();
83}
84
85static QGeoRectangle parseBoundingBox(const QJsonArray &coordinates)
86{
87 if (coordinates.count() != 4)
88 return QGeoRectangle();
89
90 double bottom = coordinates.at(i: 0).toString().toDouble();
91 double top = coordinates.at(i: 1).toString().toDouble();
92 double left = coordinates.at(i: 2).toString().toDouble();
93 double right = coordinates.at(i: 3).toString().toDouble();
94
95 return QGeoRectangle(QGeoCoordinate(top, left), QGeoCoordinate(bottom, right));
96}
97
98void QPlaceSearchReplyOsm::replyFinished()
99{
100 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
101 reply->deleteLater();
102
103 if (reply->error() != QNetworkReply::NoError)
104 return;
105
106 QJsonDocument document = QJsonDocument::fromJson(json: reply->readAll());
107 if (!document.isArray()) {
108 setError(errorCode: ParseError, errorString: tr(s: "Response parse error"));
109 return;
110 }
111
112 QJsonArray resultsArray = document.array();
113
114 QGeoCoordinate searchCenter = request().searchArea().center();
115
116 QStringList placeIds;
117
118 QList<QPlaceSearchResult> results;
119 for (int i = 0; i < resultsArray.count(); ++i) {
120 QJsonObject item = resultsArray.at(i).toObject();
121 QPlaceResult pr = parsePlaceResult(item);
122 pr.setDistance(searchCenter.distanceTo(other: pr.place().location().coordinate()));
123 placeIds.append(t: pr.place().placeId());
124 results.append(t: pr);
125 }
126
127 QVariantMap searchContext = request().searchContext().toMap();
128 QStringList excludePlaceIds =
129 searchContext.value(QStringLiteral("ExcludePlaceIds")).toStringList();
130
131 if (!excludePlaceIds.isEmpty()) {
132 QPlaceSearchRequest r = request();
133 QVariantMap parameters = searchContext;
134
135 QStringList epi = excludePlaceIds;
136 epi.removeLast();
137
138 parameters.insert(QStringLiteral("ExcludePlaceIds"), avalue: epi);
139 r.setSearchContext(parameters);
140 QPlaceSearchRequestPrivate *rpimpl = QPlaceSearchRequestPrivate::get(request&: r);
141 rpimpl->related = true;
142 rpimpl->page--;
143 setPreviousPageRequest(r);
144 }
145
146 if (!placeIds.isEmpty()) {
147 QPlaceSearchRequest r = request();
148 QVariantMap parameters = searchContext;
149
150 QStringList epi = excludePlaceIds;
151 epi.append(t: placeIds.join(sep: QLatin1Char(',')));
152
153 parameters.insert(QStringLiteral("ExcludePlaceIds"), avalue: epi);
154 r.setSearchContext(parameters);
155 QPlaceSearchRequestPrivate *rpimpl = QPlaceSearchRequestPrivate::get(request&: r);
156 rpimpl->related = true;
157 rpimpl->page++;
158 setNextPageRequest(r);
159 }
160
161 setResults(results);
162
163 setFinished(true);
164 emit finished();
165}
166
167void QPlaceSearchReplyOsm::networkError(QNetworkReply::NetworkError error)
168{
169 Q_UNUSED(error);
170 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
171 reply->deleteLater();
172 setError(errorCode: QPlaceReply::CommunicationError, errorString: reply->errorString());
173}
174
175QPlaceResult QPlaceSearchReplyOsm::parsePlaceResult(const QJsonObject &item) const
176{
177 QPlace place;
178
179 QGeoCoordinate coordinate = QGeoCoordinate(item.value(QStringLiteral("lat")).toString().toDouble(),
180 item.value(QStringLiteral("lon")).toString().toDouble());
181
182 //const QString placeRank = item.value(QStringLiteral("place_rank")).toString();
183 //const QString category = item.value(QStringLiteral("category")).toString();
184 const QString type = item.value(QStringLiteral("type")).toString();
185 //double importance = item.value(QStringLiteral("importance")).toDouble();
186
187 place.setAttribution(item.value(QStringLiteral("licence")).toString());
188 place.setPlaceId(QString::number(item.value(QStringLiteral("place_id")).toInt()));
189
190 QVariantMap iconParameters;
191 iconParameters.insert(akey: QPlaceIcon::SingleUrl,
192 avalue: QUrl(item.value(QStringLiteral("icon")).toString()));
193 QPlaceIcon icon;
194 icon.setParameters(iconParameters);
195 place.setIcon(icon);
196
197 QJsonObject addressDetails = item.value(QStringLiteral("address")).toObject();
198
199 const QString title = addressDetails.value(key: type).toString();
200
201 place.setName(title);
202
203 if (!requestUrl.isEmpty()) {
204 QPlaceAttribute attribute;
205 attribute.setLabel("requestUrl");
206 attribute.setText(requestUrl);
207 place.setExtendedAttribute(attributeType: "requestUrl", attribute);
208 }
209
210 QGeoAddress address;
211 address.setCity(addressDetails.value(QStringLiteral("city")).toString());
212 address.setCountry(addressDetails.value(QStringLiteral("country")).toString());
213 // FIXME: country_code is alpha-2 setCountryCode takes alpha-3
214 //address.setCountryCode(addressDetails.value(QStringLiteral("country_code")).toString());
215 address.setPostalCode(addressDetails.value(QStringLiteral("postcode")).toString());
216 address.setStreet(addressDetails.value(QStringLiteral("road")).toString());
217 address.setState(addressDetails.value(QStringLiteral("state")).toString());
218 address.setDistrict(addressDetails.value(QStringLiteral("suburb")).toString());
219
220 QGeoLocation location;
221 location.setCoordinate(coordinate);
222 location.setAddress(address);
223 location.setBoundingBox(parseBoundingBox(coordinates: item.value(QStringLiteral("boundingbox")).toArray()));
224
225 place.setLocation(location);
226
227 QPlaceResult result;
228 result.setIcon(icon);
229 result.setPlace(place);
230 result.setTitle(title);
231
232 return result;
233}
234
235QT_END_NAMESPACE
236

source code of qtlocation/src/plugins/geoservices/osm/qplacesearchreplyosm.cpp