QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgstiles.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstiles.cpp
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 #include "qgstiles.h"
17 
18 #include "qgslogger.h"
20 #include "qgssettings.h"
21 
23 {
24  double z0xMin = -20037508.3427892, z0yMax = 20037508.3427892;
25 
26  return fromCustomDef( zoomLevel, QgsCoordinateReferenceSystem( "EPSG:3857" ), QgsPointXY( z0xMin, z0yMax ), 2 * z0yMax );
27 }
28 
30  const QgsPointXY &z0TopLeftPoint, double z0Dimension, int z0MatrixWidth, int z0MatrixHeight )
31 {
32  // Square extent calculation
33  double z0xMin = z0TopLeftPoint.x();
34  double z0yMax = z0TopLeftPoint.y();
35  double z0xMax = z0xMin + z0MatrixWidth * z0Dimension;
36  double z0yMin = z0yMax - z0MatrixHeight * z0Dimension;
37 
38  // Constant for scale denominator calculation
39  const double tileSize = 256.0;
40  const double PIXELS_TO_M = 2.8 / 10000.0; // WMS/WMTS define "standardized rendering pixel size" as 0.28mm
42  // Scale denominator calculation
43  double scaleDenom0 = ( z0Dimension / tileSize ) * ( UNIT_TO_M / PIXELS_TO_M );
44 
45  int numTiles = static_cast<int>( pow( 2, zoomLevel ) ); // assuming we won't ever go over 30 zoom levels
46 
47  QgsTileMatrix tm;
48  tm.mCrs = crs;
49  tm.mZoomLevel = zoomLevel;
50  tm.mMatrixWidth = z0MatrixWidth * numTiles;
51  tm.mMatrixHeight = z0MatrixHeight * numTiles;
52  tm.mTileXSpan = ( z0xMax - z0xMin ) / tm.mMatrixWidth;
53  tm.mTileYSpan = ( z0yMax - z0yMin ) / tm.mMatrixHeight;
54  tm.mExtent = QgsRectangle( z0xMin, z0yMin, z0xMax, z0yMax );
55  tm.mScaleDenom = scaleDenom0 / pow( 2, zoomLevel );
56  return tm;
57 }
58 
59 
60 QgsTileMatrix QgsTileMatrix::fromTileMatrix( const int &zoomLevel, const QgsTileMatrix &tileMatrix )
61 {
62  QgsTileMatrix tm;
63  int numTiles = static_cast<int>( pow( 2, zoomLevel ) ); // assuming we won't ever go over 30 zoom levels
64  int aZoomLevel = tileMatrix.zoomLevel();
65  int aNumTiles = static_cast<int>( pow( 2, aZoomLevel ) );
66  int aMatrixWidth = tileMatrix.matrixWidth();
67  int aMatrixHeight = tileMatrix.matrixHeight();
68  QgsRectangle aExtent = tileMatrix.extent();
69  tm.mCrs = tileMatrix.crs();
70  tm.mZoomLevel = zoomLevel;
71  tm.mMatrixWidth = aMatrixWidth * numTiles / aNumTiles;
72  tm.mMatrixHeight = aMatrixHeight * numTiles / aNumTiles;
73  tm.mTileXSpan = aExtent.width() / tm.mMatrixWidth;
74  tm.mTileYSpan = aExtent.height() / tm.mMatrixHeight;
75  tm.mExtent = aExtent;
76  tm.mScaleDenom = tileMatrix.scale() * pow( 2, aZoomLevel ) / pow( 2, zoomLevel );
77  return tm;
78 }
79 
81 {
82  double xMin = mExtent.xMinimum() + mTileXSpan * id.column();
83  double xMax = xMin + mTileXSpan;
84  double yMax = mExtent.yMaximum() - mTileYSpan * id.row();
85  double yMin = yMax - mTileYSpan;
86  return QgsRectangle( xMin, yMin, xMax, yMax );
87 }
88 
90 {
91  double x = mExtent.xMinimum() + mTileXSpan * id.column() + mTileXSpan / 2;
92  double y = mExtent.yMaximum() - mTileYSpan * id.row() - mTileYSpan / 2;
93  return QgsPointXY( x, y );
94 }
95 
97 {
98  double x0 = std::clamp( r.xMinimum(), mExtent.xMinimum(), mExtent.xMaximum() );
99  double y0 = std::clamp( r.yMinimum(), mExtent.yMinimum(), mExtent.yMaximum() );
100  double x1 = std::clamp( r.xMaximum(), mExtent.xMinimum(), mExtent.xMaximum() );
101  double y1 = std::clamp( r.yMaximum(), mExtent.yMinimum(), mExtent.yMaximum() );
102  if ( x0 >= x1 || y0 >= y1 )
103  return QgsTileRange(); // nothing to display
104 
105  double tileX1 = ( x0 - mExtent.xMinimum() ) / mTileXSpan;
106  double tileX2 = ( x1 - mExtent.xMinimum() ) / mTileXSpan;
107  double tileY1 = ( mExtent.yMaximum() - y1 ) / mTileYSpan;
108  double tileY2 = ( mExtent.yMaximum() - y0 ) / mTileYSpan;
109 
110  QgsDebugMsgLevel( QStringLiteral( "Tile range of edges [%1,%2] - [%3,%4]" ).arg( tileX1 ).arg( tileY1 ).arg( tileX2 ).arg( tileY2 ), 2 );
111 
112  // figure out tile range from zoom
113  int startColumn = std::clamp( static_cast<int>( floor( tileX1 ) ), 0, mMatrixWidth - 1 );
114  int endColumn = std::clamp( static_cast<int>( floor( tileX2 ) ), 0, mMatrixWidth - 1 );
115  int startRow = std::clamp( static_cast<int>( floor( tileY1 ) ), 0, mMatrixHeight - 1 );
116  int endRow = std::clamp( static_cast<int>( floor( tileY2 ) ), 0, mMatrixHeight - 1 );
117  return QgsTileRange( startColumn, endColumn, startRow, endRow );
118 }
119 
120 QPointF QgsTileMatrix::mapToTileCoordinates( const QgsPointXY &mapPoint ) const
121 {
122  double dx = mapPoint.x() - mExtent.xMinimum();
123  double dy = mExtent.yMaximum() - mapPoint.y();
124  return QPointF( dx / mTileXSpan, dy / mTileYSpan );
125 }
This class represents a coordinate reference system (CRS).
Q_GADGET QgsUnitTypes::DistanceUnit mapUnits
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:193
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:183
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition: qgstiles.h:104
QgsRectangle tileExtent(QgsTileXYZ id) const
Returns extent of the given tile in this matrix.
Definition: qgstiles.cpp:80
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
Definition: qgstiles.cpp:120
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent)
Returns tile range that fully covers the given extent.
Definition: qgstiles.cpp:96
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
Definition: qgstiles.cpp:22
QgsRectangle extent() const
Returns extent of the tile matrix.
Definition: qgstiles.h:131
int matrixWidth() const
Returns number of columns of the tile matrix.
Definition: qgstiles.h:125
QgsCoordinateReferenceSystem crs() const
Returns the authority identifier for the CRS of the tile matrix.
Definition: qgstiles.h:119
double scale() const
Returns scale denominator of the tile matrix.
Definition: qgstiles.h:134
QgsPointXY tileCenter(QgsTileXYZ id) const
Returns center of the given tile in this matrix.
Definition: qgstiles.cpp:89
int matrixHeight() const
Returns number of rows of the tile matrix.
Definition: qgstiles.h:128
static QgsTileMatrix fromTileMatrix(const int &zoomLevel, const QgsTileMatrix &tileMatrix)
Returns a tile matrix based on another one.
Definition: qgstiles.cpp:60
int zoomLevel() const
Returns zoom level of the tile matrix.
Definition: qgstiles.h:122
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:29
Range of tiles in a tile matrix to be rendered.
Definition: qgstiles.h:67
Stores coordinates of a tile in a tile matrix set.
Definition: qgstiles.h:34
@ DistanceMeters
Meters.
Definition: qgsunittypes.h:69
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
const QgsCoordinateReferenceSystem & crs