QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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
37class 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 bool operator==( const QgsTileXYZ &other ) const { return mColumn == other.mColumn && mRow == other.mRow && mZoomLevel == other.mZoomLevel; }
57 bool operator!=( const QgsTileXYZ &other ) const { return !( *this == other ); }
58
59#ifdef SIP_RUN
60 SIP_PYOBJECT __repr__();
61 % MethodCode
62 const QString str = QStringLiteral( "<QgsTileXYZ: %1, %2, %3>" ).arg( sipCpp->column() ).arg( sipCpp->row() ).arg( sipCpp->zoomLevel() );
63 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
64 % End
65#endif
66
67 private:
68 int mColumn = -1;
69 int mRow = -1;
70 int mZoomLevel = -1;
71};
72
78CORE_EXPORT inline uint qHash( QgsTileXYZ id ) SIP_SKIP
79{
80 return id.column() + id.row() + id.zoomLevel();
81
82 const uint h1 = qHash( static_cast< quint64 >( id.column( ) ) );
83 const uint h2 = qHash( static_cast< quint64 >( id.row() ) );
84 const uint h3 = qHash( static_cast< quint64 >( id.zoomLevel() ) );
85 return h1 ^ ( h2 << 1 ) ^ ( h3 );
86}
87
88
96class CORE_EXPORT QgsTileRange
97{
98 public:
100 QgsTileRange( int c1 = -1, int c2 = -1, int r1 = -1, int r2 = -1 )
101 : mStartColumn( c1 ), mEndColumn( c2 ), mStartRow( r1 ), mEndRow( r2 ) {}
102
104 bool isValid() const { return mStartColumn >= 0 && mEndColumn >= 0 && mStartRow >= 0 && mEndRow >= 0; }
105
107 int startColumn() const { return mStartColumn; }
109 int endColumn() const { return mEndColumn; }
111 int startRow() const { return mStartRow; }
113 int endRow() const { return mEndRow; }
114
115 private:
116 int mStartColumn = -1;
117 int mEndColumn = -1;
118 int mStartRow = -1;
119 int mEndRow = -1;
120};
121
122
133class CORE_EXPORT QgsTileMatrix
134{
135 public:
136
138 static QgsTileMatrix fromWebMercator( int zoomLevel );
139
145 static QgsTileMatrix fromCustomDef( int zoomLevel, const QgsCoordinateReferenceSystem &crs,
146 const QgsPointXY &z0TopLeftPoint, double z0Dimension,
147 int z0MatrixWidth = 1, int z0MatrixHeight = 1 );
148
150 static QgsTileMatrix fromTileMatrix( int zoomLevel, const QgsTileMatrix &tileMatrix );
151
157 QgsCoordinateReferenceSystem crs() const { return mCrs; }
158
165 void setCrs( const QgsCoordinateReferenceSystem &crs ) { mCrs = crs;}
166
172 int zoomLevel() const { return mZoomLevel; }
173
180 void setZoomLevel( int level ) { mZoomLevel = level; }
181
183 int matrixWidth() const { return mMatrixWidth; }
184
186 int matrixHeight() const { return mMatrixHeight; }
187
189 QgsRectangle extent() const { return mExtent; }
190
196 double scale() const { return mScaleDenom; }
197
204 void setScale( double scale ) { mScaleDenom = scale; }
205
207 QgsRectangle tileExtent( QgsTileXYZ id ) const;
208
210 QgsPointXY tileCenter( QgsTileXYZ id ) const;
211
213 QgsTileRange tileRangeFromExtent( const QgsRectangle &mExtent ) const;
214
216 QPointF mapToTileCoordinates( const QgsPointXY &mapPoint ) const;
217
219 bool isRootTileMatrix() const { return mZoomLevel == 0; }
220
221 private:
225 int mZoomLevel = -1;
227 int mMatrixWidth = 0;
229 int mMatrixHeight = 0;
231 QgsRectangle mExtent;
233 double mScaleDenom = 0;
235 double mTileXSpan = 0;
237 double mTileYSpan = 0;
238
239 friend class QgsTileMatrixSet;
240};
241
242
249class CORE_EXPORT QgsTileMatrixSet
250{
251
252 public:
253
255
256 virtual ~QgsTileMatrixSet() = default;
257
261 bool isEmpty() const;
262
266 void addGoogleCrs84QuadTiles( int minimumZoom = 0, int maximumZoom = 14 );
267
271 QgsTileMatrix tileMatrix( int zoom ) const;
272
278 QgsTileMatrix rootMatrix() const;
279
285 void setRootMatrix( const QgsTileMatrix &matrix );
286
292 void addMatrix( const QgsTileMatrix &matrix );
293
299 int minimumZoom() const;
300
306 int maximumZoom() const;
307
312 void dropMatricesOutsideZoomRange( int minimumZoom, int maximumZoom );
313
325 Qgis::TileAvailability tileAvailability( QgsTileXYZ id ) const;
326
334
340 double scaleToZoom( double scale ) const;
341
349 int scaleToZoomLevel( double scale, bool clamp = true ) const;
350
356 double scaleForRenderContext( const QgsRenderContext &context ) const;
357
363 double calculateTileScaleForMap( double actualMapScale,
364 const QgsCoordinateReferenceSystem &mapCrs,
365 const QgsRectangle &mapExtent,
366 const QSize mapSize,
367 const double mapDpi
368 ) const;
369
375 virtual bool readXml( const QDomElement &element, QgsReadWriteContext &context );
376
380 virtual QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
381
387 Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const { return mScaleToTileZoomMethod; }
388
394 void setScaleToTileZoomMethod( Qgis::ScaleToTileZoomLevelMethod method ) { mScaleToTileZoomMethod = method; }
395
401 QVector<QgsTileXYZ> tilesInRange( QgsTileRange range, int zoomLevel ) const;
402
403 protected:
406
407 // Usually corresponds to zoom level 0, even if that zoom level is NOT present in the actual tile matrices for this set
409 QMap< int, QgsTileMatrix > mTileMatrices;
411};
412
413#endif // QGSTILES_H
ScaleToTileZoomLevelMethod
Available methods for converting map scales to tile zoom levels.
Definition qgis.h:2524
@ MapBox
Uses a scale doubling approach to account for hi-DPI tiles, and rounds to the nearest tile level for ...
TileAvailability
Vector tile data provider capabilities.
Definition qgis.h:3899
This class represents a coordinate reference system (CRS).
A class to represent a 2D point.
Definition qgspointxy.h:59
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:250
std::function< Qgis::TileAvailability(QgsTileXYZ id) > mTileAvailabilityFunction
Definition qgstiles.h:404
QMap< int, QgsTileMatrix > mTileMatrices
Definition qgstiles.h:409
virtual ~QgsTileMatrixSet()=default
QgsTileMatrix mRootMatrix
Definition qgstiles.h:408
Qgis::ScaleToTileZoomLevelMethod scaleToTileZoomMethod() const
Returns the scale to tile zoom method.
Definition qgstiles.h:387
void setScaleToTileZoomMethod(Qgis::ScaleToTileZoomLevelMethod method)
Sets the scale to tile zoom method.
Definition qgstiles.h:394
std::function< Qgis::TileAvailability(QgsTileXYZ id, QgsTileXYZ &replacement) > mTileReplacementFunction
Definition qgstiles.h:405
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition qgstiles.h:134
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs of the tile matrix.
Definition qgstiles.h:165
void setScale(double scale)
Sets the scale denominator of the tile matrix.
Definition qgstiles.h:204
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition qgstiles.h:189
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition qgstiles.h:183
QgsCoordinateReferenceSystem crs() const
Returns the crs of the tile matrix.
Definition qgstiles.h:157
bool isRootTileMatrix() const
Returns the root status of the tile matrix (zoom level == 0)
Definition qgstiles.h:219
double scale() const
Returns scale denominator of the tile matrix.
Definition qgstiles.h:196
void setZoomLevel(int level)
Sets the zoom level of the tile matrix.
Definition qgstiles.h:180
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition qgstiles.h:186
int zoomLevel() const
Returns the zoom level of the tile matrix.
Definition qgstiles.h:172
Range of tiles in a tile matrix to be rendered.
Definition qgstiles.h:97
int endColumn() const
Returns index of the last column in the range.
Definition qgstiles.h:109
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:100
int endRow() const
Returns index of the last row in the range.
Definition qgstiles.h:113
int startRow() const
Returns index of the first row in the range.
Definition qgstiles.h:111
int startColumn() const
Returns index of the first column in the range.
Definition qgstiles.h:107
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
Definition qgstiles.h:104
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:38
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:54
int zoomLevel() const
Returns tile's zoom level (Z)
Definition qgstiles.h:51
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
bool operator==(const QgsTileXYZ &other) const
Definition qgstiles.h:56
int column() const
Returns tile's column index (X)
Definition qgstiles.h:47
bool operator!=(const QgsTileXYZ &other) const
Definition qgstiles.h:57
int row() const
Returns tile's row index (Y)
Definition qgstiles.h:49
#define str(x)
Definition qgis.cpp:38
#define SIP_SKIP
Definition qgis_sip.h:126
CORE_EXPORT uint qHash(QgsTileXYZ id)
Returns a hash for a tile id.
Definition qgstiles.h:78
const QgsCoordinateReferenceSystem & crs