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
geocodereply_esri.cpp
Go to the documentation of this file.
1// Copyright (C) 2013-2018 Esri <contracts@esri.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QJsonDocument>
7#include <QJsonObject>
8#include <QJsonArray>
9#include <QGeoCoordinate>
10#include <QGeoAddress>
11#include <QGeoLocation>
12#include <QGeoRectangle>
13
15
17 QObject *parent) :
18 QGeoCodeReply(parent), m_operationType(operationType)
19{
20 if (!reply) {
21 setError(UnknownError, QStringLiteral("Null reply"));
22 return;
23 }
24 connect(reply, &QNetworkReply::finished, this, &GeoCodeReplyEsri::networkReplyFinished);
25 connect(reply, &QNetworkReply::errorOccurred,
26 this, &GeoCodeReplyEsri::networkReplyError);
27 connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
28 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
29
30 setLimit(1);
31 setOffset(0);
32}
33
37
38void GeoCodeReplyEsri::networkReplyError(QNetworkReply::NetworkError error)
39{
40 Q_UNUSED(error);
41 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
42 reply->deleteLater();
43 setError(QGeoCodeReply::CommunicationError, reply->errorString());
44}
45
46void GeoCodeReplyEsri::networkReplyFinished()
47{
48 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
49 reply->deleteLater();
50
51 if (reply->error() != QNetworkReply::NoError)
52 return;
53
54 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
55
56 if (document.isObject()) {
57 QJsonObject object = document.object();
58
59 switch (operationType()) {
60 case Geocode:
61 {
62 const QJsonArray candidates = object.value(QStringLiteral("candidates")).toArray();
63
64 QList<QGeoLocation> locations;
65
66 for (const auto candidate : candidates) {
67 if (!candidate.isObject())
68 continue;
69 locations.append(parseCandidate(candidate.toObject()));
70 }
71
72 setLocations(locations);
73 setFinished(true);
74 }
75 break;
76
77 case ReverseGeocode:
78 {
79 setLocations({parseAddress(object)});
80 setFinished(true);
81 }
82 break;
83 }
84
85 } else {
86 setError(QGeoCodeReply::CommunicationError, QStringLiteral("Unknown document"));
87 }
88}
89
90QGeoLocation GeoCodeReplyEsri::parseAddress(const QJsonObject& object)
91{
92 QJsonObject addressObject = object.value(QStringLiteral("address")).toObject();
93
94 QGeoAddress address;
95
96 address.setCountryCode(addressObject.value(QStringLiteral("CountryCode")).toString());
97 address.setState(addressObject.value(QStringLiteral("Region")).toString());
98 address.setCity(addressObject.value(QStringLiteral("City")).toString());
99 address.setDistrict(addressObject.value(QStringLiteral("Subregion")).toString());
100 address.setPostalCode(addressObject.value(QStringLiteral("Postal")).toString());
101 address.setStreet(addressObject.value(QStringLiteral("Address")).toString());
102
103 QGeoCoordinate coordinate;
104
105 QJsonObject locationObject = object.value(QStringLiteral("location")).toObject();
106
107 coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
108 coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());
109
110 QGeoLocation location;
111
112 location.setCoordinate(coordinate);
113 location.setAddress(address);
114
115 return location;
116}
117
118QGeoLocation GeoCodeReplyEsri::parseCandidate(const QJsonObject& candidate)
119{
120 QGeoCoordinate coordinate;
121
122 QJsonObject locationObject = candidate.value(QStringLiteral("location")).toObject();
123
124 coordinate.setLongitude(locationObject.value(QStringLiteral("x")).toDouble());
125 coordinate.setLatitude(locationObject.value(QStringLiteral("y")).toDouble());
126
127 QGeoRectangle extent;
128
129 if (candidate.contains(QStringLiteral("extent"))) {
130 QJsonObject extentObject = candidate.value(QStringLiteral("extent")).toObject();
131
132 extent.setTopLeft(QGeoCoordinate(extentObject.value(QStringLiteral("ymin")).toDouble(),
133 extentObject.value(QStringLiteral("xmin")).toDouble()));
134
135 extent.setBottomRight(QGeoCoordinate(extentObject.value(QStringLiteral("ymax")).toDouble(),
136 extentObject.value(QStringLiteral("xmax")).toDouble()));
137 }
138
139 QJsonObject attributesObject = candidate.value(QStringLiteral("attributes")).toObject();
140
141 QGeoAddress address;
142
143 address.setText(candidate.value(QStringLiteral("address")).toString());
144
145 address.setCountry(attributesObject.value(QStringLiteral("Country")).toString());
146 address.setCountryCode(attributesObject.value(QStringLiteral("Country")).toString());
147 address.setState(attributesObject.value(QStringLiteral("Region")).toString());
148 address.setCity(attributesObject.value(QStringLiteral("City")).toString());
149 address.setDistrict(attributesObject.value(QStringLiteral("Subregion")).toString());
150 address.setPostalCode(attributesObject.value(QStringLiteral("Postal")).toString());
151
152 QGeoLocation location;
153
154 location.setCoordinate(coordinate);
155 location.setBoundingShape(extent);
156 location.setAddress(address);
157
158 return location;
159}
160
161QT_END_NAMESPACE
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
QObject * parent
Definition qobject.h:73
Combined button and popup list for selecting options.