QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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 )
48 , mRow( tr )
49 , mZoomLevel( tz )
50 {}
51
53 int column() const { return mColumn; }
55 int row() const { return mRow; }
57 int zoomLevel() const { return mZoomLevel; }
58
60 QString toString() const { return u"X=%1 Y=%2 Z=%3"_s.arg( mColumn ).arg( mRow ).arg( mZoomLevel ); }
61
62 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
63 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
64
65#ifdef SIP_RUN
66 // clang-format off
67 SIP_PYOBJECT __repr__();
68 % MethodCode
69 const QString str = u"<QgsTileXYZ: %1, %2, %3>"_s.arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
70 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
71 % End
72// clang-format on
73#endif
74
75 // clang-format off
76 private:
77 // clang-format on
78 int mColumn
79 = -1;
80 int mRow = -1;
81 int mZoomLevel = -1;
82};
83
84#ifndef SIP_RUN
85#if ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 6 ) ) || defined( __clang__ )
86#pragma GCC diagnostic push
87#pragma GCC diagnostic ignored "-Wattributes"
88#elif defined( _MSC_VER )
89__pragma( warning( push ) ) __pragma( warning( disable : 4273 ) )
90#endif
91#endif
92
98CORE_EXPORT inline size_t qHash( QgsTileXYZ id ) SIP_SKIP
99{
100 const size_t h1 = qHash( static_cast< quint64 >( id.column() ) );
101 const size_t h2 = qHash( static_cast< quint64 >( id.row() ) );
102 const size_t h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
103 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 );
104}
105
106#ifndef SIP_RUN
107#if ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 6 ) ) || defined( __clang__ )
108#pragma GCC diagnostic pop
109#elif defined( _MSC_VER )
110__pragma( warning( pop ) )
111#endif
112#endif
113
122class CORE_EXPORT QgsTileRange
123{
124 public:
126 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
127 : mStartColumn( c1 )
128 , mEndColumn( c2 )
129 , mStartRow( r1 )
130 , mEndRow( r2 )
131 {}
132
134 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
135
137 int startColumn() const { return mStartColumn; }
139 int endColumn() const { return mEndColumn; }
141 int startRow() const { return mStartRow; }
143 int endRow() const { return mEndRow; }
144
150 int count() const { return isValid() ? ( mEndRow - mStartRow + 1 ) * ( mEndColumn - mStartColumn + 1 ) : 0; }
151
152 private:
153 int mStartColumn = -1;
154 int mEndColumn = -1;
155 int mStartRow = -1;
156 int mEndRow = -1;
157};
158
159
170class CORE_EXPORT QgsTileMatrix
171{
172 public:
175
181 static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs, const QgsPointXY &z0TopLeftPoint, double z0Dimension, int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
182
184 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
185
191 QgsCoordinateReferenceSystem crs() const { return mCrs; }
192
199 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs; }
200
206 int zoomLevel() const { return mZoomLevel; }
207
214 void setZoomLevel( int level ) { mZoomLevel = level; }
215
217 int matrixWidth() const { return mMatrixWidth; }
218
220 int matrixHeight() const { return mMatrixHeight; }
221
230 long long derivedMatrixWidthAtZoomLevel( int zoomLevel ) const;
231
240 long long derivedMatrixHeightAtZoomLevel( int zoomLevel ) const;
241
243 QgsRectangle extent() const { return mExtent; }
244
250 double scale() const { return mScaleDenom; }
251
258 void setScale( double scale ) { mScaleDenom = scale; }
259
261 QgsRectangle tileExtent( QgsTileXYZ id ) const;
262
264 QgsPointXY tileCenter( QgsTileXYZ id ) const;
265
267 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
268
270 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
271
273 bool isRootTileMatrix() const { return mZoomLevel == 0; }
274
275 private:
279 int mZoomLevel = -1;
281 int mMatrixWidth = 0;
283 int mMatrixHeight = 0;
285 QgsRectangle mExtent;
287 double mScaleDenom = 0;
289 double mTileXSpan = 0;
291 double mTileYSpan = 0;
292
293 friend class QgsTileMatrixSet;
294};
295
296
303class CORE_EXPORT QgsTileMatrixSet
304{
305 public:
307
308 virtual ~QgsTileMatrixSet() = default;
309
313 bool isEmpty() const;
314
318 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
319
323 QgsTileMatrix tileMatrix( int zoom ) const;
324
331
337 void setRootMatrix( const QgsTileMatrix &matrix );
338
344 void addMatrix( const QgsTileMatrix &matrix );
345
351 int minimumZoom() const;
352
358 int maximumZoom() const;
359
365
378
386
392 double scaleToZoom( double scale ) const;
393
401 int scaleToZoomLevel( double scale, bool clamp = true ) const;
402
408 double scaleForRenderContext( const QgsRenderContext &context ) const;
409
415 double calculateTileScaleForMap( double actualMapScale, const QgsCoordinateReferenceSystem &mapCrs, const QgsRectangle &mapExtent, const QSize mapSize, const double mapDpi ) const;
416
422 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
423
427 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
428
435
442
448 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
449
450 protected:
453
454 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
456 QMap< int, QgsTileMatrix > mTileMatrices;
458};
459
460#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:3658
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
Definition qgis.h:3659
TileAvailability
Possible availability states for a tile within a tile matrix.
Definition qgis.h:6427
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:401
Qgis::ScaleToTileZoomLevelMethod mScaleToTileZoomMethod
Definition qgstiles.h:457
void addGoogleCrs84QuadTiles(int minimumZoom=0, int maximumZoom=14)
Adds tile matrices corresponding to the standard web mercator/GoogleCRS84Quad setup.
Definition qgstiles.cpp:161
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system associated with the tiles.
Definition qgstiles.cpp:236
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:451
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:317
int minimumZoom() const
Returns the minimum zoom level for tiles present in the set.
Definition qgstiles.cpp:194
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
Definition qgstiles.cpp:244
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:456
int maximumZoom() const
Returns the maximum zoom level for tiles present in the set.
Definition qgstiles.cpp:205
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Definition qgstiles.cpp:174
Qgis::TileAvailability tileAvailability(QgsTileXYZ id) const
Returns the availability of the given tile in this matrix.
Definition qgstiles.cpp:231
QgsTileMatrix rootMatrix() const
Returns the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:179
virtual bool readXml(const QDomElement &element, QgsReadWriteContext &context)
Reads the set from an XML element.
Definition qgstiles.cpp:356
void addMatrix(const QgsTileMatrix &matrix)
Adds a matrix to the set.
Definition qgstiles.cpp:189
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:322
void dropMatricesOutsideZoomRange(int minimumZoom, int maximumZoom)
Deletes any existing matrices which fall outside the zoom range specified by minimumZoom to maximumZo...
Definition qgstiles.cpp:216
virtual ~QgsTileMatrixSet()=default
bool isEmpty() const
Returns true if the matrix set is empty.
Definition qgstiles.cpp:156
QgsTileMatrix mRootMatrix
Definition qgstiles.h:455
void setRootMatrix(const QgsTileMatrix &matrix)
Sets the root tile matrix (usually corresponding to zoom level 0).
Definition qgstiles.cpp:184
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:434
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:441
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:452
int scaleToZoomLevel(double scale, bool clamp=true) const
Finds the best fitting (integer) zoom level given a map scale denominator.
Definition qgstiles.cpp:301
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:171
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:199
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:258
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:243
friend class QgsTileMatrixSet
Definition qgstiles.h:293
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:217
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:191
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0).
Definition qgstiles.h:273
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:250
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:214
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:220
static QgsTileMatrix fromTileMatrix(int zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
Definition qgstiles.cpp:64
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:206
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:123
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:139
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:126
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:143
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:141
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:137
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative).
Definition qgstiles.h:134
int count() const
Returns the total number of tiles in the range.
Definition qgstiles.h:150
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:60
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:57
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:62
int column() const
Returns tile's column index (X).
Definition qgstiles.h:53
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:63
int row() const
Returns tile's row index (Y).
Definition qgstiles.h:55
#define SIP_SKIP
Definition qgis_sip.h:138
CORE_EXPORT size_t qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:98