QGIS API Documentation 3.99.0-Master (d270888f95f)
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
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31
42class CORE_EXPORT QgsTileXYZ
43{
44 public:
46 QgsTileXYZ( int tc = -1, int tr = -1, int tz = -1 )
47 : mColumn( tc ), mRow( tr ), mZoomLevel( tz )
48 {
49 }
50
52 int column() const { return mColumn; }
54 int row() const { return mRow; }
56 int zoomLevel() const { return mZoomLevel; }
57
59 QString toString() const { return u"X=%1 Y=%2 Z=%3"_s.arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
60
61 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
62 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
63
64#ifdef SIP_RUN
65 SIP_PYOBJECT __repr__();
66 % MethodCode
67 const QString str = u"<QgsTileXYZ: %1, %2, %3>"_s.arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
68 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
69 % End
70#endif
71
72 private:
73 int mColumn = -1;
74 int mRow = -1;
75 int mZoomLevel = -1;
76};
77
78#ifndef SIP_RUN
79#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
80#pragma GCC diagnostic push
81#pragma GCC diagnostic ignored "-Wattributes"
82#elif defined(_MSC_VER)
83__pragma( warning( push ) )
84__pragma( warning( disable: 4273 ) )
85#endif
86#endif
87
93CORE_EXPORT inline uint qHash( QgsTileXYZ id ) SIP_SKIP
94{
95 const uint h1 = qHash( static_cast< quint64 >( id.column( ) ) );
96 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
97 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
98 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
99}
100
101#ifndef SIP_RUN
102#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || defined(__clang__)
103#pragma GCC diagnostic pop
104#elif defined(_MSC_VER)
105__pragma( warning( pop ) )
106#endif
107#endif
108
117class CORE_EXPORT QgsTileRange
118{
119 public:
121 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
122 : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
123
125 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
126
128 int startColumn() const { return mStartColumn; }
130 int endColumn() const { return mEndColumn; }
132 int startRow() const { return mStartRow; }
134 int endRow() const { return mEndRow; }
135
141 int count() const { return isValid() ? ( mEndRow - mStartRow + 1 ) * ( mEndColumn - mStartColumn + 1 ) : 0; }
142
143 private:
144 int mStartColumn = -1;
145 int mEndColumn = -1;
146 int mStartRow = -1;
147 int mEndRow = -1;
148};
149
150
161class CORE_EXPORT QgsTileMatrix
162{
163 public:
164
167
174 const QgsPointXY &z0TopLeftPoint, double z0Dimension,
175 int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
176
178 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
179
185 QgsCoordinateReferenceSystem crs() const { return mCrs; }
186
193 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
194
200 int zoomLevel() const { return mZoomLevel; }
201
208 void setZoomLevel( int level ) { mZoomLevel = level; }
209
211 int matrixWidth() const { return mMatrixWidth; }
212
214 int matrixHeight() const { return mMatrixHeight; }
215
217 QgsRectangle extent() const { return mExtent; }
218
224 double scale() const { return mScaleDenom; }
225
232 void setScale( double scale ) { mScaleDenom = scale; }
233
235 QgsRectangle tileExtent( QgsTileXYZ id ) const;
236
238 QgsPointXY tileCenter( QgsTileXYZ id ) const;
239
241 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
242
244 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
245
247 bool isRootTileMatrix() const { return mZoomLevel == 0; }
248
249 private:
253 int mZoomLevel = -1;
255 int mMatrixWidth = 0;
257 int mMatrixHeight = 0;
259 QgsRectangle mExtent;
261 double mScaleDenom = 0;
263 double mTileXSpan = 0;
265 double mTileYSpan = 0;
266
267 friend class QgsTileMatrixSet;
268};
269
270
277class CORE_EXPORT QgsTileMatrixSet
278{
279
280 public:
281
283
284 virtual ~QgsTileMatrixSet() = default;
285
289 bool isEmpty() const;
290
294 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
295
299 QgsTileMatrix tileMatrix( int zoom ) const;
300
307
313 void setRootMatrix( const QgsTileMatrix &matrix );
314
320 void addMatrix( const QgsTileMatrix &matrix );
321
327 int minimumZoom() const;
328
334 int maximumZoom() const;
335
341
354
362
368 double scaleToZoom( double scale ) const;
369
377 int scaleToZoomLevel( double scale, bool clamp = true ) const;
378
384 double scaleForRenderContext( const QgsRenderContext &context ) const;
385
391 double calculateTileScaleForMap( double actualMapScale,
392 const QgsCoordinateReferenceSystem &mapCrs,
393 const QgsRectangle &mapExtent,
394 const QSize mapSize,
395 const double mapDpi
396 ) const;
397
403 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
404
408 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
409
416
423
429 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
430
431 protected:
434
435 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
437 QMap< int, QgsTileMatrix > mTileMatrices;
439};
440
441#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3479
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
Definition qgis.h:3480
TileAvailability
Possible availability states for a tile within a tile matrix.
Definition qgis.h:5873
Represents a coordinate reference system (CRS).
Represents a 2D point.
Definition qgspointxy.h:62
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:395
Qgis::ScaleToTileZoomLevelMethod mScaleToTileZoomMethod
Definition qgstiles.h:438
void addGoogleCrs84QuadTiles(int minimumZoom=0, int maximumZoom=14)
Adds tile matrices corresponding to the standard web mercator/GoogleCRS84Quad setup.
Definition qgstiles.cpp:147
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system associated with the tiles.
Definition qgstiles.cpp:222
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:432
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:303
int minimumZoom() const
Returns the minimum zoom level for tiles present in the set.
Definition qgstiles.cpp:180
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
Definition qgstiles.cpp:230
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:437
int maximumZoom() const
Returns the maximum zoom level for tiles present in the set.
Definition qgstiles.cpp:191
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Definition qgstiles.cpp:160
Qgis::TileAvailability tileAvailability(QgsTileXYZ id) const
Returns the availability of the given tile in this matrix.
Definition qgstiles.cpp:217
QgsTileMatrix rootMatrix() const
Returns the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:165
virtual bool readXml(const QDomElement &element, QgsReadWriteContext &context)
Reads the set from an XML element.
Definition qgstiles.cpp:346
void addMatrix(const QgsTileMatrix &matrix)
Adds a matrix to the set.
Definition qgstiles.cpp:175
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:312
void dropMatricesOutsideZoomRange(int minimumZoom, int maximumZoom)
Deletes any existing matrices which fall outside the zoom range specified by minimumZoom to maximumZo...
Definition qgstiles.cpp:202
virtual ~QgsTileMatrixSet()=default
bool isEmpty() const
Returns true if the matrix set is empty.
Definition qgstiles.cpp:142
QgsTileMatrix mRootMatrix
Definition qgstiles.h:436
void setRootMatrix(const QgsTileMatrix &matrix)
Sets the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:170
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:415
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:422
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:433
int scaleToZoomLevel(double scale, bool clamp=true) const
Finds the best fitting (integer) zoom level given a map scale denominator.
Definition qgstiles.cpp:287
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:162
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:193
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
Definition qgstiles.cpp:27
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:232
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:217
friend class QgsTileMatrixSet
Definition qgstiles.h:267
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:211
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:185
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0).
Definition qgstiles.h:247
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:224
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:208
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:214
static QgsTileMatrix fromTileMatrix(int zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
Definition qgstiles.cpp:65
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:200
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:35
A range of tiles in a tile matrix.
Definition qgstiles.h:118
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:130
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:121
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:134
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:132
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:128
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative).
Definition qgstiles.h:125
int count() const
Returns the total number of tiles in the range.
Definition qgstiles.h:141
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:43
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:59
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:56
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:46
bool operator==(const QgsTileXYZ &other) const
Definition qgstiles.h:61
int column() const
Returns tile's column index (X).
Definition qgstiles.h:52
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:62
int row() const
Returns tile's row index (Y).
Definition qgstiles.h:54
#define SIP_SKIP
Definition qgis_sip.h:134
CORE_EXPORT uint qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:93