QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  bool isEmpty() const;
234 
238  void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
239 
243  QgsTileMatrix tileMatrix( int zoom ) const;
244 
250  void addMatrix( const QgsTileMatrix &matrix );
251 
257  int minimumZoom() const;
258 
264  int maximumZoom() const;
265 
270  void dropMatricesOutsideZoomRange( int minimumZoom, int maximumZoom );
271 
279 
285  double scaleToZoom( double scale ) const;
286 
292  int scaleToZoomLevel( double scale ) const;
293 
299  double scaleForRenderContext( const QgsRenderContext &context ) const;
300 
306  double calculateTileScaleForMap( double actualMapScale,
307  const QgsCoordinateReferenceSystem &mapCrs,
308  const QgsRectangle &mapExtent,
309  const QSize mapSize,
310  const double mapDpi
311  ) const;
312 
318  virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
319 
323  virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
324 
330  Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const { return mScaleToTileZoomMethod; }
331 
337  void setScaleToTileZoomMethod( Qgis::ScaleToTileZoomLevelMethod method ) { mScaleToTileZoomMethod = method; }
338 
339  private:
340 
341  QMap< int, QgsTileMatrix > mTileMatrices;
343 };
344 
345 #endif // QGSTILES_H
QgsTileMatrix::setZoomLevel
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition: qgstiles.h:154
Qgis::ScaleToTileZoomLevelMethod::MapBox
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
QgsTileXYZ
Stores coordinates of a tile in a tile matrix set. Tile matrix is identified by the zoomLevel(),...
Definition: qgstiles.h:37
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgsrectangle.h
QgsTileRange
Range of tiles in a tile matrix to be rendered. The selection is rectangular, given by start/end row ...
Definition: qgstiles.h:70
qgsreadwritecontext.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsTileXYZ::QgsTileXYZ
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
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsTileMatrix::setCrs
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition: qgstiles.h:139
Qgis::ScaleToTileZoomLevelMethod
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition: qgis.h:1623
QgsTileMatrix::crs
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition: qgstiles.h:131
QgsTileMatrix::isRootTileMatrix
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0)
Definition: qgstiles.h:193
QgsTileMatrix::zoomLevel
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition: qgstiles.h:146
QgsTileRange::endRow
int endRow() const
Returns index of the last row in the range.
Definition: qgstiles.h:87
QgsTileMatrix
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition: qgstiles.h:107
QgsTileMatrix::matrixHeight
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition: qgstiles.h:160
QgsTileMatrix::extent
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition: qgstiles.h:163
qgis_sip.h
QgsTileRange::QgsTileRange
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
QgsTileMatrixSet
Defines a set of tile matrices for multiple zoom levels.
Definition: qgstiles.h:223
QgsTileXYZ::toString
QString toString() const
Returns tile coordinates in a formatted string.
Definition: qgstiles.h:54
QgsTileRange::isValid
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
Definition: qgstiles.h:78
QgsTileRange::endColumn
int endColumn() const
Returns index of the last column in the range.
Definition: qgstiles.h:83
QgsTileXYZ::zoomLevel
int zoomLevel() const
Returns tile's zoom level (Z)
Definition: qgstiles.h:51
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsTileRange::startRow
int startRow() const
Returns index of the first row in the range.
Definition: qgstiles.h:85
QgsTileMatrix::setScale
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition: qgstiles.h:178
QgsTileXYZ::row
int row() const
Returns tile's row index (Y)
Definition: qgstiles.h:49
QgsTileMatrixSet::setScaleToTileZoomMethod
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition: qgstiles.h:337
QgsTileMatrix::matrixWidth
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition: qgstiles.h:157
qgscoordinatereferencesystem.h
QgsTileMatrix::scale
double scale() const
Returns scale denominator of the tile matrix.
Definition: qgstiles.h:170
QgsTileMatrixSet::scaleToTileZoomMethod
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition: qgstiles.h:330
QgsTileRange::startColumn
int startColumn() const
Returns index of the first column in the range.
Definition: qgstiles.h:81
QgsTileXYZ::column
int column() const
Returns tile's column index (X)
Definition: qgstiles.h:47