QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
27 class QgsRenderContext;
28 
37 class CORE_EXPORT QgsTileXYZ
38 {
39  public:
41  QgsTileXYZ( int tc = -1, int tr = -1, int tz = -1 )
42  : mColumn( tc ), mRow( tr ), mZoomLevel( tz )
43  {
44  }
45 
47  int column() const { return mColumn; }
49  int row() const { return mRow; }
51  int zoomLevel() const { return mZoomLevel; }
52 
54  QString toString() const { return QStringLiteral( "X=%1 Y=%2 Z=%3" ).arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
55 
56  private:
57  int mColumn;
58  int mRow;
59  int mZoomLevel;
60 };
61 
62 
70 class CORE_EXPORT QgsTileRange
71 {
72  public:
74  QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
75  : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
76 
78  bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
79 
81  int startColumn() const { return mStartColumn; }
83  int endColumn() const { return mEndColumn; }
85  int startRow() const { return mStartRow; }
87  int endRow() const { return mEndRow; }
88 
89  private:
90  int mStartColumn;
91  int mEndColumn;
92  int mStartRow;
93  int mEndRow;
94 };
95 
96 
107 class CORE_EXPORT QgsTileMatrix
108 {
109  public:
110 
112  static QgsTileMatrix fromWebMercator( int zoomLevel );
113 
119  static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs,
120  const QgsPointXY &z0TopLeftPoint, double z0Dimension,
121  int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
122 
124  static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
125 
131  QgsCoordinateReferenceSystem crs() const { return mCrs; }
132 
139  void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
140 
146  int zoomLevel() const { return mZoomLevel; }
147 
154  void setZoomLevel( int level ) { mZoomLevel = level; }
155 
157  int matrixWidth() const { return mMatrixWidth; }
158 
160  int matrixHeight() const { return mMatrixHeight; }
161 
163  QgsRectangle extent() const { return mExtent; }
164 
170  double scale() const { return mScaleDenom; }
171 
178  void setScale( double scale ) { mScaleDenom = scale; }
179 
181  QgsRectangle tileExtent( QgsTileXYZ id ) const;
182 
184  QgsPointXY tileCenter( QgsTileXYZ id ) const;
185 
187  QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
188 
190  QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
191 
193  bool isRootTileMatrix() const { return mZoomLevel == 0; }
194 
195  private:
199  int mZoomLevel = -1;
201  int mMatrixWidth;
203  int mMatrixHeight;
205  QgsRectangle mExtent;
207  double mScaleDenom;
209  double mTileXSpan;
211  double mTileYSpan;
212 
213  friend class QgsTileMatrixSet;
214 };
215 
216 
223 class CORE_EXPORT QgsTileMatrixSet
224 {
225 
226  public:
227 
228  virtual ~QgsTileMatrixSet() = default;
229 
233  void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
234 
238  QgsTileMatrix tileMatrix( int zoom ) const;
239 
245  void addMatrix( const QgsTileMatrix &matrix );
246 
252  int minimumZoom() const;
253 
259  int maximumZoom() const;
260 
265  void dropMatricesOutsideZoomRange( int minimumZoom, int maximumZoom );
266 
274 
280  double scaleToZoom( double scale ) const;
281 
287  int scaleToZoomLevel( double scale ) const;
288 
294  double scaleForRenderContext( const QgsRenderContext &context ) const;
295 
301  double calculateTileScaleForMap( double actualMapScale,
302  const QgsCoordinateReferenceSystem &mapCrs,
303  const QgsRectangle &mapExtent,
304  const QSize mapSize,
305  const double mapDpi
306  ) const;
307 
313  virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
314 
318  virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
319 
325  Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const { return mScaleToTileZoomMethod; }
326 
332  void setScaleToTileZoomMethod( Qgis::ScaleToTileZoomLevelMethod method ) { mScaleToTileZoomMethod = method; }
333 
334  private:
335 
336  QMap< int, QgsTileMatrix > mTileMatrices;
338 };
339 
340 #endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition: qgis.h:1267
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
This class represents a coordinate reference system (CRS).
A class to represent a 2D point.
Definition: qgspointxy.h:59
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
Defines a set of tile matrices for multiple zoom levels.
Definition: qgstiles.h:224
virtual ~QgsTileMatrixSet()=default
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition: qgstiles.h:325
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition: qgstiles.h:332
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition: qgstiles.h:108
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition: qgstiles.h:139
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition: qgstiles.h:178
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition: qgstiles.h:163
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition: qgstiles.h:157
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition: qgstiles.h:131
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0)
Definition: qgstiles.h:193
double scale() const
Returns scale denominator of the tile matrix.
Definition: qgstiles.h:170
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition: qgstiles.h:154
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition: qgstiles.h:160
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition: qgstiles.h:146
Range of tiles in a tile matrix to be rendered.
Definition: qgstiles.h:71
int endColumn() const
Returns index of the last column in the range.
Definition: qgstiles.h:83
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:74
int endRow() const
Returns index of the last row in the range.
Definition: qgstiles.h:87
int startRow() const
Returns index of the first row in the range.
Definition: qgstiles.h:85
int startColumn() const
Returns index of the first column in the range.
Definition: qgstiles.h:81
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
Definition: qgstiles.h:78
Stores coordinates of a tile in a tile matrix set.
Definition: qgstiles.h:38
QString toString() const
Returns tile coordinates in a formatted string.
Definition: qgstiles.h:54
int zoomLevel() const
Returns tile's zoom level (Z)
Definition: qgstiles.h:51
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:41
int column() const
Returns tile's column index (X)
Definition: qgstiles.h:47
int row() const
Returns tile's row index (Y)
Definition: qgstiles.h:49
const QgsCoordinateReferenceSystem & crs