QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsTilingScheme::QgsTilingScheme
QgsTilingScheme()=default
Creates invalid tiling scheme.
QgsRectangle::height
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
Definition: qgsrectangle.h:230
QgsPointXY::y
double y
Definition: qgspointxy.h:63
qgsrectangle.h
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:55
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
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:28
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:198
qgschunknode_p.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
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:36
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:188
QgsTilingScheme::tileToExtent
QgsRectangle tileToExtent(int x, int y, int z) const
Returns map coordinates of the extent of a tile.
Definition: qgstilingscheme.cpp:43
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:58
QgsRectangle::width
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:223
QgsPointXY::x
double x
Definition: qgspointxy.h:62
qgstilingscheme.h