8#include <QtCore/QElapsedTimer>
9#include <QtCore/QLocale>
10#include <QtCore/QRegularExpression>
11#include <QtCore/QUrlQuery>
12#include <QtCore/QXmlStreamReader>
14#include <QtNetwork/QNetworkAccessManager>
15#include <QtNetwork/QNetworkRequest>
16#include <QtNetwork/QNetworkReply>
18#include <QtPositioning/QGeoCircle>
20#include <QtLocation/QPlaceCategory>
21#include <QtLocation/QPlaceSearchRequest>
22#include <QtLocation/private/unsupportedreplies_p.h>
26QString SpecialPhrasesBaseUrl = QStringLiteral(
"http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/");
28QString nameForTagKey(
const QString &tagKey)
30 if (tagKey == QLatin1String(
"aeroway"))
31 return QPlaceManagerEngineOsm::tr(
"Aeroway");
32 else if (tagKey == QLatin1String(
"amenity"))
33 return QPlaceManagerEngineOsm::tr(
"Amenity");
34 else if (tagKey == QLatin1String(
"building"))
35 return QPlaceManagerEngineOsm::tr(
"Building");
36 else if (tagKey == QLatin1String(
"highway"))
37 return QPlaceManagerEngineOsm::tr(
"Highway");
38 else if (tagKey == QLatin1String(
"historic"))
39 return QPlaceManagerEngineOsm::tr(
"Historic");
40 else if (tagKey == QLatin1String(
"landuse"))
41 return QPlaceManagerEngineOsm::tr(
"Land use");
42 else if (tagKey == QLatin1String(
"leisure"))
43 return QPlaceManagerEngineOsm::tr(
"Leisure");
44 else if (tagKey == QLatin1String(
"man_made"))
45 return QPlaceManagerEngineOsm::tr(
"Man made");
46 else if (tagKey == QLatin1String(
"natural"))
47 return QPlaceManagerEngineOsm::tr(
"Natural");
48 else if (tagKey == QLatin1String(
"place"))
49 return QPlaceManagerEngineOsm::tr(
"Place");
50 else if (tagKey == QLatin1String(
"railway"))
51 return QPlaceManagerEngineOsm::tr(
"Railway");
52 else if (tagKey == QLatin1String(
"shop"))
53 return QPlaceManagerEngineOsm::tr(
"Shop");
54 else if (tagKey == QLatin1String(
"tourism"))
55 return QPlaceManagerEngineOsm::tr(
"Tourism");
56 else if (tagKey == QLatin1String(
"waterway"))
57 return QPlaceManagerEngineOsm::tr(
"Waterway");
65 QGeoServiceProvider::Error *error,
67: QPlaceManagerEngine(parameters), m_networkManager(
new QNetworkAccessManager(
this)),
70 if (parameters.contains(QStringLiteral(
"osm.useragent")))
71 m_userAgent = parameters.value(QStringLiteral(
"osm.useragent")).toString().toLatin1();
73 m_userAgent =
"Qt Location based application";
75 if (parameters.contains(QStringLiteral(
"osm.places.host")))
76 m_urlPrefix = parameters.value(QStringLiteral(
"osm.places.host")).toString();
78 m_urlPrefix = QStringLiteral(
"http://nominatim.openstreetmap.org/search");
81 if (parameters.contains(QStringLiteral(
"osm.places.debug_query")))
82 m_debugQuery = parameters.value(QStringLiteral(
"osm.places.debug_query")).toBool();
84 if (parameters.contains(QStringLiteral(
"osm.places.page_size"))
85 && parameters.value(QStringLiteral(
"osm.places.page_size")).canConvert<
int>())
86 m_pageSize = parameters.value(QStringLiteral(
"osm.places.page_size")).toInt();
88 *error = QGeoServiceProvider::NoError;
98 bool unsupported =
false;
101 unsupported |= request.visibilityScope() != QLocation::UnspecifiedVisibility &&
102 request.visibilityScope() != QLocation::PublicVisibility;
103 unsupported |= request.searchTerm().isEmpty() && request.categories().isEmpty();
106 return QPlaceManagerEngine::search(request);
108 QUrlQuery queryItems;
110 queryItems.addQueryItem(QStringLiteral(
"format"), QStringLiteral(
"jsonv2"));
114 QGeoRectangle boundingBox = request.searchArea().boundingGeoRectangle();
116 if (!boundingBox.isEmpty()) {
117 queryItems.addQueryItem(QStringLiteral(
"bounded"), QStringLiteral(
"1"));
119 coordinates = QString::number(boundingBox.topLeft().longitude()) + QLatin1Char(
',') +
120 QString::number(boundingBox.topLeft().latitude()) + QLatin1Char(
',') +
121 QString::number(boundingBox.bottomRight().longitude()) + QLatin1Char(
',') +
122 QString::number(boundingBox.bottomRight().latitude());
123 queryItems.addQueryItem(QStringLiteral(
"viewbox"), coordinates);
126 QStringList queryParts;
127 if (!request.searchTerm().isEmpty())
128 queryParts.append(request.searchTerm());
130 for (
const QPlaceCategory &category : request.categories()) {
131 QString id = category.categoryId();
132 queryParts.append(QLatin1Char(
'[') + id + QLatin1Char(
']'));
135 queryItems.addQueryItem(QStringLiteral(
"q"), queryParts.join(QLatin1Char(
'+')));
137 QVariantMap parameters = request.searchContext().toMap();
139 QStringList placeIds = parameters.value(QStringLiteral(
"ExcludePlaceIds")).toStringList();
140 if (!placeIds.isEmpty())
141 queryItems.addQueryItem(QStringLiteral(
"exclude_place_ids"), placeIds.join(QLatin1Char(
',')));
143 queryItems.addQueryItem(QStringLiteral(
"addressdetails"), QStringLiteral(
"1"));
144 queryItems.addQueryItem(QStringLiteral(
"limit"), (request.limit() > 0) ? QString::number(request.limit())
145 : QString::number(m_pageSize));
147 QUrl requestUrl(m_urlPrefix);
148 requestUrl.setQuery(queryItems);
150 QNetworkRequest rq(requestUrl);
151 rq.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
152 QNetworkReply *networkReply = m_networkManager->get(rq);
155 connect(reply, &QPlaceSearchReplyOsm::finished,
156 this, &QPlaceManagerEngineOsm::replyFinished);
157 connect(reply, &QPlaceSearchReplyOsm::errorOccurred,
158 this, &QPlaceManagerEngineOsm::replyError);
161 reply->requestUrl = requestUrl.url(QUrl::None);
169 if (m_categories.isEmpty() && !m_categoriesReply) {
170 m_categoryLocales = m_locales;
171 m_categoryLocales.append(QLocale(QLocale::English));
172 fetchNextCategoryLocale();
175 QPlaceCategoriesReplyOsm *reply =
new QPlaceCategoriesReplyOsm(
this);
176 connect(reply, &QPlaceCategoriesReplyOsm::finished,
177 this, &QPlaceManagerEngineOsm::replyFinished);
178 connect(reply, &QPlaceCategoriesReplyOsm::errorOccurred,
179 this, &QPlaceManagerEngineOsm::replyError);
182 if (!m_categories.isEmpty())
183 reply->emitFinished();
185 m_pendingCategoriesReply.append(reply);
191 Q_UNUSED(categoryId);
199 return m_subcategories.value(categoryId);
204 return m_categories.value(categoryId);
209 QList<QPlaceCategory> categories;
210 for (
const QString &id : m_subcategories.value(parentId))
211 categories.append(m_categories.value(id));
227 QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
228 reply->deleteLater();
230 QXmlStreamReader parser(reply);
231 while (!parser.atEnd() && parser.readNextStartElement()) {
232 if (parser.name() == QLatin1String(
"mediawiki"))
234 if (parser.name() == QLatin1String(
"page"))
236 if (parser.name() == QLatin1String(
"revision"))
238 if (parser.name() == QLatin1String(
"text")) {
240 QString page = parser.readElementText();
241 QRegularExpression regex(QStringLiteral(
"\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])"));
242 QRegularExpressionMatchIterator i = regex.globalMatch(page);
243 while (i.hasNext()) {
244 QRegularExpressionMatch match = i.next();
245 QString name = match.capturedView(1).toString();
246 QString tagKey = match.capturedView(2).toString();
247 QString tagValue = match.capturedView(3).toString();
248 QString op = match.capturedView(4).toString();
249 QString plural = match.capturedView(5).toString();
252 if (op != QLatin1String(
"-") || plural != QLatin1String(
"Y"))
255 if (!m_categories.contains(tagKey)) {
256 QPlaceCategory category;
257 category.setCategoryId(tagKey);
258 category.setName(nameForTagKey(tagKey));
259 m_categories.insert(category.categoryId(), category);
260 m_subcategories[QString()].append(tagKey);
261 emit categoryAdded(category, QString());
264 QPlaceCategory category;
265 category.setCategoryId(tagKey + QLatin1Char(
'=') + tagValue);
266 category.setName(name);
268 if (!m_categories.contains(category.categoryId())) {
269 m_categories.insert(category.categoryId(), category);
270 m_subcategories[tagKey].append(category.categoryId());
271 emit categoryAdded(category, tagKey);
276 parser.skipCurrentElement();
279 if (m_categories.isEmpty() && !m_categoryLocales.isEmpty()) {
280 fetchNextCategoryLocale();
283 m_categoryLocales.clear();
286 for (QPlaceCategoriesReplyOsm *reply : m_pendingCategoriesReply)
287 reply->emitFinished();
288 m_pendingCategoriesReply.clear();
293 for (QPlaceCategoriesReplyOsm *reply : m_pendingCategoriesReply)
294 reply->setError(QPlaceReply::CommunicationError, tr(
"Network request error"));
299 QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
301 emit finished(reply);
306 QPlaceReply *reply = qobject_cast<QPlaceReply *>(sender());
308 emit errorOccurred(reply, errorCode, errorString);
313 if (m_categoryLocales.isEmpty()) {
314 qWarning(
"No locales specified to fetch categories for");
318 QLocale locale = m_categoryLocales.takeFirst();
321 QUrl requestUrl = QUrl(SpecialPhrasesBaseUrl + locale.name().left(2).toUpper());
323 m_categoriesReply = m_networkManager->get(QNetworkRequest(requestUrl));
324 connect(m_categoriesReply, &QNetworkReply::finished,
325 this, &QPlaceManagerEngineOsm::categoryReplyFinished);
326 connect(m_categoriesReply, &QNetworkReply::errorOccurred,
327 this, &QPlaceManagerEngineOsm::categoryReplyError);
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()