Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "geocodereply_esri.h"
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);
26 this, &GeoCodeReplyEsri::networkReplyError);
29
30 setLimit(1);
31 setOffset(0);
32}
33
37
38void GeoCodeReplyEsri::networkReplyError(QNetworkReply::NetworkError error)
39{
41 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
44}
45
46void GeoCodeReplyEsri::networkReplyFinished()
47{
48 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
50
52 return;
53
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
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 {
87 }
88}
89
90QGeoLocation GeoCodeReplyEsri::parseAddress(const QJsonObject& object)
91{
92 QJsonObject addressObject = object.value(QStringLiteral("address")).toObject();
93
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
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
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
153
154 location.setCoordinate(coordinate);
155 location.setBoundingShape(extent);
156 location.setAddress(address);
157
158 return location;
159}
160
GeoCodeReplyEsri(QNetworkReply *reply, OperationType operationType, QObject *parent=nullptr)
OperationType operationType() const
\inmodule QtPositioning
Definition qgeoaddress.h:18
void setCountryCode(const QString &countryCode)
Sets the countryCode according to ISO 3166-1 alpha-3.
void setText(const QString &text)
If text is not empty, explicitly assigns text as the string to be returned by text().
\inmodule QtLocation
void setLimit(qsizetype limit)
Sets the limit on the number of responses from each data source to limit.
Error error() const
Returns the error state of this reply.
void setFinished(bool finished)
Sets whether or not this reply has finished to finished.
void setError(Error error, const QString &errorString)
Sets the error state of this reply to error and the textual representation of the error to errorStrin...
QList< QGeoLocation > locations() const
Returns a list of locations.
void setLocations(const QList< QGeoLocation > &locations)
Sets the list of locations in the reply.
void setOffset(qsizetype offset)
Sets the offset in the entire result set at which to start fetching result to offset.
\inmodule QtPositioning
void setLatitude(double latitude)
Sets the latitude (in decimal degrees) to latitude.
void setLongitude(double longitude)
Sets the longitude (in decimal degrees) to longitude.
\inmodule QtPositioning
void setCoordinate(const QGeoCoordinate &position)
Sets the coordinate of the location.
\inmodule QtPositioning
void setTopLeft(const QGeoCoordinate &topLeft)
Sets the top left coordinate of this geo rectangle to topLeft.
void setBottomRight(const QGeoCoordinate &bottomRight)
Sets the bottom right coordinate of this geo rectangle to bottomRight.
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QJsonValue value(const QString &key) const
Returns a QJsonValue representing the value for the key key.
bool contains(const QString &key) const
Returns true if the object contains key key.
QJsonObject toObject() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void append(parameter_type t)
Definition qlist.h:458
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
void errorOccurred(QNetworkReply::NetworkError)
NetworkError error() const
Returns the error that was found during the processing of this request.
virtual void abort()=0
Aborts the operation immediately and close down any network connections still open.
NetworkError
Indicates all possible error conditions found during the processing of the request.
void finished()
This signal is emitted when the reply has finished processing.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
DBusConnection const char DBusError * error
GLint location
GLuint GLuint64EXT address
GLuint const GLint * locations
#define QStringLiteral(str)
#define Q_UNUSED(x)
static double toDouble(Value v)
QNetworkReply * reply
char * toString(const MyType &t)
[31]