| 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 "qgeocodingmanagerengineosm.h" |
| 41 | |
| 42 | #include <QtCore/QVariantMap> |
| 43 | #include <QtCore/QUrl> |
| 44 | #include <QtCore/QUrlQuery> |
| 45 | #include <QtCore/QLocale> |
| 46 | #include <QtNetwork/QNetworkAccessManager> |
| 47 | #include <QtNetwork/QNetworkRequest> |
| 48 | #include <QtPositioning/QGeoCoordinate> |
| 49 | #include <QtPositioning/QGeoAddress> |
| 50 | #include <QtPositioning/QGeoShape> |
| 51 | #include <QtPositioning/QGeoRectangle> |
| 52 | #include "qgeocodereplyosm.h" |
| 53 | |
| 54 | |
| 55 | QT_BEGIN_NAMESPACE |
| 56 | |
| 57 | static QString addressToQuery(const QGeoAddress &address) |
| 58 | { |
| 59 | return address.street() + QStringLiteral(", " ) + |
| 60 | address.district() + QStringLiteral(", " ) + |
| 61 | address.city() + QStringLiteral(", " ) + |
| 62 | address.state() + QStringLiteral(", " ) + |
| 63 | address.country(); |
| 64 | } |
| 65 | |
| 66 | static QString boundingBoxToLtrb(const QGeoRectangle &rect) |
| 67 | { |
| 68 | return QString::number(rect.topLeft().longitude()) + QLatin1Char(',') + |
| 69 | QString::number(rect.topLeft().latitude()) + QLatin1Char(',') + |
| 70 | QString::number(rect.bottomRight().longitude()) + QLatin1Char(',') + |
| 71 | QString::number(rect.bottomRight().latitude()); |
| 72 | } |
| 73 | |
| 74 | QGeoCodingManagerEngineOsm::QGeoCodingManagerEngineOsm(const QVariantMap ¶meters, |
| 75 | QGeoServiceProvider::Error *error, |
| 76 | QString *errorString) |
| 77 | : QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this)) |
| 78 | { |
| 79 | if (parameters.contains(QStringLiteral("osm.useragent" ))) |
| 80 | m_userAgent = parameters.value(QStringLiteral("osm.useragent" )).toString().toLatin1(); |
| 81 | else |
| 82 | m_userAgent = "Qt Location based application" ; |
| 83 | |
| 84 | if (parameters.contains(QStringLiteral("osm.geocoding.host" ))) |
| 85 | m_urlPrefix = parameters.value(QStringLiteral("osm.geocoding.host" )).toString().toLatin1(); |
| 86 | else |
| 87 | m_urlPrefix = QStringLiteral("https://nominatim.openstreetmap.org" ); |
| 88 | |
| 89 | if (parameters.contains(QStringLiteral("osm.geocoding.debug_query" ))) |
| 90 | m_debugQuery = parameters.value(QStringLiteral("osm.geocoding.debug_query" )).toBool(); |
| 91 | if (parameters.contains(QStringLiteral("osm.geocoding.include_extended_data" ))) |
| 92 | m_includeExtraData = parameters.value(QStringLiteral("osm.geocoding.include_extended_data" )).toBool(); |
| 93 | |
| 94 | *error = QGeoServiceProvider::NoError; |
| 95 | errorString->clear(); |
| 96 | } |
| 97 | |
| 98 | QGeoCodingManagerEngineOsm::~QGeoCodingManagerEngineOsm() |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | QGeoCodeReply *QGeoCodingManagerEngineOsm::geocode(const QGeoAddress &address, const QGeoShape &bounds) |
| 103 | { |
| 104 | return geocode(address: addressToQuery(address), limit: -1, offset: -1, bounds); |
| 105 | } |
| 106 | |
| 107 | QGeoCodeReply *QGeoCodingManagerEngineOsm::geocode(const QString &address, int limit, int offset, const QGeoShape &bounds) |
| 108 | { |
| 109 | Q_UNUSED(offset); |
| 110 | |
| 111 | QNetworkRequest request; |
| 112 | request.setRawHeader(headerName: "User-Agent" , value: m_userAgent); |
| 113 | |
| 114 | QUrl url(QString("%1/search" ).arg(a: m_urlPrefix)); |
| 115 | QUrlQuery query; |
| 116 | query.addQueryItem(QStringLiteral("q" ), value: address); |
| 117 | query.addQueryItem(QStringLiteral("format" ), QStringLiteral("json" )); |
| 118 | query.addQueryItem(QStringLiteral("accept-language" ), value: locale().name().left(n: 2)); |
| 119 | //query.addQueryItem(QStringLiteral("countrycodes"), QStringLiteral("au,jp")); |
| 120 | if (bounds.type() != QGeoShape::UnknownType) { |
| 121 | query.addQueryItem(QStringLiteral("viewbox" ), value: boundingBoxToLtrb(rect: bounds.boundingGeoRectangle())); |
| 122 | query.addQueryItem(QStringLiteral("bounded" ), QStringLiteral("1" )); |
| 123 | } |
| 124 | query.addQueryItem(QStringLiteral("polygon_geojson" ), QStringLiteral("1" )); |
| 125 | query.addQueryItem(QStringLiteral("addressdetails" ), QStringLiteral("1" )); |
| 126 | if (limit != -1) |
| 127 | query.addQueryItem(QStringLiteral("limit" ), value: QString::number(limit)); |
| 128 | |
| 129 | url.setQuery(query); |
| 130 | request.setUrl(url); |
| 131 | |
| 132 | QNetworkReply *reply = m_networkManager->get(request); |
| 133 | |
| 134 | QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this); |
| 135 | if (m_debugQuery) { |
| 136 | QGeoCodeReplyOsmPrivate *replyPrivate |
| 137 | = static_cast<QGeoCodeReplyOsmPrivate *>(QGeoCodeReplyPrivate::get(reply&: *geocodeReply)); |
| 138 | replyPrivate->m_extraData["request_url" ] = url; |
| 139 | } |
| 140 | |
| 141 | connect(sender: geocodeReply, SIGNAL(finished()), receiver: this, SLOT(replyFinished())); |
| 142 | connect(sender: geocodeReply, SIGNAL(error(QGeoCodeReply::Error,QString)), |
| 143 | receiver: this, SLOT(replyError(QGeoCodeReply::Error,QString))); |
| 144 | |
| 145 | return geocodeReply; |
| 146 | } |
| 147 | |
| 148 | QGeoCodeReply *QGeoCodingManagerEngineOsm::reverseGeocode(const QGeoCoordinate &coordinate, |
| 149 | const QGeoShape &bounds) |
| 150 | { |
| 151 | Q_UNUSED(bounds); |
| 152 | |
| 153 | QNetworkRequest request; |
| 154 | request.setRawHeader(headerName: "User-Agent" , value: m_userAgent); |
| 155 | |
| 156 | QUrl url(QString("%1/reverse" ).arg(a: m_urlPrefix)); |
| 157 | QUrlQuery query; |
| 158 | query.addQueryItem(QStringLiteral("format" ), QStringLiteral("json" )); |
| 159 | query.addQueryItem(QStringLiteral("accept-language" ), value: locale().name().left(n: 2)); |
| 160 | query.addQueryItem(QStringLiteral("lat" ), value: QString::number(coordinate.latitude())); |
| 161 | query.addQueryItem(QStringLiteral("lon" ), value: QString::number(coordinate.longitude())); |
| 162 | query.addQueryItem(QStringLiteral("zoom" ), QStringLiteral("18" )); |
| 163 | query.addQueryItem(QStringLiteral("addressdetails" ), QStringLiteral("1" )); |
| 164 | |
| 165 | url.setQuery(query); |
| 166 | request.setUrl(url); |
| 167 | |
| 168 | QNetworkReply *reply = m_networkManager->get(request); |
| 169 | |
| 170 | QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this); |
| 171 | if (m_debugQuery) { |
| 172 | QGeoCodeReplyOsmPrivate *replyPrivate |
| 173 | = static_cast<QGeoCodeReplyOsmPrivate *>(QGeoCodeReplyPrivate::get(reply&: *geocodeReply)); |
| 174 | replyPrivate->m_extraData["request_url" ] = url; |
| 175 | } |
| 176 | |
| 177 | connect(sender: geocodeReply, SIGNAL(finished()), receiver: this, SLOT(replyFinished())); |
| 178 | connect(sender: geocodeReply, SIGNAL(error(QGeoCodeReply::Error,QString)), |
| 179 | receiver: this, SLOT(replyError(QGeoCodeReply::Error,QString))); |
| 180 | |
| 181 | return geocodeReply; |
| 182 | } |
| 183 | |
| 184 | void QGeoCodingManagerEngineOsm::replyFinished() |
| 185 | { |
| 186 | QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(object: sender()); |
| 187 | if (reply) |
| 188 | emit finished(reply); |
| 189 | } |
| 190 | |
| 191 | void QGeoCodingManagerEngineOsm::replyError(QGeoCodeReply::Error errorCode, const QString &errorString) |
| 192 | { |
| 193 | QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(object: sender()); |
| 194 | if (reply) |
| 195 | emit error(reply, error: errorCode, errorString); |
| 196 | } |
| 197 | |
| 198 | QT_END_NAMESPACE |
| 199 | |