QGIS API Documentation 3.43.0-Master (ac9f54ad1f7)
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_core.h"
20#include "qgis_sip.h"
21
22#include "qgis.h"
23#include "qgsrectangle.h"
25#include "qgsreadwritecontext.h"
26
28
39class CORE_EXPORT QgsTileXYZ
40{
41 public:
43 QgsTileXYZ( int tc = -1, int tr = -1, int tz = -1 )
44 : mColumn( tc ), mRow( tr ), mZoomLevel( tz )
45 {
46 }
47
49 int column() const { return mColumn; }
51 int row() const { return mRow; }
53 int zoomLevel() const { return mZoomLevel; }
54
56 QString toString() const { return QStringLiteral( "X=%1 Y=%2 Z=%3" ).arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
57
58 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
59 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
60
61#ifdef SIP_RUN
62 SIP_PYOBJECT __repr__();
63 % MethodCode
64 const QString str = QStringLiteral( "<QgsTileXYZ: %1, %2, %3>" ).arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
65 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
66 % End
67#endif
68
69 private:
70 int mColumn = -1;
71 int mRow = -1;
72 int mZoomLevel = -1;
73};
74
80CORE_EXPORT inline uint qHash( QgsTileXYZ id ) SIP_SKIP
81{
82 const uint h1 = qHash( static_cast< quint64 >( id.column( ) ) );
83 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
84 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
85 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
86}
87
88
97class CORE_EXPORT QgsTileRange
98{
99 public:
101 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
102 : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
103
105 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
106
108 int startColumn() const { return mStartColumn; }
110 int endColumn() const { return mEndColumn; }
112 int startRow() const { return mStartRow; }
114 int endRow() const { return mEndRow; }
115
121 int count() const { return isValid() ? ( mEndRow - mStartRow + 1 ) * ( mEndColumn - mStartColumn + 1 ) : 0; }
122
123 private:
124 int mStartColumn = -1;
125 int mEndColumn = -1;
126 int mStartRow = -1;
127 int mEndRow = -1;
128};
129
130
141class CORE_EXPORT QgsTileMatrix
142{
143 public:
144
146 static QgsTileMatrix fromWebMercator( int zoomLevel );
147
153 static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs,
154 const QgsPointXY &z0TopLeftPoint, double z0Dimension,
155 int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
156
158 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
159
165 QgsCoordinateReferenceSystem crs() const { return mCrs; }
166
173 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
174
180 int zoomLevel() const { return mZoomLevel; }
181
188 void setZoomLevel( int level ) { mZoomLevel = level; }
189
191 int matrixWidth() const { return mMatrixWidth; }
192
194 int matrixHeight() const { return mMatrixHeight; }
195
197 QgsRectangle extent() const { return mExtent; }
198
204 double scale() const { return mScaleDenom; }
205
212 void setScale( double scale ) { mScaleDenom = scale; }
213
215 QgsRectangle tileExtent( QgsTileXYZ id ) const;
216
218 QgsPointXY tileCenter( QgsTileXYZ id ) const;
219
221 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
222
224 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
225
227 bool isRootTileMatrix() const { return mZoomLevel == 0; }
228
229 private:
233 int mZoomLevel = -1;
235 int mMatrixWidth = 0;
237 int mMatrixHeight = 0;
239 QgsRectangle mExtent;
241 double mScaleDenom = 0;
243 double mTileXSpan = 0;
245 double mTileYSpan = 0;
246
247 friend class QgsTileMatrixSet;
248};
249
250
257class CORE_EXPORT QgsTileMatrixSet
258{
259
260 public:
261
263
264 virtual ~QgsTileMatrixSet() = default;
265
269 bool isEmpty() const;
270
274 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
275
279 QgsTileMatrix tileMatrix( int zoom ) const;
280
286 QgsTileMatrix rootMatrix() const;
287
293 void setRootMatrix( const QgsTileMatrix &matrix );
294
300 void addMatrix( const QgsTileMatrix &matrix );
301
307 int minimumZoom() const;
308
314 int maximumZoom() const;
315
320 void dropMatricesOutsideZoomRange( int minimumZoom, int maximumZoom );
321
333 Qgis::TileAvailability tileAvailability( QgsTileXYZ id ) const;
334
342
348 double scaleToZoom( double scale ) const;
349
357 int scaleToZoomLevel( double scale, bool clamp = true ) const;
358
364 double scaleForRenderContext( const QgsRenderContext &context ) const;
365
371 double calculateTileScaleForMap( double actualMapScale,
372 const QgsCoordinateReferenceSystem &mapCrs,
373 const QgsRectangle &mapExtent,
374 const QSize mapSize,
375 const double mapDpi
376 ) const;
377
383 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
384
388 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
389
395 Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const { return mScaleToTileZoomMethod; }
396
402 void setScaleToTileZoomMethod( Qgis::ScaleToTileZoomLevelMethod method ) { mScaleToTileZoomMethod = method; }
403
409 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
410
411 protected:
414
415 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
417 QMap< int, QgsTileMatrix > mTileMatrices;
419};
420
421#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3288
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
TileAvailability
Possible availability states for a tile within a tile matrix.
Definition qgis.h:5403
Represents a coordinate reference system (CRS).
Represents a 2D point.
Definition qgspointxy.h:60
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.
Defines a set of tile matrices for multiple zoom levels.
Definition qgstiles.h:258
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:412
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:417
virtual ~QgsTileMatrixSet()=default
QgsTileMatrix mRootMatrix
Definition qgstiles.h:416
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:395
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:402
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:413
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:142
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:173
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:212
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:197
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:191
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:165
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0)
Definition qgstiles.h:227
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:204
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:188
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:194
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:180
A range of tiles in a tile matrix.
Definition qgstiles.h:98
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:110
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:101
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:114
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:112
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:108
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
Definition qgstiles.h:105
int count() const
Returns the total number of tiles in the range.
Definition qgstiles.h:121
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:40
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:56
int zoomLevel() const
Returns tile's zoom level (Z)
Definition qgstiles.h:53
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:43
bool operator==(const QgsTileXYZ &other) const
Definition qgstiles.h:58
int column() const
Returns tile's column index (X)
Definition qgstiles.h:49
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:59
int row() const
Returns tile's row index (Y)
Definition qgstiles.h:51
#define SIP_SKIP
Definition qgis_sip.h:126
CORE_EXPORT uint qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:80
const QgsCoordinateReferenceSystem & crs