QGIS API Documentation 4.3.0-Master (16649ce5cc6)
Loading...
Searching...
No Matches
qgsglobeutils_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerchunkloader_p.h
3 --------------------------------------
4 Date : September 2026
5 Copyright : (C) 2026 by Dominik Cindric
6 Email : viper dot miniq 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 "qgsglobeutils_p.h"
17
19#include "qgsexception.h"
20#include "qgslogger.h"
21#include "qgsvector3d.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QgsRectangle QgsGlobeUtils::nodeIdToLonLatRect( QgsChunkNodeId id )
30{
31 if ( id.d == 0 )
32 return QgsRectangle( -180, -90, 180, 90 );
33
34 const double tileSize = 180.0 / std::pow( 2.0, id.d - 1 );
35 const double lonMin = id.x * tileSize - 180.0;
36 const double latMin = id.y * tileSize - 90.0;
37 return QgsRectangle( lonMin, latMin, lonMin + tileSize, latMin + tileSize );
38}
39
40QgsChunkNodeId QgsGlobeUtils::findSmallestIdContainingExtent( const QgsRectangle &lonLatExtent )
41{
42 QgsChunkNodeId id( 0, 0, 0 );
43 if ( !lonLatExtent.isValid() )
44 return id;
45
46 bool descended = true;
47 while ( descended )
48 {
49 descended = false;
50 const int childCount = id.d == 0 ? 2 : 4;
51 for ( int i = 0; i < childCount; ++i )
52 {
53 const int dx = i & 1, dy = id.d == 0 ? 0 : !!( i & 2 );
54 const QgsChunkNodeId childId( id.d + 1, id.x * 2 + dx, id.y * 2 + dy );
55 if ( nodeIdToLonLatRect( childId ).contains( lonLatExtent ) )
56 {
57 id = childId;
58 descended = true;
59 break;
60 }
61 }
62 }
63 return id;
64}
65
66QgsVector3D QgsGlobeUtils::ellipsoidRadius( const QgsCoordinateTransform &globeCrsToLatLon )
67{
68 return QgsVector3D(
69 globeCrsToLatLon.transform( QgsVector3D( 0, 0, 0 ), Qgis::TransformDirection::Reverse ).x(),
70 globeCrsToLatLon.transform( QgsVector3D( 90, 0, 0 ), Qgis::TransformDirection::Reverse ).y(),
71 globeCrsToLatLon.transform( QgsVector3D( 0, 90, 0 ), Qgis::TransformDirection::Reverse ).z()
72 );
73}
74
75QgsBox3D QgsGlobeUtils::nodeIdToBox3D( QgsChunkNodeId id, const QgsCoordinateTransform &globeCrsToLatLon )
76{
77 Q_ASSERT( id.d >= 2 );
78
79 const QgsRectangle rect = nodeIdToLonLatRect( id );
80 const QVector<QgsVector3D> corners = {
81 QgsVector3D( rect.xMinimum(), rect.yMinimum(), 0.0 ),
82 QgsVector3D( rect.xMinimum(), rect.yMaximum(), 0.0 ),
83 QgsVector3D( rect.xMaximum(), rect.yMinimum(), 0.0 ),
84 QgsVector3D( rect.xMaximum(), rect.yMaximum(), 0.0 ),
85 };
86
87 QgsBox3D box3D;
88 for ( const QgsVector3D &corner : corners )
89 {
90 try
91 {
92 const QgsVector3D transformed = globeCrsToLatLon.transform( corner, Qgis::TransformDirection::Reverse );
93 box3D.combineWith( transformed.x(), transformed.y(), transformed.z() );
94 }
95 catch ( const QgsCsException & )
96 {
97 QgsDebugError( u"Failed to transform tile corner to globe CRS"_s );
98 }
99 }
100
101 return box3D;
102}
103
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2864
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
void combineWith(const QgsBox3D &box)
Expands the bbox so that it covers both the original rectangle and the given rectangle.
Definition qgsbox3d.cpp:211
Handles coordinate transforms between two coordinate systems.
QgsPointXY transform(const QgsPointXY &point, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transform the point from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:62
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
#define QgsDebugError(str)
Definition qgslogger.h:71