9#include <QtCore/QElapsedTimer>
10#include <QtCore/QLocale>
11#include <QtCore/QRegularExpression>
12#include <QtCore/QUrlQuery>
13#include <QtCore/QXmlStreamReader>
15#include <QtNetwork/QNetworkAccessManager>
16#include <QtNetwork/QNetworkRequest>
17#include <QtNetwork/QNetworkReply>
19#include <QtPositioning/QGeoCircle>
21#include <QtLocation/QPlaceCategory>
22#include <QtLocation/QPlaceSearchRequest>
23#include <QtLocation/private/unsupportedreplies_p.h>
27QString SpecialPhrasesBaseUrl = QStringLiteral(
"http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/");
29QString nameForTagKey(
const QString &tagKey)
31 if (tagKey == QLatin1String(
"aeroway"))
32 return QPlaceManagerEngineOsm::tr(
"Aeroway");
33 else if (tagKey == QLatin1String(
"amenity"))
34 return QPlaceManagerEngineOsm::tr(
"Amenity");
35 else if (tagKey == QLatin1String(
"building"))
36 return QPlaceManagerEngineOsm::tr(
"Building");
37 else if (tagKey == QLatin1String(
"highway"))
38 return QPlaceManagerEngineOsm::tr(
"Highway");
39 else if (tagKey == QLatin1String(
"historic"))
40 return QPlaceManagerEngineOsm::tr(
"Historic");
41 else if (tagKey == QLatin1String(
"landuse"))
42 return QPlaceManagerEngineOsm::tr(
"Land use");
43 else if (tagKey == QLatin1String(
"leisure"))
44 return QPlaceManagerEngineOsm::tr(
"Leisure");
45 else if (tagKey == QLatin1String(
"man_made"))
46 return QPlaceManagerEngineOsm::tr(
"Man made");
47 else if (tagKey == QLatin1String(
"natural"))
48 return QPlaceManagerEngineOsm::tr(
"Natural");
49 else if (tagKey == QLatin1String(
"place"))
50 return QPlaceManagerEngineOsm::tr(
"Place");
51 else if (tagKey == QLatin1String(
"railway"))
52 return QPlaceManagerEngineOsm::tr(
"Railway");
53 else if (tagKey == QLatin1String(
"shop"))
54 return QPlaceManagerEngineOsm::tr(
"Shop");
55 else if (tagKey == QLatin1String(
"tourism"))
56 return QPlaceManagerEngineOsm::tr(
"Tourism");
57 else if (tagKey == QLatin1String(
"waterway"))
58 return QPlaceManagerEngineOsm::tr(
"Waterway");
66 QGeoServiceProvider::Error *error,
68: QPlaceManagerEngine(parameters), m_networkManager(
new QNetworkAccessManager(
this)),
71 if (parameters.contains(QStringLiteral(
"osm.useragent")))
72 m_userAgent = parameters.value(QStringLiteral(
"osm.useragent")).toString().toLatin1();
74 m_userAgent =
"Qt Location based application";
76 if (parameters.contains(QStringLiteral(
"osm.places.host")))
77 m_urlPrefix = parameters.value(QStringLiteral(
"osm.places.host")).toString();
79 m_urlPrefix = QStringLiteral(
"http://nominatim.openstreetmap.org/search");
82 if (parameters.contains(QStringLiteral(
"osm.places.debug_query")))
83 m_debugQuery = parameters.value(QStringLiteral(
"osm.places.debug_query")).toBool();
85 if (parameters.contains(QStringLiteral(
"osm.places.page_size"))
86 && parameters.value(QStringLiteral(
"osm.places.page_size")).canConvert<
int>())
87 m_pageSize = parameters.value(QStringLiteral(
"osm.places.page_size")).toInt();
89 *error = QGeoServiceProvider::NoError;
99 bool unsupported =
false;
102 unsupported |= request.visibilityScope() != QLocation::UnspecifiedVisibility &&
103 request.visibilityScope() != QLocation::PublicVisibility;
104 unsupported |= request.searchTerm().isEmpty() && request.categories().isEmpty();
107 return QPlaceManagerEngine::search(request);
109 QUrlQuery queryItems;
111 queryItems.addQueryItem(QStringLiteral(
"format"), QStringLiteral(
"jsonv2"));
115 QGeoRectangle boundingBox = request.searchArea().boundingGeoRectangle();
117 if (!boundingBox.isEmpty()) {
118 queryItems.addQueryItem(QStringLiteral(
"bounded"), QStringLiteral(
"1"));
120 coordinates = QString::number(boundingBox.topLeft().longitude()) + QLatin1Char(
',') +
121 QString::number(boundingBox.topLeft().latitude()) + QLatin1Char(
',') +
122 QString::number(boundingBox.bottomRight().longitude()) + QLatin1Char(
',') +
123 QString::number(boundingBox.bottomRight().latitude());
124 queryItems.addQueryItem(QStringLiteral(
"viewbox"), coordinates);
127 QStringList queryParts;
128 if (!request.searchTerm().isEmpty())
129 queryParts.append(request.searchTerm());
131 const auto categoriesList = request.categories();
132 for (
const QPlaceCategory &category : categoriesList) {
133 QString id = category.categoryId();
134 queryParts.append(QLatin1Char(
'[') + id + QLatin1Char(
']'));
137 queryItems.addQueryItem(QStringLiteral(
"q"), queryParts.join(QLatin1Char(
'+')));
139 QVariantMap parameters = request.searchContext().toMap();
141 QStringList placeIds = parameters.value(QStringLiteral(
"ExcludePlaceIds")).toStringList();
142 if (!placeIds.isEmpty())
143 queryItems.addQueryItem(QStringLiteral(
"exclude_place_ids"), placeIds.join(QLatin1Char(
',')));
145 queryItems.addQueryItem(QStringLiteral(
"addressdetails"), QStringLiteral(
"1"));
146 queryItems.addQueryItem(QStringLiteral(
"limit"), (request.limit() > 0) ? QString::number(request.limit())
147 : QString::number(m_pageSize));
149 QUrl requestUrl(m_urlPrefix);
150 requestUrl.setQuery(queryItems);
152 QNetworkRequest rq(requestUrl);
153 rq.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
154 QNetworkReply *networkReply = m_networkManager->get(rq);
157 connect(reply, &QPlaceSearchReplyOsm::finished,
159 connect(reply, &QPlaceSearchReplyOsm::errorOccurred,
160 this, &QPlaceManagerEngineOsm::replyError);
163 reply->requestUrl = requestUrl.url(QUrl::None);
171 if (m_categories.isEmpty() && !m_categoriesReply) {
172 m_categoryLocales = m_locales;
173 m_categoryLocales.append(QLocale(QLocale::English));
174 fetchNextCategoryLocale();
177 QPlaceCategoriesReplyOsm *reply =
new QPlaceCategoriesReplyOsm(
this);
178 connect(reply, &QPlaceCategoriesReplyOsm::finished,
180 connect(reply, &QPlaceCategoriesReplyOsm::errorOccurred,
181 this, &QPlaceManagerEngineOsm::replyError);
184 if (!m_categories.isEmpty())
185 reply->emitFinished();
187 m_pendingCategoriesReply.append(reply);
193 Q_UNUSED(categoryId);
201 return m_subcategories.value(categoryId);
206 return m_categories.value(categoryId);
211 QList<QPlaceCategory> categories;
212 const QStringList subcategoriesList = m_subcategories.value(parentId);
213 for (
const QString &id : subcategoriesList)
214 categories.append(m_categories.value(id));
230 QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
231 reply->deleteLater();
233 QXmlStreamReader parser(reply);
234 while (!parser.atEnd() && parser.readNextStartElement()) {
235 if (parser.name() == QLatin1String(
"mediawiki"))
237 if (parser.name() == QLatin1String(
"page"))
239 if (parser.name() == QLatin1String(
"revision"))
241 if (parser.name() == QLatin1String(
"text")) {
243 QString page = parser.readElementText();
244 QRegularExpression regex(QStringLiteral(
"\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])"));
245 QRegularExpressionMatchIterator i = regex.globalMatch(page);
246 while (i.hasNext()) {
247 QRegularExpressionMatch match = i.next();
248 QString name = match.capturedView(1).toString();
249 QString tagKey = match.capturedView(2).toString();
250 QString tagValue = match.capturedView(3).toString();
251 QString op = match.capturedView(4).toString();
252 QString plural = match.capturedView(5).toString();
255 if (op != QLatin1String(
"-") || plural != QLatin1String(
"Y"))
258 if (!m_categories.contains(tagKey)) {
259 QPlaceCategory category;
260 category.setCategoryId(tagKey);
261 category.setName(nameForTagKey(tagKey));
262 m_categories.insert(category.categoryId(), category);
263 m_subcategories[QString()].append(tagKey);
264 emit categoryAdded(category, QString());
267 QPlaceCategory category;
268 category.setCategoryId(tagKey + QLatin1Char(
'=') + tagValue);
269 category.setName(name);
271 if (!m_categories.contains(category.categoryId())) {
272 m_categories.insert(category.categoryId(), category);
273 m_subcategories[tagKey].append(category.categoryId());
274 emit categoryAdded(category, tagKey);
279 parser.skipCurrentElement();
282 if (m_categories.isEmpty() && !m_categoryLocales.isEmpty()) {
283 fetchNextCategoryLocale();
286 m_categoryLocales.clear();
289 for (QPlaceCategoriesReplyOsm *reply : std::as_const(m_pendingCategoriesReply))
290 reply->emitFinished();
291 m_pendingCategoriesReply.clear();
296 for (QPlaceCategoriesReplyOsm *reply : std::as_const(m_pendingCategoriesReply))
297 reply->setError(QPlaceReply::CommunicationError, tr(
"Network request error"));
302 QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
304 emit finished(reply);
309 QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
311 emit errorOccurred(reply, errorCode, errorString);
316 if (m_categoryLocales.isEmpty()) {
317 qWarning(
"No locales specified to fetch categories for");
321 QLocale locale = m_categoryLocales.takeFirst();
324 QUrl requestUrl = QUrl(SpecialPhrasesBaseUrl + locale.name().left(2).toUpper());
326 m_categoriesReply = m_networkManager->get(QNetworkRequest(requestUrl));
327 connect(m_categoriesReply, &QNetworkReply::finished,
328 this, &QPlaceManagerEngineOsm::categoryReplyFinished);
329 connect(m_categoriesReply, &QNetworkReply::errorOccurred,
QStringList childCategoryIds(const QString &categoryId) const override
Returns the child category identifiers of the category corresponding to categoryId.
QPlaceReply * initializeCategories() override
Initializes the categories of the manager engine.
QList< QPlaceCategory > childCategories(const QString &parentId) const override
Returns a list of categories that are children of the category corresponding to parentId.
QList< QLocale > locales() const override
Returns a list of preferred locales.
QPlaceCategory category(const QString &categoryId) const override
Returns the category corresponding to the given categoryId.
QPlaceSearchReply * search(const QPlaceSearchRequest &request) override
Searches for places according to the parameters specified in request.
QString parentCategoryId(const QString &categoryId) const override
Returns the parent category identifier of the category corresponding to categoryId.
void setLocales(const QList< QLocale > &locales) override
Set the list of preferred locales.
~QPlaceManagerEngineOsm()