QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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_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 return id.column() + id.row() + id.zoomLevel();
83
84 const uint h1 = qHash( static_cast< quint64 >( id.column( ) ) );
85 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
86 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
87 return h1 ^ ( h2 << 1 ) ^ ( h3 );
88}
89
90
98class CORE_EXPORT QgsTileRange
99{
100 public:
102 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
103 : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
104
106 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
107
109 int startColumn() const { return mStartColumn; }
111 int endColumn() const { return mEndColumn; }
113 int startRow() const { return mStartRow; }
115 int endRow() const { return mEndRow; }
116
117 private:
118 int mStartColumn = -1;
119 int mEndColumn = -1;
120 int mStartRow = -1;
121 int mEndRow = -1;
122};
123
124
135class CORE_EXPORT QgsTileMatrix
136{
137 public:
138
140 static QgsTileMatrix fromWebMercator( int zoomLevel );
141
147 static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs,
148 const QgsPointXY &z0TopLeftPoint, double z0Dimension,
149 int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
150
152 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
153
159 QgsCoordinateReferenceSystem crs() const { return mCrs; }
160
167 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
168
174 int zoomLevel() const { return mZoomLevel; }
175
182 void setZoomLevel( int level ) { mZoomLevel = level; }
183
185 int matrixWidth() const { return mMatrixWidth; }
186
188 int matrixHeight() const { return mMatrixHeight; }
189
191 QgsRectangle extent() const { return mExtent; }
192
198 double scale() const { return mScaleDenom; }
199
206 void setScale( double scale ) { mScaleDenom = scale; }
207
209 QgsRectangle tileExtent( QgsTileXYZ id ) const;
210
212 QgsPointXY tileCenter( QgsTileXYZ id ) const;
213
215 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
216
218 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
219
221 bool isRootTileMatrix() const { return mZoomLevel == 0; }
222
223 private:
227 int mZoomLevel = -1;
229 int mMatrixWidth = 0;
231 int mMatrixHeight = 0;
233 QgsRectangle mExtent;
235 double mScaleDenom = 0;
237 double mTileXSpan = 0;
239 double mTileYSpan = 0;
240
241 friend class QgsTileMatrixSet;
242};
243
244
251class CORE_EXPORT QgsTileMatrixSet
252{
253
254 public:
255
257
258 virtual ~QgsTileMatrixSet() = default;
259
263 bool isEmpty() const;
264
268 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
269
273 QgsTileMatrix tileMatrix( int zoom ) const;
274
280 QgsTileMatrix rootMatrix() const;
281
287 void setRootMatrix( const QgsTileMatrix &matrix );
288
294 void addMatrix( const QgsTileMatrix &matrix );
295
301 int minimumZoom() const;
302
308 int maximumZoom() const;
309
314 void dropMatricesOutsideZoomRange( int minimumZoom, int maximumZoom );
315
327 Qgis::TileAvailability tileAvailability( QgsTileXYZ id ) const;
328
336
342 double scaleToZoom( double scale ) const;
343
351 int scaleToZoomLevel( double scale, bool clamp = true ) const;
352
358 double scaleForRenderContext( const QgsRenderContext &context ) const;
359
365 double calculateTileScaleForMap( double actualMapScale,
366 const QgsCoordinateReferenceSystem &mapCrs,
367 const QgsRectangle &mapExtent,
368 const QSize mapSize,
369 const double mapDpi
370 ) const;
371
377 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
378
382 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
383
389 Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const { return mScaleToTileZoomMethod; }
390
396 void setScaleToTileZoomMethod( Qgis::ScaleToTileZoomLevelMethod method ) { mScaleToTileZoomMethod = method; }
397
403 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
404
405 protected:
408
409 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
411 QMap< int, QgsTileMatrix > mTileMatrices;
413};
414
415#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3171
@ 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:5205
This class represents a coordinate reference system (CRS).
A class to represent a 2D point.
Definition qgspointxy.h:60
The class is used as a container of context for various read/write operations on other 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:252
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:406
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:411
virtual ~QgsTileMatrixSet()=default
QgsTileMatrix mRootMatrix
Definition qgstiles.h:410
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:389
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:396
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:407
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:136
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:167
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:206
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:191
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:185
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:159
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0)
Definition qgstiles.h:221
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:198
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:182
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:188
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:174
Range of tiles in a tile matrix to be rendered.
Definition qgstiles.h:99
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:111
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:102
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:115
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:113
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:109
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
Definition qgstiles.h:106
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 str(x)
Definition qgis.cpp:39
#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