QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 , mFullExtent( fullExtent )
24{
26 mBaseTileSide = std::max( fullExtent.width(), fullExtent.height() );
27}
28
29QgsPointXY QgsTilingScheme::tileToMap( int x, int y, int z ) const
30{
31 const double tileSide = mBaseTileSide / pow( 2, z );
32 const double mx = mMapOrigin.x() + x * tileSide;
33 const double my = mMapOrigin.y() + y * tileSide;
34 return QgsPointXY( mx, my );
35}
36
37void QgsTilingScheme::mapToTile( const QgsPointXY &pt, int z, float &x, float &y ) const
38{
39 const double tileSide = mBaseTileSide / pow( 2, z );
40 x = ( pt.x() - mMapOrigin.x() ) / tileSide;
41 y = ( pt.y() - mMapOrigin.y() ) / tileSide;
42}
43
44QgsRectangle QgsTilingScheme::tileToExtent( int x, int y, int z ) const
45{
46 const QgsPointXY pt0 = tileToMap( x, y, z );
47 const QgsPointXY pt1 = tileToMap( x + 1, y + 1, z );
48 return QgsRectangle( pt0, pt1 );
49}
50
51QgsRectangle QgsTilingScheme::tileToExtent( const QgsChunkNodeId &nodeId ) const
52{
53 return tileToExtent( nodeId.x, nodeId.y, nodeId.d );
54}
55
56void QgsTilingScheme::extentToTile( const QgsRectangle &extent, int &x, int &y, int &z ) const
57{
58 x = y = z = 0; // start with root tile
59 while ( true )
60 {
61 // try to see if any child tile fully contains our extent - if so, go deeper
62 if ( tileToExtent( x * 2, y * 2, z + 1 ).contains( extent ) )
63 {
64 x = x * 2;
65 y = y * 2;
66 }
67 else if ( tileToExtent( x * 2 + 1, y * 2, z + 1 ).contains( extent ) )
68 {
69 x = x * 2 + 1;
70 y = y * 2;
71 }
72 else if ( tileToExtent( x * 2, y * 2 + 1, z + 1 ).contains( extent ) )
73 {
74 x = x * 2;
75 y = y * 2 + 1;
76 }
77 else if ( tileToExtent( x * 2 + 1, y * 2 + 1, z + 1 ).contains( extent ) )
78 {
79 x = x * 2 + 1;
80 y = y * 2 + 1;
81 }
82 else
83 {
84 return; // cannot go deeper
85 }
86 z++;
87 }
88}
This class represents a coordinate reference system (CRS).
A class to represent a 2D point.
Definition: qgspointxy.h:60
double y
Definition: qgspointxy.h:64
Q_GADGET double x
Definition: qgspointxy.h:63
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:201
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:211
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:236
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:243
void mapToTile(const QgsPointXY &pt, int z, float &x, float &y) const
Returns tile coordinates for given map coordinates and Z level.
QgsRectangle fullExtent() const
Returns the full extent used in the constructor, which might not be square.
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