QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgstiles.h
Go to the documentation of this file.
1/***************************************************************************
2 qgstiles.h
3 --------------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSTILES_H
17#define QGSTILES_H
18
19#include "qgis.h"
20#include "qgis_core.h"
21#include "qgis_sip.h"
23#include "qgsreadwritecontext.h"
24#include "qgsrectangle.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31
42class CORE_EXPORT QgsTileXYZ
43{
44 public:
46 QgsTileXYZ( int tc = -1, int tr = -1, int tz = -1 )
47 : mColumn( tc )
48 , mRow( tr )
49 , mZoomLevel( tz )
50 {}
51
53 int column() const { return mColumn; }
55 int row() const { return mRow; }
57 int zoomLevel() const { return mZoomLevel; }
58
60 QString toString() const { return u"X=%1 Y=%2 Z=%3"_s.arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
61
62 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
63 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
64
65#ifdef SIP_RUN
66 // clang-format off
67 SIP_PYOBJECT __repr__();
68 % MethodCode
69 const QString str = u"<QgsTileXYZ: %1, %2, %3>"_s.arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
70 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
71 % End
72// clang-format on
73#endif
74
75 // clang-format off
76 private:
77 // clang-format on
78 int mColumn
79 = -1;
80 int mRow = -1;
81 int mZoomLevel = -1;
82};
83
84#ifndef SIP_RUN
85#if ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 6 ) ) || defined( __clang__ )
86#pragma GCC diagnostic push
87#pragma GCC diagnostic ignored "-Wattributes"
88#elif defined( _MSC_VER )
89__pragma( warning( push ) ) __pragma( warning( disable : 4273 ) )
90#endif
91#endif
92
98CORE_EXPORT inline uint qHash( QgsTileXYZ id ) SIP_SKIP
99{
100 const uint h1 = qHash( static_cast< quint64 >( id.column() ) );
101 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
102 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
103 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
104}
105
106#ifndef SIP_RUN
107#if ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 6 ) ) || defined( __clang__ )
108#pragma GCC diagnostic pop
109#elif defined( _MSC_VER )
110__pragma( warning( pop ) )
111#endif
112#endif
113
122class CORE_EXPORT QgsTileRange
123{
124 public:
126 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
127 : mStartColumn( c1 )
128 , mEndColumn( c2 )
129 , mStartRow( r1 )
130 , mEndRow( r2 )
131 {}
132
134 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
135
137 int startColumn() const { return mStartColumn; }
139 int endColumn() const { return mEndColumn; }
141 int startRow() const { return mStartRow; }
143 int endRow() const { return mEndRow; }
144
150 int count() const { return isValid() ? ( mEndRow - mStartRow + 1 ) * ( mEndColumn - mStartColumn + 1 ) : 0; }
151
152 private:
153 int mStartColumn = -1;
154 int mEndColumn = -1;
155 int mStartRow = -1;
156 int mEndRow = -1;
157};
158
159
170class CORE_EXPORT QgsTileMatrix
171{
172 public:
175
181 static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs, const QgsPointXY &z0TopLeftPoint, double z0Dimension, int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
182
184 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
185
191 QgsCoordinateReferenceSystem crs() const { return mCrs; }
192
199 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs; }
200
206 int zoomLevel() const { return mZoomLevel; }
207
214 void setZoomLevel( int level ) { mZoomLevel = level; }
215
217 int matrixWidth() const { return mMatrixWidth; }
218
220 int matrixHeight() const { return mMatrixHeight; }
221
223 QgsRectangle extent() const { return mExtent; }
224
230 double scale() const { return mScaleDenom; }
231
238 void setScale( double scale ) { mScaleDenom = scale; }
239
241 QgsRectangle tileExtent( QgsTileXYZ id ) const;
242
244 QgsPointXY tileCenter( QgsTileXYZ id ) const;
245
247 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
248
250 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
251
253 bool isRootTileMatrix() const { return mZoomLevel == 0; }
254
255 private:
259 int mZoomLevel = -1;
261 int mMatrixWidth = 0;
263 int mMatrixHeight = 0;
265 QgsRectangle mExtent;
267 double mScaleDenom = 0;
269 double mTileXSpan = 0;
271 double mTileYSpan = 0;
272
273 friend class QgsTileMatrixSet;
274};
275
276
283class CORE_EXPORT QgsTileMatrixSet
284{
285 public:
287
288 virtual ~QgsTileMatrixSet() = default;
289
293 bool isEmpty() const;
294
298 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
299
303 QgsTileMatrix tileMatrix( int zoom ) const;
304
311
317 void setRootMatrix( const QgsTileMatrix &matrix );
318
324 void addMatrix( const QgsTileMatrix &matrix );
325
331 int minimumZoom() const;
332
338 int maximumZoom() const;
339
345
358
366
372 double scaleToZoom( double scale ) const;
373
381 int scaleToZoomLevel( double scale, bool clamp = true ) const;
382
388 double scaleForRenderContext( const QgsRenderContext &context ) const;
389
395 double calculateTileScaleForMap( double actualMapScale, const QgsCoordinateReferenceSystem &mapCrs, const QgsRectangle &mapExtent, const QSize mapSize, const double mapDpi ) const;
396
402 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
403
407 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
408
415
422
428 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
429
430 protected:
433
434 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
436 QMap< int, QgsTileMatrix > mTileMatrices;
438};
439
440#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3534
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
Definition qgis.h:3535
TileAvailability
Possible availability states for a tile within a tile matrix.
Definition qgis.h:5963
Represents a coordinate reference system (CRS).
Represents a 2D point.
Definition qgspointxy.h:62
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Writes the set to an XML element.
Definition qgstiles.cpp:389
Qgis::ScaleToTileZoomLevelMethod mScaleToTileZoomMethod
Definition qgstiles.h:437
void addGoogleCrs84QuadTiles(int minimumZoom=0, int maximumZoom=14)
Adds tile matrices corresponding to the standard web mercator/GoogleCRS84Quad setup.
Definition qgstiles.cpp:149
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system associated with the tiles.
Definition qgstiles.cpp:224
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:431
double scaleForRenderContext(const QgsRenderContext &context) const
Calculates the correct scale to use for the tiles when rendered using the specified render context.
Definition qgstiles.cpp:305
int minimumZoom() const
Returns the minimum zoom level for tiles present in the set.
Definition qgstiles.cpp:182
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
Definition qgstiles.cpp:232
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:436
int maximumZoom() const
Returns the maximum zoom level for tiles present in the set.
Definition qgstiles.cpp:193
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Definition qgstiles.cpp:162
Qgis::TileAvailability tileAvailability(QgsTileXYZ id) const
Returns the availability of the given tile in this matrix.
Definition qgstiles.cpp:219
QgsTileMatrix rootMatrix() const
Returns the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:167
virtual bool readXml(const QDomElement &element, QgsReadWriteContext &context)
Reads the set from an XML element.
Definition qgstiles.cpp:344
void addMatrix(const QgsTileMatrix &matrix)
Adds a matrix to the set.
Definition qgstiles.cpp:177
double calculateTileScaleForMap(double actualMapScale, const QgsCoordinateReferenceSystem &mapCrs, const QgsRectangle &mapExtent, const QSize mapSize, const double mapDpi) const
Calculates the correct scale to use for the tiles when rendered using the specified map properties.
Definition qgstiles.cpp:310
void dropMatricesOutsideZoomRange(int minimumZoom, int maximumZoom)
Deletes any existing matrices which fall outside the zoom range specified by minimumZoom to maximumZo...
Definition qgstiles.cpp:204
virtual ~QgsTileMatrixSet()=default
bool isEmpty() const
Returns true if the matrix set is empty.
Definition qgstiles.cpp:144
QgsTileMatrix mRootMatrix
Definition qgstiles.h:435
void setRootMatrix(const QgsTileMatrix &matrix)
Sets the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:172
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:414
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:421
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:432
int scaleToZoomLevel(double scale, bool clamp=true) const
Finds the best fitting (integer) zoom level given a map scale denominator.
Definition qgstiles.cpp:289
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:171
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:199
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
Definition qgstiles.cpp:27
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:238
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:223
friend class QgsTileMatrixSet
Definition qgstiles.h:273
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:217
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:191
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0).
Definition qgstiles.h:253
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:230
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:214
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:220
static QgsTileMatrix fromTileMatrix(int zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
Definition qgstiles.cpp:64
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:206
static QgsTileMatrix fromCustomDef(int zoomLevel, const QgsCoordinateReferenceSystem &crs, const QgsPointXY &z0TopLeftPoint, double z0Dimension, int z0MatrixWidth=1, int z0MatrixHeight=1)
Returns a tile matrix for a specific CRS, top left point, zoom level 0 dimension in CRS units.
Definition qgstiles.cpp:35
A range of tiles in a tile matrix.
Definition qgstiles.h:123
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:139
QgsTileRange(int c1=-1, int c2=-1, int r1=-1, int r2=-1)
Constructs a range of tiles from given span of columns and rows.
Definition qgstiles.h:126
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:143
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:141
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:137
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative).
Definition qgstiles.h:134
int count() const
Returns the total number of tiles in the range.
Definition qgstiles.h:150
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:43
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:60
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:57
QgsTileXYZ(int tc=-1, int tr=-1, int tz=-1)
Constructs a tile identifier from given column, row and zoom level indices.
Definition qgstiles.h:46
bool operator==(const QgsTileXYZ &other) const
Definition qgstiles.h:62
int column() const
Returns tile's column index (X).
Definition qgstiles.h:53
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:63
int row() const
Returns tile's row index (Y).
Definition qgstiles.h:55
#define SIP_SKIP
Definition qgis_sip.h:133
CORE_EXPORT uint qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:98