File indexing completed on 2024-05-12 04:42:18

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef OSM_OVERPASSQUERY_H
0008 #define OSM_OVERPASSQUERY_H
0009 
0010 #include "kosm_export.h"
0011 
0012 #include "datatypes.h"
0013 
0014 #include <QObject>
0015 #include <QRectF>
0016 
0017 class QNetworkReply;
0018 
0019 namespace OSM {
0020 
0021 /** An Overpass QL query job, to be executed by OverpassQueryManager.
0022  *  @note Use this wrongly can cause excessive load on Overpass servers,
0023  *  so only use this when you know what you are doing!
0024  */
0025 class KOSM_EXPORT OverpassQuery : public QObject
0026 {
0027     Q_OBJECT
0028 public:
0029     explicit OverpassQuery(QObject *parent = nullptr);
0030     ~OverpassQuery();
0031 
0032     /** Returns the raw (without bbox replacement) query string. */
0033     [[nodiscard]] QString query() const;
0034     /** Returns the query with @p bbox applied. */
0035     [[nodiscard]] QString query(const QRectF &bbox) const;
0036     /** Overpass QL query string.
0037      *  Can contain the '{{bbox}}' bounding box placeholder also supported by Overpass Turbo.
0038      */
0039     void setQuery(const QString &query);
0040 
0041     /** Bounding box for this query, values in degree. */
0042     [[nodiscard]] QRectF boundingBox() const;
0043     /** Set the bounding box for this query, values in degree. */
0044     void setBoundingBox(const QRectF &bbox);
0045 
0046     /** Tile size in which the bounding box is broken down for querying.
0047      *  Values in degree.
0048      */
0049     [[nodiscard]] QSizeF tileSize() const;
0050     /** Sets the tile size in which the bounding box is broken down for querying.
0051      *  Values in degree.
0052      */
0053     void setTileSize(const QSizeF &tileSize);
0054     /** Minimum tile size to which tiles can be broken down in case of query timeouts. */
0055     [[nodiscard]] QSizeF minimumTileSize() const;
0056     /** Sets the minimum tile size.
0057      *  Should be smaller than tile size by a power of 2.
0058      */
0059     void setMinimumTileSize(const QSizeF &minTileSize);
0060 
0061     /** Error codes. */
0062     enum Error {
0063         NoError,
0064         QueryError, ///< generic query error
0065         QueryTimeout, ///< query exceeded its execution time budget
0066         NetworkError,
0067     };
0068     /** Error code of this query job. */
0069     [[nodiscard]] Error error() const;
0070 
0071     /** Query result data set. */
0072     [[nodiscard]] const DataSet& result() const;
0073     [[nodiscard]] DataSet&& takeResult();
0074 
0075 Q_SIGNALS:
0076     void finished();
0077 
0078 private:
0079     friend class OverpassQueryManager;
0080     friend class OverpassQueryManagerPrivate;
0081 
0082     [[nodiscard]] Error processReply(QNetworkReply *reply);
0083 
0084     QString m_query;
0085     QRectF m_bbox = { -180.0, -90.0, 360.0, 180.0 };
0086     QSizeF m_tileSize = { 360.0, 180.0 };
0087     QSizeF m_minimumTileSize = { 1.0, 1.0 };
0088     Error m_error = NoError;
0089     OSM::DataSet m_result;
0090 };
0091 
0092 }
0093 
0094 #endif // OSM_OVERPASSQUERY_H