| 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 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 "qgeocodereplyosm.h" |
| 41 | |
| 42 | #include <QtCore/QJsonDocument> |
| 43 | #include <QtCore/QJsonObject> |
| 44 | #include <QtCore/QJsonArray> |
| 45 | #include <QtPositioning/QGeoCoordinate> |
| 46 | #include <QtPositioning/QGeoAddress> |
| 47 | #include <QtPositioning/QGeoLocation> |
| 48 | #include <QtPositioning/QGeoRectangle> |
| 49 | #include <QtLocation/private/qgeojson_p.h> |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | QGeoCodeReplyOsm::QGeoCodeReplyOsm(QNetworkReply *reply, bool , QObject *parent) |
| 54 | : QGeoCodeReply(*new QGeoCodeReplyOsmPrivate, parent), m_includeExtraData(includeExtraData) |
| 55 | { |
| 56 | if (!reply) { |
| 57 | setError(error: UnknownError, QStringLiteral("Null reply" )); |
| 58 | return; |
| 59 | } |
| 60 | connect(sender: reply, SIGNAL(finished()), receiver: this, SLOT(networkReplyFinished())); |
| 61 | connect(sender: reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), |
| 62 | receiver: this, SLOT(networkReplyError(QNetworkReply::NetworkError))); |
| 63 | connect(sender: this, signal: &QGeoCodeReply::aborted, receiver: reply, slot: &QNetworkReply::abort); |
| 64 | connect(sender: this, signal: &QObject::destroyed, receiver: reply, slot: &QObject::deleteLater); |
| 65 | setLimit(1); |
| 66 | setOffset(0); |
| 67 | } |
| 68 | |
| 69 | QGeoCodeReplyOsm::~QGeoCodeReplyOsm() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | static QGeoAddress parseAddressObject(const QJsonObject &object) |
| 74 | { |
| 75 | QGeoAddress address; |
| 76 | address.setText(object.value(QStringLiteral("display_name" )).toString()); |
| 77 | QJsonObject ao = object.value(QStringLiteral("address" )).toObject(); |
| 78 | // setCountry |
| 79 | address.setCountry(ao.value(QStringLiteral("country" )).toString()); |
| 80 | // setCountryCode |
| 81 | address.setCountryCode(ao.value(QStringLiteral("country_code" )).toString()); |
| 82 | // setState |
| 83 | address.setState(ao.value(QStringLiteral("state" )).toString()); |
| 84 | // setCity |
| 85 | if (ao.contains(key: QLatin1String("city" ))) |
| 86 | address.setCity(ao.value(QStringLiteral("city" )).toString()); |
| 87 | else if (ao.contains(key: QLatin1String("town" ))) |
| 88 | address.setCity(ao.value(key: QLatin1String("town" )).toString()); |
| 89 | else if (ao.contains(key: QLatin1String("village" ))) |
| 90 | address.setCity(ao.value(key: QLatin1String("village" )).toString()); |
| 91 | else |
| 92 | address.setCity(ao.value(key: QLatin1String("hamlet" )).toString()); |
| 93 | // setDistrict |
| 94 | address.setDistrict(ao.value(QStringLiteral("suburb" )).toString()); |
| 95 | // setPostalCode |
| 96 | address.setPostalCode(ao.value(QStringLiteral("postcode" )).toString()); |
| 97 | // setStreet |
| 98 | address.setStreet(ao.value(QStringLiteral("road" )).toString()); |
| 99 | return address; |
| 100 | } |
| 101 | |
| 102 | static void (QGeoLocation &location, const QJsonObject &object) |
| 103 | { |
| 104 | QVariantMap ; |
| 105 | static const QList<QString> = { QStringLiteral("geojson" ), |
| 106 | QStringLiteral("icon" ), |
| 107 | QStringLiteral("importance" ), |
| 108 | QStringLiteral("type" ), |
| 109 | QStringLiteral("osm_id" ), |
| 110 | QStringLiteral("osm_type" ), |
| 111 | QStringLiteral("licence" ), |
| 112 | QStringLiteral("place_id" ), |
| 113 | QStringLiteral("class" ) }; |
| 114 | |
| 115 | for (const auto &k: extraKeys) { |
| 116 | if (object.contains(key: k)) { |
| 117 | extra[k] = object.value(key: k).toVariant(); |
| 118 | if (k == QStringLiteral("geojson" )) |
| 119 | extra[QStringLiteral("geojson_model" )] = |
| 120 | QGeoJson::importGeoJson(doc: QJsonDocument::fromVariant(variant: extra[k])); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | location.setExtendedAttributes(extra); |
| 125 | } |
| 126 | |
| 127 | void QGeoCodeReplyOsm::networkReplyFinished() |
| 128 | { |
| 129 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 130 | reply->deleteLater(); |
| 131 | |
| 132 | if (reply->error() != QNetworkReply::NoError) |
| 133 | return; |
| 134 | |
| 135 | QList<QGeoLocation> locations; |
| 136 | QJsonDocument document = QJsonDocument::fromJson(json: reply->readAll()); |
| 137 | |
| 138 | if (document.isObject()) { |
| 139 | QJsonObject object = document.object(); |
| 140 | |
| 141 | QGeoCoordinate coordinate; |
| 142 | |
| 143 | coordinate.setLatitude(object.value(QStringLiteral("lat" )).toString().toDouble()); |
| 144 | coordinate.setLongitude(object.value(QStringLiteral("lon" )).toString().toDouble()); |
| 145 | |
| 146 | QGeoLocation location; |
| 147 | location.setCoordinate(coordinate); |
| 148 | location.setAddress(parseAddressObject(object)); |
| 149 | |
| 150 | if (m_includeExtraData) |
| 151 | injectExtra(location, object); |
| 152 | locations.append(t: location); |
| 153 | |
| 154 | setLocations(locations); |
| 155 | } else if (document.isArray()) { |
| 156 | QJsonArray results = document.array(); |
| 157 | |
| 158 | for (int i = 0; i < results.count(); ++i) { |
| 159 | if (!results.at(i).isObject()) |
| 160 | continue; |
| 161 | |
| 162 | QJsonObject object = results.at(i).toObject(); |
| 163 | |
| 164 | QGeoCoordinate coordinate; |
| 165 | |
| 166 | coordinate.setLatitude(object.value(QStringLiteral("lat" )).toString().toDouble()); |
| 167 | coordinate.setLongitude(object.value(QStringLiteral("lon" )).toString().toDouble()); |
| 168 | |
| 169 | QGeoRectangle rectangle; |
| 170 | |
| 171 | if (object.contains(QStringLiteral("boundingbox" ))) { |
| 172 | QJsonArray a = object.value(QStringLiteral("boundingbox" )).toArray(); |
| 173 | if (a.count() == 4) { |
| 174 | rectangle.setTopLeft(QGeoCoordinate(a.at(i: 1).toString().toDouble(), |
| 175 | a.at(i: 2).toString().toDouble())); |
| 176 | rectangle.setBottomRight(QGeoCoordinate(a.at(i: 0).toString().toDouble(), |
| 177 | a.at(i: 3).toString().toDouble())); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | QGeoLocation location; |
| 182 | location.setCoordinate(coordinate); |
| 183 | location.setBoundingBox(rectangle); |
| 184 | location.setAddress(parseAddressObject(object)); |
| 185 | if (m_includeExtraData) |
| 186 | injectExtra(location, object); |
| 187 | locations.append(t: location); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | setLocations(locations); |
| 193 | setFinished(true); |
| 194 | } |
| 195 | |
| 196 | void QGeoCodeReplyOsm::networkReplyError(QNetworkReply::NetworkError error) |
| 197 | { |
| 198 | Q_UNUSED(error); |
| 199 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 200 | reply->deleteLater(); |
| 201 | setError(error: QGeoCodeReply::CommunicationError, errorString: reply->errorString()); |
| 202 | } |
| 203 | |
| 204 | QGeoCodeReplyOsmPrivate::QGeoCodeReplyOsmPrivate() |
| 205 | { |
| 206 | |
| 207 | } |
| 208 | |
| 209 | QGeoCodeReplyOsmPrivate::~QGeoCodeReplyOsmPrivate() |
| 210 | { |
| 211 | |
| 212 | } |
| 213 | |
| 214 | QVariantMap QGeoCodeReplyOsmPrivate::() const |
| 215 | { |
| 216 | return m_extraData; |
| 217 | } |
| 218 | |
| 219 | QT_END_NAMESPACE |
| 220 | |
| 221 | |
| 222 | |