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
qgeocodingmanagerengineosm.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
5
6#include <QtCore/QVariantMap>
7#include <QtCore/QUrl>
8#include <QtCore/QUrlQuery>
9#include <QtCore/QLocale>
10#include <QtNetwork/QNetworkAccessManager>
11#include <QtNetwork/QNetworkRequest>
12#include <QtPositioning/QGeoCoordinate>
13#include <QtPositioning/QGeoAddress>
14#include <QtPositioning/QGeoShape>
15#include <QtPositioning/QGeoRectangle>
17
18
20
21static QString addressToQuery(const QGeoAddress &address)
22{
23 return address.street() + QStringLiteral(", ") +
24 address.district() + QStringLiteral(", ") +
25 address.city() + QStringLiteral(", ") +
26 address.state() + QStringLiteral(", ") +
27 address.country();
28}
29
30static QString boundingBoxToLtrb(const QGeoRectangle &rect)
31{
32 return QString::number(rect.topLeft().longitude()) + QLatin1Char(',') +
33 QString::number(rect.topLeft().latitude()) + QLatin1Char(',') +
34 QString::number(rect.bottomRight().longitude()) + QLatin1Char(',') +
35 QString::number(rect.bottomRight().latitude());
36}
37
38QGeoCodingManagerEngineOsm::QGeoCodingManagerEngineOsm(const QVariantMap &parameters,
39 QGeoServiceProvider::Error *error,
40 QString *errorString)
41: QGeoCodingManagerEngine(parameters), m_networkManager(new QNetworkAccessManager(this))
42{
43 if (parameters.contains(QStringLiteral("osm.useragent")))
44 m_userAgent = parameters.value(QStringLiteral("osm.useragent")).toString().toLatin1();
45 else
46 m_userAgent = "Qt Location based application";
47
48 if (parameters.contains(QStringLiteral("osm.geocoding.host")))
49 m_urlPrefix = parameters.value(QStringLiteral("osm.geocoding.host")).toString().toLatin1();
50 else
51 m_urlPrefix = QStringLiteral("https://nominatim.openstreetmap.org");
52
53 if (parameters.contains(QStringLiteral("osm.geocoding.debug_query")))
54 m_debugQuery = parameters.value(QStringLiteral("osm.geocoding.debug_query")).toBool();
55 if (parameters.contains(QStringLiteral("osm.geocoding.include_extended_data")))
56 m_includeExtraData = parameters.value(QStringLiteral("osm.geocoding.include_extended_data")).toBool();
57
58 *error = QGeoServiceProvider::NoError;
59 errorString->clear();
60}
61
65
66QGeoCodeReply *QGeoCodingManagerEngineOsm::geocode(const QGeoAddress &address, const QGeoShape &bounds)
67{
68 return geocode(addressToQuery(address), -1, -1, bounds);
69}
70
71QGeoCodeReply *QGeoCodingManagerEngineOsm::geocode(const QString &address, int limit, int offset, const QGeoShape &bounds)
72{
73 Q_UNUSED(offset);
74
75 QNetworkRequest request;
76 request.setRawHeader("User-Agent", m_userAgent);
77
78 QUrl url(QString("%1/search").arg(m_urlPrefix));
79 QUrlQuery query;
80 query.addQueryItem(QStringLiteral("q"), address);
81 query.addQueryItem(QStringLiteral("format"), QStringLiteral("json"));
82 query.addQueryItem(QStringLiteral("accept-language"), locale().name().left(2));
83 //query.addQueryItem(QStringLiteral("countrycodes"), QStringLiteral("au,jp"));
84 if (bounds.type() != QGeoShape::UnknownType) {
85 query.addQueryItem(QStringLiteral("viewbox"), boundingBoxToLtrb(bounds.boundingGeoRectangle()));
86 query.addQueryItem(QStringLiteral("bounded"), QStringLiteral("1"));
87 }
88 query.addQueryItem(QStringLiteral("polygon_geojson"), QStringLiteral("1"));
89 query.addQueryItem(QStringLiteral("addressdetails"), QStringLiteral("1"));
90 if (limit != -1)
91 query.addQueryItem(QStringLiteral("limit"), QString::number(limit));
92
93 url.setQuery(query);
94 request.setUrl(url);
95
96 QNetworkReply *reply = m_networkManager->get(request);
97
98 QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this);
99
100 connect(geocodeReply, &QGeoCodeReplyOsm::finished,
101 this, &QGeoCodingManagerEngineOsm::replyFinished);
102 connect(geocodeReply, &QGeoCodeReplyOsm::errorOccurred,
103 this, &QGeoCodingManagerEngineOsm::replyError);
104
105 return geocodeReply;
106}
107
108QGeoCodeReply *QGeoCodingManagerEngineOsm::reverseGeocode(const QGeoCoordinate &coordinate,
109 const QGeoShape &bounds)
110{
111 Q_UNUSED(bounds);
112
113 QNetworkRequest request;
114 request.setRawHeader("User-Agent", m_userAgent);
115
116 QUrl url(QString("%1/reverse").arg(m_urlPrefix));
117 QUrlQuery query;
118 query.addQueryItem(QStringLiteral("format"), QStringLiteral("json"));
119 query.addQueryItem(QStringLiteral("accept-language"), locale().name().left(2));
120 query.addQueryItem(QStringLiteral("lat"), QString::number(coordinate.latitude()));
121 query.addQueryItem(QStringLiteral("lon"), QString::number(coordinate.longitude()));
122 query.addQueryItem(QStringLiteral("zoom"), QStringLiteral("18"));
123 query.addQueryItem(QStringLiteral("addressdetails"), QStringLiteral("1"));
124
125 url.setQuery(query);
126 request.setUrl(url);
127
128 QNetworkReply *reply = m_networkManager->get(request);
129
130 QGeoCodeReplyOsm *geocodeReply = new QGeoCodeReplyOsm(reply, m_includeExtraData, this);
131
132 connect(geocodeReply, &QGeoCodeReplyOsm::finished,
133 this, &QGeoCodingManagerEngineOsm::replyFinished);
134 connect(geocodeReply, &QGeoCodeReplyOsm::errorOccurred,
135 this, &QGeoCodingManagerEngineOsm::replyError);
136
137 return geocodeReply;
138}
139
140void QGeoCodingManagerEngineOsm::replyFinished()
141{
142 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
143 if (reply)
144 emit finished(reply);
145}
146
147void QGeoCodingManagerEngineOsm::replyError(QGeoCodeReply::Error errorCode, const QString &errorString)
148{
149 QGeoCodeReply *reply = qobject_cast<QGeoCodeReply *>(sender());
150 if (reply)
151 emit errorOccurred(reply, errorCode, errorString);
152}
153
154QT_END_NAMESPACE
QGeoCodeReply * geocode(const QString &address, int limit, int offset, const QGeoShape &bounds) override
Begins geocoding for a location matching address.
QGeoCodeReply * reverseGeocode(const QGeoCoordinate &coordinate, const QGeoShape &bounds) override
Begins the reverse geocoding of coordinate.
QGeoCodeReply * geocode(const QGeoAddress &address, const QGeoShape &bounds) override
Begins the geocoding of address.
Combined button and popup list for selecting options.
static QString boundingBoxToLtrb(const QGeoRectangle &rect)
static QT_BEGIN_NAMESPACE QString addressToQuery(const QGeoAddress &address)