QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 = qBound( mExtent.xMinimum(), r.xMinimum(), mExtent.xMaximum() );
57  double y0 = qBound( mExtent.yMinimum(), r.yMinimum(), mExtent.yMaximum() );
58  double x1 = qBound( mExtent.xMinimum(), r.xMaximum(), mExtent.xMaximum() );
59  double y1 = qBound( mExtent.yMinimum(), r.yMaximum(), 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 = qBound( 0, static_cast<int>( floor( tileX1 ) ), mMatrixWidth - 1 );
72  int endColumn = qBound( 0, static_cast<int>( floor( tileX2 ) ), mMatrixWidth - 1 );
73  int startRow = qBound( 0, static_cast<int>( floor( tileY1 ) ), mMatrixHeight - 1 );
74  int endRow = qBound( 0, static_cast<int>( floor( tileY2 ) ), 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 }
QgsPointXY::y
double y
Definition: qgspointxy.h:48
QgsTileXYZ
Definition: qgstiles.h:32
QgsTileRange
Definition: qgstiles.h:65
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsTileMatrix::tileExtent
QgsRectangle tileExtent(QgsTileXYZ id) const
Returns extent of the given tile in this matrix.
Definition: qgstiles.cpp:38
QgsRectangle::xMaximum
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
QgsRectangle
Definition: qgsrectangle.h:41
QgsTileMatrix::zoomLevel
int zoomLevel() const
Returns zoom level of the tile matrix.
Definition: qgstiles.h:110
QgsTileMatrix
Definition: qgstiles.h:102
QgsRectangle::yMaximum
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
QgsPointXY
Definition: qgspointxy.h:43
qgstiles.h
QgsTileMatrix::mapToTileCoordinates
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
Definition: qgstiles.cpp:78
QgsPointXY::x
double x
Definition: qgspointxy.h:47
QgsRectangle::yMinimum
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
QgsTileMatrix::fromWebMercator
static QgsTileMatrix fromWebMercator(int mZoomLevel)
Returns a tile matrix for the usual web mercator.
Definition: qgstiles.cpp:20
qgslogger.h
QgsTileMatrix::tileCenter
QgsPointXY tileCenter(QgsTileXYZ id) const
Returns center of the given tile in this matrix.
Definition: qgstiles.cpp:47
QgsTileMatrix::tileRangeFromExtent
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent)
Returns tile range that fully covers the given extent.
Definition: qgstiles.cpp:54
QgsRectangle::xMinimum
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167