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
qgeocodereplyosm.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Aaron McCarthy <mccarthy.aaron@gmail.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 "qgeocodereplyosm.h"
5
6#include <QtCore/QJsonDocument>
7#include <QtCore/QJsonObject>
8#include <QtCore/QJsonArray>
9#include <QtPositioning/QGeoCoordinate>
10#include <QtPositioning/QGeoAddress>
11#include <QtPositioning/QGeoLocation>
12#include <QtPositioning/QGeoRectangle>
13#include <QtLocation/private/qgeojson_p.h>
14
16
18: QGeoCodeReply(parent), m_includeExtraData(includeExtraData)
19{
20 if (!reply) {
21 setError(UnknownError, QStringLiteral("Null reply"));
22 return;
23 }
25 this, &QGeoCodeReplyOsm::networkReplyFinished);
27 this, &QGeoCodeReplyOsm::networkReplyError);
30 setLimit(1);
31 setOffset(0);
32}
33
37
39{
41 address.setText(object.value(QStringLiteral("display_name")).toString());
42 QJsonObject ao = object.value(QStringLiteral("address")).toObject();
43 // setCountry
44 address.setCountry(ao.value(QStringLiteral("country")).toString());
45 // setCountryCode
46 address.setCountryCode(ao.value(QStringLiteral("country_code")).toString());
47 // setState
48 address.setState(ao.value(QStringLiteral("state")).toString());
49 // setCity
50 if (ao.contains(QLatin1String("city")))
51 address.setCity(ao.value(QStringLiteral("city")).toString());
52 else if (ao.contains(QLatin1String("town")))
53 address.setCity(ao.value(QLatin1String("town")).toString());
54 else if (ao.contains(QLatin1String("village")))
55 address.setCity(ao.value(QLatin1String("village")).toString());
56 else
57 address.setCity(ao.value(QLatin1String("hamlet")).toString());
58 // setDistrict
59 address.setDistrict(ao.value(QStringLiteral("suburb")).toString());
60 // setPostalCode
61 address.setPostalCode(ao.value(QStringLiteral("postcode")).toString());
62 // setStreet
63 address.setStreet(ao.value(QStringLiteral("road")).toString());
64 return address;
65}
66
67static void injectExtra(QGeoLocation &location, const QJsonObject &object)
68{
69 QVariantMap extra;
70 static const QList<QString> extraKeys = { QStringLiteral("geojson"),
71 QStringLiteral("icon"),
72 QStringLiteral("importance"),
73 QStringLiteral("type"),
74 QStringLiteral("osm_id"),
75 QStringLiteral("osm_type"),
76 QStringLiteral("licence"),
77 QStringLiteral("place_id"),
78 QStringLiteral("class") };
79
80 for (const auto &k: extraKeys) {
81 if (object.contains(k)) {
82 extra[k] = object.value(k).toVariant();
83 if (k == QStringLiteral("geojson"))
84 extra[QStringLiteral("geojson_model")] =
86 }
87 }
88
89 location.setExtendedAttributes(extra);
90}
91
92void QGeoCodeReplyOsm::networkReplyFinished()
93{
94 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
96
98 return;
99
100 QList<QGeoLocation> locations;
102
103 if (document.isObject()) {
104 QJsonObject object = document.object();
105
106 QGeoCoordinate coordinate;
107
108 coordinate.setLatitude(object.value(QStringLiteral("lat")).toString().toDouble());
109 coordinate.setLongitude(object.value(QStringLiteral("lon")).toString().toDouble());
110
112 location.setCoordinate(coordinate);
113 location.setAddress(parseAddressObject(object));
114
115 if (m_includeExtraData)
116 injectExtra(location, object);
117 locations.append(location);
118
120 } else if (document.isArray()) {
121 QJsonArray results = document.array();
122
123 for (int i = 0; i < results.count(); ++i) {
124 if (!results.at(i).isObject())
125 continue;
126
127 QJsonObject object = results.at(i).toObject();
128
129 QGeoCoordinate coordinate;
130
131 coordinate.setLatitude(object.value(QStringLiteral("lat")).toString().toDouble());
132 coordinate.setLongitude(object.value(QStringLiteral("lon")).toString().toDouble());
133
134 QGeoRectangle rectangle;
135
136 if (object.contains(QStringLiteral("boundingbox"))) {
137 QJsonArray a = object.value(QStringLiteral("boundingbox")).toArray();
138 if (a.count() == 4) {
139 rectangle.setTopLeft(QGeoCoordinate(a.at(1).toString().toDouble(),
140 a.at(2).toString().toDouble()));
141 rectangle.setBottomRight(QGeoCoordinate(a.at(0).toString().toDouble(),
142 a.at(3).toString().toDouble()));
143 }
144 }
145
147 location.setCoordinate(coordinate);
148 location.setBoundingShape(rectangle);
149 location.setAddress(parseAddressObject(object));
150 if (m_includeExtraData)
151 injectExtra(location, object);
152 locations.append(location);
153 }
154
155 }
156
158 setFinished(true);
159}
160
161void QGeoCodeReplyOsm::networkReplyError(QNetworkReply::NetworkError error)
162{
164 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
167}
168
\inmodule QtPositioning
Definition qgeoaddress.h:18
void setText(const QString &text)
If text is not empty, explicitly assigns text as the string to be returned by text().
QGeoCodeReplyOsm(QNetworkReply *reply, bool includeExtraData=false, QObject *parent=nullptr)
\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 fromVariant(const QVariant &variant)
Creates a QJsonDocument from the QVariant variant.
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.
QJsonObject toObject() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
qsizetype count() const noexcept
Definition qlist.h:398
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
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
QVariantList importGeoJson(const QJsonDocument &geoJson)
This method imports the geoJson document, expected to contain valid GeoJSON data, into a QVariantList...
Definition qgeojson.cpp:935
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QGeoAddress parseAddressObject(const QJsonObject &object)
static void injectExtra(QGeoLocation &location, const QJsonObject &object)
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLint location
GLboolean GLboolean GLboolean GLboolean a
[7]
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]