QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgstilingscheme.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstilingscheme.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 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 "qgstilingscheme.h"
17 
18 #include "qgsrectangle.h"
19 
21  : mCrs( crs )
22 {
23  mMapOrigin = QgsPointXY( fullExtent.xMinimum(), fullExtent.yMinimum() );
24  mBaseTileSide = std::max( fullExtent.width(), fullExtent.height() );
25 }
26 
27 QgsPointXY QgsTilingScheme::tileToMap( int x, int y, int z ) const
28 {
29  double tileSide = mBaseTileSide / pow( 2, z );
30  double mx = mMapOrigin.x() + x * tileSide;
31  double my = mMapOrigin.y() + y * tileSide;
32  return QgsPointXY( mx, my );
33 }
34 
35 void QgsTilingScheme::mapToTile( const QgsPointXY &pt, int z, float &x, float &y ) const
36 {
37  double tileSide = mBaseTileSide / pow( 2, z );
38  x = ( pt.x() - mMapOrigin.x() ) / tileSide;
39  y = ( pt.y() - mMapOrigin.y() ) / tileSide;
40 }
41 
42 QgsRectangle QgsTilingScheme::tileToExtent( int x, int y, int z ) const
43 {
44  QgsPointXY pt0 = tileToMap( x, y, z );
45  QgsPointXY pt1 = tileToMap( x + 1, y + 1, z );
46  return QgsRectangle( pt0, pt1 );
47 }
48 
49 void QgsTilingScheme::extentToTile( const QgsRectangle &extent, int &x, int &y, int &z ) const
50 {
51  x = y = z = 0; // start with root tile
52  while ( true )
53  {
54  // try to see if any child tile fully contains our extent - if so, go deeper
55  if ( tileToExtent( x * 2, y * 2, z + 1 ).contains( extent ) )
56  {
57  x = x * 2;
58  y = y * 2;
59  }
60  else if ( tileToExtent( x * 2 + 1, y * 2, z + 1 ).contains( extent ) )
61  {
62  x = x * 2 + 1;
63  y = y * 2;
64  }
65  else if ( tileToExtent( x * 2, y * 2 + 1, z + 1 ).contains( extent ) )
66  {
67  x = x * 2;
68  y = y * 2 + 1;
69  }
70  else if ( tileToExtent( x * 2 + 1, y * 2 + 1, z + 1 ).contains( extent ) )
71  {
72  x = x * 2 + 1;
73  y = y * 2 + 1;
74  }
75  else
76  {
77  return; // cannot go deeper
78  }
79  z++;
80  }
81 }
QgsTilingScheme::QgsTilingScheme
QgsTilingScheme()=default
Creates invalid tiling scheme.
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:209
QgsPointXY::y
double y
Definition: qgspointxy.h:48
qgsrectangle.h
QgsPointXY::x
Q_GADGET double x
Definition: qgspointxy.h:47
QgsTilingScheme::extentToTile
void extentToTile(const QgsRectangle &extent, int &x, int &y, int &z) const
Returns coordinates of a tile that most tightly fits the whole extent.
Definition: qgstilingscheme.cpp:49
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsTilingScheme::tileToMap
QgsPointXY tileToMap(int x, int y, int z) const
Returns map coordinates at tile coordinates (for lower-left corner of the tile)
Definition: qgstilingscheme.cpp:27
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsTilingScheme::mapToTile
void mapToTile(const QgsPointXY &pt, int z, float &x, float &y) const
Returns tile coordinates for given map coordinates and Z level.
Definition: qgstilingscheme.cpp:35
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:42
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
qgstilingscheme.h