QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
19 
21 {
22  int numTiles = static_cast<int>( pow( 2, zoomLevel ) ); // assuming we won't ever go over 30 zoom levels
23  double z0xMin = -20037508.3427892, z0yMin = -20037508.3427892;
24  double z0xMax = 20037508.3427892, z0yMax = 20037508.3427892;
25  double s0 = 559082264.0287178; // scale denominator at zoom level 0 of GoogleCRS84Quad
26 
27  QgsTileMatrix tm;
28  tm.mZoomLevel = zoomLevel;
29  tm.mMatrixWidth = numTiles;
30  tm.mMatrixHeight = numTiles;
31  tm.mTileXSpan = ( z0xMax - z0xMin ) / tm.mMatrixWidth;
32  tm.mTileYSpan = ( z0yMax - z0yMin ) / tm.mMatrixHeight;
33  tm.mExtent = QgsRectangle( z0xMin, z0yMin, z0xMax, z0yMax );
34  tm.mScaleDenom = s0 / pow( 2, zoomLevel );
35  return tm;
36 }
37 
39 {
40  double xMin = mExtent.xMinimum() + mTileXSpan * id.column();
41  double xMax = xMin + mTileXSpan;
42  double yMax = mExtent.yMaximum() - mTileYSpan * id.row();
43  double yMin = yMax - mTileYSpan;
44  return QgsRectangle( xMin, yMin, xMax, yMax );
45 }
46 
48 {
49  double x = mExtent.xMinimum() + mTileXSpan / 2 * id.column();
50  double y = mExtent.yMaximum() - mTileYSpan / 2 * id.row();
51  return QgsPointXY( x, y );
52 }
53 
55 {
56  double x0 = std::clamp( r.xMinimum(), mExtent.xMinimum(), mExtent.xMaximum() );
57  double y0 = std::clamp( r.yMinimum(), mExtent.yMinimum(), mExtent.yMaximum() );
58  double x1 = std::clamp( r.xMaximum(), mExtent.xMinimum(), mExtent.xMaximum() );
59  double y1 = std::clamp( r.yMaximum(), mExtent.yMinimum(), mExtent.yMaximum() );
60  if ( x0 >= x1 || y0 >= y1 )
61  return QgsTileRange(); // nothing to display
62 
63  double tileX1 = ( x0 - mExtent.xMinimum() ) / mTileXSpan;
64  double tileX2 = ( x1 - mExtent.xMinimum() ) / mTileXSpan;
65  double tileY1 = ( mExtent.yMaximum() - y1 ) / mTileYSpan;
66  double tileY2 = ( mExtent.yMaximum() - y0 ) / mTileYSpan;
67 
68  QgsDebugMsgLevel( QStringLiteral( "Tile range of edges [%1,%2] - [%3,%4]" ).arg( tileX1 ).arg( tileY1 ).arg( tileX2 ).arg( tileY2 ), 2 );
69 
70  // figure out tile range from zoom
71  int startColumn = std::clamp( static_cast<int>( floor( tileX1 ) ), 0, mMatrixWidth - 1 );
72  int endColumn = std::clamp( static_cast<int>( floor( tileX2 ) ), 0, mMatrixWidth - 1 );
73  int startRow = std::clamp( static_cast<int>( floor( tileY1 ) ), 0, mMatrixHeight - 1 );
74  int endRow = std::clamp( static_cast<int>( floor( tileY2 ) ), 0, mMatrixHeight - 1 );
75  return QgsTileRange( startColumn, endColumn, startRow, endRow );
76 }
77 
78 QPointF QgsTileMatrix::mapToTileCoordinates( const QgsPointXY &mapPoint ) const
79 {
80  double dx = mapPoint.x() - mExtent.xMinimum();
81  double dy = mExtent.yMaximum() - mapPoint.y();
82  return QPointF( dx / mTileXSpan, dy / mTileYSpan );
83 }
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
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Definition: qgstiles.h:103
QgsRectangle tileExtent(QgsTileXYZ id) const
Returns extent of the given tile in this matrix.
Definition: qgstiles.cpp:38
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
Definition: qgstiles.cpp:78
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent)
Returns tile range that fully covers the given extent.
Definition: qgstiles.cpp:54
static QgsTileMatrix fromWebMercator(int mZoomLevel)
Returns a tile matrix for the usual web mercator.
Definition: qgstiles.cpp:20
QgsPointXY tileCenter(QgsTileXYZ id) const
Returns center of the given tile in this matrix.
Definition: qgstiles.cpp:47
int zoomLevel() const
Returns zoom level of the tile matrix.
Definition: qgstiles.h:110
Range of tiles in a tile matrix to be rendered.
Definition: qgstiles.h:66
Stores coordinates of a tile in a tile matrix set.
Definition: qgstiles.h:33
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39