QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 "qgschunknode_p.h"
19 #include "qgsrectangle.h"
20 
22  : mCrs( crs )
23 {
24  mMapOrigin = QgsPointXY( fullExtent.xMinimum(), fullExtent.yMinimum() );
25  mBaseTileSide = std::max( fullExtent.width(), fullExtent.height() );
26 }
27 
28 QgsPointXY QgsTilingScheme::tileToMap( int x, int y, int z ) const
29 {
30  const double tileSide = mBaseTileSide / pow( 2, z );
31  const double mx = mMapOrigin.x() + x * tileSide;
32  const double my = mMapOrigin.y() + y * tileSide;
33  return QgsPointXY( mx, my );
34 }
35 
36 void QgsTilingScheme::mapToTile( const QgsPointXY &pt, int z, float &x, float &y ) const
37 {
38  const double tileSide = mBaseTileSide / pow( 2, z );
39  x = ( pt.x() - mMapOrigin.x() ) / tileSide;
40  y = ( pt.y() - mMapOrigin.y() ) / tileSide;
41 }
42 
43 QgsRectangle QgsTilingScheme::tileToExtent( int x, int y, int z ) const
44 {
45  const QgsPointXY pt0 = tileToMap( x, y, z );
46  const QgsPointXY pt1 = tileToMap( x + 1, y + 1, z );
47  return QgsRectangle( pt0, pt1 );
48 }
49 
50 QgsRectangle QgsTilingScheme::tileToExtent( const QgsChunkNodeId &nodeId ) const
51 {
52  return tileToExtent( nodeId.x, nodeId.y, nodeId.d );
53 }
54 
55 void QgsTilingScheme::extentToTile( const QgsRectangle &extent, int &x, int &y, int &z ) const
56 {
57  x = y = z = 0; // start with root tile
58  while ( true )
59  {
60  // try to see if any child tile fully contains our extent - if so, go deeper
61  if ( tileToExtent( x * 2, y * 2, z + 1 ).contains( extent ) )
62  {
63  x = x * 2;
64  y = y * 2;
65  }
66  else if ( tileToExtent( x * 2 + 1, y * 2, z + 1 ).contains( extent ) )
67  {
68  x = x * 2 + 1;
69  y = y * 2;
70  }
71  else if ( tileToExtent( x * 2, y * 2 + 1, z + 1 ).contains( extent ) )
72  {
73  x = x * 2;
74  y = y * 2 + 1;
75  }
76  else if ( tileToExtent( x * 2 + 1, y * 2 + 1, z + 1 ).contains( extent ) )
77  {
78  x = x * 2 + 1;
79  y = y * 2 + 1;
80  }
81  else
82  {
83  return; // cannot go deeper
84  }
85  z++;
86  }
87 }
This class represents a coordinate reference system (CRS).
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 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
void mapToTile(const QgsPointXY &pt, int z, float &x, float &y) const
Returns tile coordinates for given map coordinates and Z level.
void extentToTile(const QgsRectangle &extent, int &x, int &y, int &z) const
Returns coordinates of a tile that most tightly fits the whole extent.
QgsPointXY tileToMap(int x, int y, int z) const
Returns map coordinates at tile coordinates (for lower-left corner of the tile)
QgsTilingScheme()=default
Creates invalid tiling scheme.
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
const QgsCoordinateReferenceSystem & crs