1 /****************************************************************************
2 **
3 ** Copyright (C) 2013-2018 Esri <contracts@esri.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation 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 "geocodereply_esri.h"
41 
42 #include <QJsonDocument>
43 #include <QJsonObject>
44 #include <QJsonArray>
45 #include <QGeoCoordinate>
46 #include <QGeoAddress>
47 #include <QGeoLocation>
48 #include <QGeoRectangle>
49 
50 QT_BEGIN_NAMESPACE
51 
GeoCodeReplyEsri(QNetworkReply * reply,OperationType operationType,QObject * parent)52 GeoCodeReplyEsri::GeoCodeReplyEsri(QNetworkReply *reply, OperationType operationType,
53                                    QObject *parent) :
54     QGeoCodeReply(parent), m_operationType(operationType)
55 {
56     if (!reply) {
57         setError(UnknownError, QStringLiteral("Null reply"));
58         return;
59     }
60     connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished()));
61     connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
62             this, SLOT(networkReplyError(QNetworkReply::NetworkError)));
63     connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
64     connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
65 
66     setLimit(1);
67     setOffset(0);
68 }
69 
~GeoCodeReplyEsri()70 GeoCodeReplyEsri::~GeoCodeReplyEsri()
71 {
72 }
73 
networkReplyError(QNetworkReply::NetworkError error)74 void GeoCodeReplyEsri::networkReplyError(QNetworkReply::NetworkError error)
75 {
76     Q_UNUSED(error);
77     QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
78     reply->deleteLater();
79     setError(QGeoCodeReply::CommunicationError, reply->errorString());
80 }
81 
networkReplyFinished()82 void GeoCodeReplyEsri::networkReplyFinished()
83 {
84     QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
85     reply->deleteLater();
86 
87     if (reply->error() != QNetworkReply::NoError)
88         return;
89 
90     QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
91 
92     if (document.isObject()) {
93         QJsonObject object = document.object();
94 
95         switch (operationType()) {
96         case Geocode:
97         {
98             QJsonArray candidates = object.value(QStringLiteral("candidates")).toArray();
99 
100             QList<QGeoLocation> locations;
101 
102             for (int i = 0; i < candidates.count(); i++) {
103                 if (!candidates.at(i).isObject())
104                     continue;
105 
106                 QJsonObject candidate = candidates.at(i).toObject();
107 
108                 QGeoLocation location = parseCandidate(candidate);
109                 locations.append(location);
110             }
111 
112             setLocations(locations);
113             setFinished(true);
114         }
115             break;
116 
117         case ReverseGeocode:
118         {
119             QGeoLocation location = parseAddress(object);
120 
121             QList<QGeoLocation> locations;
122             locations.append(location);
123 
124             setLocations(locations);
125             setFinished(true);
126         }
127             break;
128         }
129 
130     } else {
131         setError(QGeoCodeReply::CommunicationError, QStringLiteral("Unknown document"));
132     }
133 }
134 
parseAddress(const QJsonObject & object)135 QGeoLocation GeoCodeReplyEsri::parseAddress(const QJsonObject& object)
136 {
137     QJsonObject addressObject = object.value(QStringLiteral("address")).toObject();
138 
139     QGeoAddress address;
140 
141     address.setCountryCode(addressObject.value(QStringLiteral("CountryCode")).toString());
142     address.setState(addressObject.value(QStringLiteral("Region")).toString());
143     address.setCity(addressObject.value(QStringLiteral("City")).toString());
144     address.setDistrict(addressObject.value(QStringLiteral("Subregion")).toString());
145     address.setPostalCode(addressObject.value(QStringLiteral("Postal")).toString());
146     address.setStreet(addressObject.value(QStringLiteral("Address")).toString());
147 
148     QGeoCoordinate coordinate;
149 
150     QJsonObject locationObject = object.value(QStringLiteral("location")).toObject();
151 
152     coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
153     coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());
154 
155     QGeoLocation location;
156 
157     location.setCoordinate(coordinate);
158     location.setAddress(address);
159 
160     return location;
161 }
162 
parseCandidate(const QJsonObject & candidate)163 QGeoLocation GeoCodeReplyEsri::parseCandidate(const QJsonObject& candidate)
164 {
165     QGeoCoordinate coordinate;
166 
167     QJsonObject locationObject = candidate.value(QStringLiteral("location")).toObject();
168 
169     coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
170     coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());
171 
172     QGeoRectangle extent;
173 
174     if (candidate.contains(QStringLiteral("extent"))) {
175         QJsonObject extentObject = candidate.value(QStringLiteral("extent")).toObject();
176 
177         extent.setTopLeft(QGeoCoordinate(extentObject.value(QStringLiteral("ymin")).toDouble(),
178                                          extentObject.value(QStringLiteral("xmin")).toDouble()));
179 
180         extent.setBottomRight(QGeoCoordinate(extentObject.value(QStringLiteral("ymax")).toDouble(),
181                                              extentObject.value(QStringLiteral("xmax")).toDouble()));
182     }
183 
184     QJsonObject attributesObject = candidate.value(QStringLiteral("attributes")).toObject();
185 
186     QGeoAddress address;
187 
188     address.setText(candidate.value(QStringLiteral("address")).toString());
189 
190     address.setCountry(attributesObject.value(QStringLiteral("Country")).toString());
191     address.setCountryCode(attributesObject.value(QStringLiteral("Country")).toString());
192     address.setState(attributesObject.value(QStringLiteral("Region")).toString());
193     address.setCity(attributesObject.value(QStringLiteral("City")).toString());
194     address.setDistrict(attributesObject.value(QStringLiteral("Subregion")).toString());
195     address.setPostalCode(attributesObject.value(QStringLiteral("Postal")).toString());
196 
197     QGeoLocation location;
198 
199     location.setCoordinate(coordinate);
200     location.setBoundingBox(extent);
201     location.setAddress(address);
202 
203     return location;
204 }
205 
206 QT_END_NAMESPACE
207