File indexing completed on 2024-05-05 04:42:57

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "overpassquery.h"
0008 #include "io.h"
0009 
0010 #include <QNetworkReply>
0011 
0012 using namespace OSM;
0013 
0014 OverpassQuery::OverpassQuery(QObject* parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 OverpassQuery::~OverpassQuery() = default;
0020 
0021 QString OverpassQuery::query() const
0022 {
0023     return m_query;
0024 }
0025 
0026 QString OverpassQuery::query(const QRectF &bbox) const
0027 {
0028     const QString bboxStr = QString::number(bbox.y()) + QLatin1Char(',') + QString::number(bbox.x()) + QLatin1Char(',') + QString::number(bbox.y() + bbox.height()) + QLatin1Char(',') + QString::number(bbox.x() + bbox.width());
0029     auto q = m_query;
0030     return q.replace(QLatin1String("{{bbox}}"), bboxStr);
0031 }
0032 
0033 void OverpassQuery::setQuery(const QString &query)
0034 {
0035     m_query = query;
0036 }
0037 
0038 QRectF OverpassQuery::boundingBox() const
0039 {
0040     return m_bbox;
0041 }
0042 
0043 void OverpassQuery::setBoundingBox(const QRectF &bbox)
0044 {
0045     m_bbox = bbox;
0046 }
0047 
0048 QSizeF OverpassQuery::tileSize() const
0049 {
0050     return m_tileSize;
0051 }
0052 
0053 void OverpassQuery::setTileSize(const QSizeF &tileSize)
0054 {
0055     m_tileSize = tileSize;
0056 }
0057 
0058 QSizeF OverpassQuery::minimumTileSize() const
0059 {
0060     return m_minimumTileSize;
0061 }
0062 
0063 void OverpassQuery::setMinimumTileSize(const QSizeF &minTileSize)
0064 {
0065     m_minimumTileSize = minTileSize;
0066 }
0067 
0068 OverpassQuery::Error OverpassQuery::error() const
0069 {
0070     return m_error;
0071 }
0072 
0073 const DataSet& OverpassQuery::result() const
0074 {
0075     return m_result;
0076 }
0077 
0078 DataSet&& OverpassQuery::takeResult()
0079 {
0080     return std::move(m_result);
0081 }
0082 
0083 OverpassQuery::Error OverpassQuery::processReply(QNetworkReply *reply)
0084 {
0085     auto reader = OSM::IO::readerForMimeType(u"application/vnd.openstreetmap.data+xml", &m_result);
0086     if (!reader) {
0087         qWarning() << "No support for reading OSM XML available!";
0088         return QueryError;
0089     }
0090     reader->read(reply);
0091     if (!reader->errorString().isEmpty()) {
0092         qWarning() << "Query error:" << reader->errorString();
0093         qWarning() << "Request:" << reply->request().url();
0094         return reader->errorString().contains(QLatin1String("timed out"), Qt::CaseInsensitive) ? QueryTimeout : QueryError;
0095     }
0096     qDebug() << "Nodes:" << m_result.nodes.size();
0097     qDebug() << "Ways:" << m_result.ways.size();
0098     qDebug() << "Relations:" << m_result.relations.size();
0099     return NoError;
0100 }