QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
27
38class CORE_EXPORT QgsTileXYZ
39{
40 public:
42 QgsTileXYZ( int tc = -1, int tr = -1, int tz = -1 )
43 : mColumn( tc ), mRow( tr ), mZoomLevel( tz )
44 {
45 }
46
48 int column() const { return mColumn; }
50 int row() const { return mRow; }
52 int zoomLevel() const { return mZoomLevel; }
53
55 QString toString() const { return QStringLiteral( "X=%1 Y=%2 Z=%3" ).arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
56
57 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
58 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
59
60#ifdef SIP_RUN
61 SIP_PYOBJECT __repr__();
62 % MethodCode
63 const QString str = QStringLiteral( "<QgsTileXYZ: %1, %2, %3>" ).arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
64 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
65 % End
66#endif
67
68 private:
69 int mColumn = -1;
70 int mRow = -1;
71 int mZoomLevel = -1;
72};
73
74#ifndef SIP_RUN
75#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
76#pragma GCC diagnostic push
77#pragma GCC diagnostic ignored "-Wattributes"
78#elif defined(_MSC_VER)
79__pragma( warning( push ) )
80__pragma( warning( disable: 4273 ) )
81#endif
82#endif
83
89CORE_EXPORT inline uint qHash( QgsTileXYZ id ) SIP_SKIP
90{
91 const uint h1 = qHash( static_cast< quint64 >( id.column( ) ) );
92 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
93 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
94 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
95}
96
97#ifndef SIP_RUN
98#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
99#pragma GCC diagnostic pop
100#elif defined(_MSC_VER)
101__pragma( warning( pop ) )
102#endif
103#endif
104
113class CORE_EXPORT QgsTileRange
114{
115 public:
117 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
118 : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
119
121 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
122
124 int startColumn() const { return mStartColumn; }
126 int endColumn() const { return mEndColumn; }
128 int startRow() const { return mStartRow; }
130 int endRow() const { return mEndRow; }
131
137 int count() const { return isValid() ? ( mEndRow - mStartRow + 1 ) * ( mEndColumn - mStartColumn + 1 ) : 0; }
138
139 private:
140 int mStartColumn = -1;
141 int mEndColumn = -1;
142 int mStartRow = -1;
143 int mEndRow = -1;
144};
145
146
157class CORE_EXPORT QgsTileMatrix
158{
159 public:
160
163
170 const QgsPointXY &z0TopLeftPoint, double z0Dimension,
171 int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
172
174 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
175
181 QgsCoordinateReferenceSystem crs() const { return mCrs; }
182
189 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
190
196 int zoomLevel() const { return mZoomLevel; }
197
204 void setZoomLevel( int level ) { mZoomLevel = level; }
205
207 int matrixWidth() const { return mMatrixWidth; }
208
210 int matrixHeight() const { return mMatrixHeight; }
211
213 QgsRectangle extent() const { return mExtent; }
214
220 double scale() const { return mScaleDenom; }
221
228 void setScale( double scale ) { mScaleDenom = scale; }
229
231 QgsRectangle tileExtent( QgsTileXYZ id ) const;
232
234 QgsPointXY tileCenter( QgsTileXYZ id ) const;
235
237 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
238
240 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
241
243 bool isRootTileMatrix() const { return mZoomLevel == 0; }
244
245 private:
249 int mZoomLevel = -1;
251 int mMatrixWidth = 0;
253 int mMatrixHeight = 0;
255 QgsRectangle mExtent;
257 double mScaleDenom = 0;
259 double mTileXSpan = 0;
261 double mTileYSpan = 0;
262
263 friend class QgsTileMatrixSet;
264};
265
266
273class CORE_EXPORT QgsTileMatrixSet
274{
275
276 public:
277
279
280 virtual ~QgsTileMatrixSet() = default;
281
285 bool isEmpty() const;
286
290 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
291
295 QgsTileMatrix tileMatrix( int zoom ) const;
296
303
309 void setRootMatrix( const QgsTileMatrix &matrix );
310
316 void addMatrix( const QgsTileMatrix &matrix );
317
323 int minimumZoom() const;
324
330 int maximumZoom() const;
331
337
350
358
364 double scaleToZoom( double scale ) const;
365
373 int scaleToZoomLevel( double scale, bool clamp = true ) const;
374
380 double scaleForRenderContext( const QgsRenderContext &context ) const;
381
387 double calculateTileScaleForMap( double actualMapScale,
388 const QgsCoordinateReferenceSystem &mapCrs,
389 const QgsRectangle &mapExtent,
390 const QSize mapSize,
391 const double mapDpi
392 ) const;
393
399 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
400
404 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
405
412
419
425 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
426
427 protected:
430
431 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
433 QMap< int, QgsTileMatrix > mTileMatrices;
435};
436
437#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3420
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
Definition qgis.h:3421
TileAvailability
Possible availability states for a tile within a tile matrix.
Definition qgis.h:5607
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.
virtual QDomElement writeXml(QDomDocument &document, const QgsReadWriteContext &context) const
Writes the set to an XML element.
Definition qgstiles.cpp:391
Qgis::ScaleToTileZoomLevelMethod mScaleToTileZoomMethod
Definition qgstiles.h:434
void addGoogleCrs84QuadTiles(int minimumZoom=0, int maximumZoom=14)
Adds tile matrices corresponding to the standard web mercator/GoogleCRS84Quad setup.
Definition qgstiles.cpp:143
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system associated with the tiles.
Definition qgstiles.cpp:218
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:428
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:299
int minimumZoom() const
Returns the minimum zoom level for tiles present in the set.
Definition qgstiles.cpp:176
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
Definition qgstiles.cpp:226
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:433
int maximumZoom() const
Returns the maximum zoom level for tiles present in the set.
Definition qgstiles.cpp:187
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Definition qgstiles.cpp:156
Qgis::TileAvailability tileAvailability(QgsTileXYZ id) const
Returns the availability of the given tile in this matrix.
Definition qgstiles.cpp:213
QgsTileMatrix rootMatrix() const
Returns the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:161
virtual bool readXml(const QDomElement &element, QgsReadWriteContext &context)
Reads the set from an XML element.
Definition qgstiles.cpp:342
void addMatrix(const QgsTileMatrix &matrix)
Adds a matrix to the set.
Definition qgstiles.cpp:171
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:308
void dropMatricesOutsideZoomRange(int minimumZoom, int maximumZoom)
Deletes any existing matrices which fall outside the zoom range specified by minimumZoom to maximumZo...
Definition qgstiles.cpp:198
virtual ~QgsTileMatrixSet()=default
bool isEmpty() const
Returns true if the matrix set is empty.
Definition qgstiles.cpp:138
QgsTileMatrix mRootMatrix
Definition qgstiles.h:432
void setRootMatrix(const QgsTileMatrix &matrix)
Sets the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:166
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:411
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:418
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:429
int scaleToZoomLevel(double scale, bool clamp=true) const
Finds the best fitting (integer) zoom level given a map scale denominator.
Definition qgstiles.cpp:283
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:158
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:189
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
Definition qgstiles.cpp:23
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:228
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:213
friend class QgsTileMatrixSet
Definition qgstiles.h:263
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:207
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:181
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0).
Definition qgstiles.h:243
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:220
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:204
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:210
static QgsTileMatrix fromTileMatrix(int zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
Definition qgstiles.cpp:61
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:196
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:31
A range of tiles in a tile matrix.
Definition qgstiles.h:114
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:126
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:117
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:130
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:128
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:124
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative).
Definition qgstiles.h:121
int count() const
Returns the total number of tiles in the range.
Definition qgstiles.h:137
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:39
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:55
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:52
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:42
bool operator==(const QgsTileXYZ &other) const
Definition qgstiles.h:57
int column() const
Returns tile's column index (X).
Definition qgstiles.h:48
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:58
int row() const
Returns tile's row index (Y).
Definition qgstiles.h:50
#define SIP_SKIP
Definition qgis_sip.h:134
CORE_EXPORT uint qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:89