QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsvectorlayerchunkloader_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerchunkloader_p.cpp
3  --------------------------------------
4  Date : July 2019
5  Copyright : (C) 2019 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 
17 
18 #include "qgs3dutils.h"
20 #include "qgschunknode_p.h"
21 #include "qgspolygon3dsymbol_p.h"
22 #include "qgseventtracing.h"
23 #include "qgslogger.h"
24 #include "qgsvectorlayer.h"
26 
27 #include "qgsline3dsymbol.h"
28 #include "qgspoint3dsymbol.h"
29 #include "qgspolygon3dsymbol.h"
30 
31 #include "qgsapplication.h"
32 #include "qgs3dsymbolregistry.h"
33 
34 #include <QtConcurrent>
35 #include <Qt3DCore/QTransform>
36 
38 
39 
40 QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader( const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
41  : QgsChunkLoader( node )
42  , mFactory( factory )
43  , mContext( factory->mMap )
44  , mSource( new QgsVectorLayerFeatureSource( factory->mLayer ) )
45 {
46  if ( node->level() < mFactory->mLeafLevel )
47  {
48  QTimer::singleShot( 0, this, &QgsVectorLayerChunkLoader::finished );
49  return;
50  }
51 
52  QgsVectorLayer *layer = mFactory->mLayer;
53  const Qgs3DMapSettings &map = mFactory->mMap;
54 
55  QgsFeature3DHandler *handler = QgsApplication::symbol3DRegistry()->createHandlerForSymbol( layer, mFactory->mSymbol.get() );
56  if ( !handler )
57  {
58  QgsDebugMsg( QStringLiteral( "Unknown 3D symbol type for vector layer: " ) + mFactory->mSymbol->type() );
59  return;
60  }
61  mHandler.reset( handler );
62 
64  exprContext.setFields( layer->fields() );
65  mContext.setExpressionContext( exprContext );
66 
67  QSet<QString> attributeNames;
68  if ( !mHandler->prepare( mContext, attributeNames ) )
69  {
70  QgsDebugMsg( QStringLiteral( "Failed to prepare 3D feature handler!" ) );
71  return;
72  }
73 
74  // build the feature request
76  req.setDestinationCrs( map.crs(), map.transformContext() );
77  req.setSubsetOfAttributes( attributeNames, layer->fields() );
78 
79  // only a subset of data to be queried
80  const QgsRectangle rect = Qgs3DUtils::worldToMapExtent( node->bbox(), map.origin() );
81  req.setFilterRect( rect );
82 
83  //
84  // this will be run in a background thread
85  //
86  mFutureWatcher = new QFutureWatcher<void>( this );
87  connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
88 
89  const QFuture<void> future = QtConcurrent::run( [req, this]
90  {
91  const QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "VL chunk load" ) );
92 
93  QgsFeature f;
94  QgsFeatureIterator fi = mSource->getFeatures( req );
95  while ( fi.nextFeature( f ) )
96  {
97  if ( mCanceled )
98  break;
99  mContext.expressionContext().setFeature( f );
100  mHandler->processFeature( f, mContext );
101  }
102  } );
103 
104  // emit finished() as soon as the handler is populated with features
105  mFutureWatcher->setFuture( future );
106 }
107 
108 QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
109 {
110  if ( mFutureWatcher && !mFutureWatcher->isFinished() )
111  {
112  disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
113  mFutureWatcher->waitForFinished();
114  }
115 }
116 
117 void QgsVectorLayerChunkLoader::cancel()
118 {
119  mCanceled = true;
120 }
121 
122 Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
123 {
124  if ( mNode->level() < mFactory->mLeafLevel )
125  {
126  return new Qt3DCore::QEntity( parent ); // dummy entity
127  }
128 
129  Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
130  mHandler->finalize( entity, mContext );
131  return entity;
132 }
133 
134 
136 
137 
138 QgsVectorLayerChunkLoaderFactory::QgsVectorLayerChunkLoaderFactory( const Qgs3DMapSettings &map, QgsVectorLayer *vl, QgsAbstract3DSymbol *symbol, int leafLevel, double zMin, double zMax )
139  : mMap( map )
140  , mLayer( vl )
141  , mSymbol( symbol->clone() )
142  , mLeafLevel( leafLevel )
143 {
144  QgsAABB rootBbox = Qgs3DUtils::layerToWorldExtent( vl->extent(), zMin, zMax, vl->crs(), map.origin(), map.crs(), map.transformContext() );
145  // add small padding to avoid clipping of point features located at the edge of the bounding box
146  rootBbox.xMin -= 1.0;
147  rootBbox.xMax += 1.0;
148  rootBbox.yMin -= 1.0;
149  rootBbox.yMax += 1.0;
150  rootBbox.zMin -= 1.0;
151  rootBbox.zMax += 1.0;
152  setupQuadtree( rootBbox, -1, leafLevel ); // negative root error means that the node does not contain anything
153 }
154 
155 QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
156 {
157  return new QgsVectorLayerChunkLoader( this, node );
158 }
159 
160 
162 
163 
164 QgsVectorLayerChunkedEntity::QgsVectorLayerChunkedEntity( QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsAbstract3DSymbol *symbol, const Qgs3DMapSettings &map )
165  : QgsChunkedEntity( -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
166  new QgsVectorLayerChunkLoaderFactory( map, vl, symbol, tilingSettings.zoomLevelsCount() - 1, zMin, zMax ), true )
167 {
168  mTransform = new Qt3DCore::QTransform;
169  mTransform->setTranslation( QVector3D( 0.0f, map.terrainElevationOffset(), 0.0f ) );
170  this->addComponent( mTransform );
171 
172  connect( &map, &Qgs3DMapSettings::terrainElevationOffsetChanged, this, &QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged );
173 
174  setShowBoundingBoxes( tilingSettings.showBoundingBoxes() );
175 }
176 
177 QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
178 {
179  // cancel / wait for jobs
180  cancelActiveJobs();
181 }
182 
183 void QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged( float newOffset )
184 {
185  QgsDebugMsgLevel( QStringLiteral( "QgsVectorLayerChunkedEntity::onTerrainElevationOffsetChanged" ), 2 );
186  mTransform->setTranslation( QVector3D( 0.0f, newOffset, 0.0f ) );
187 }
188 
float terrainElevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down)
void terrainElevationOffsetChanged(float newElevation)
Emitted when the terrain elevation offset is changed.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
QgsFeature3DHandler * createHandlerForSymbol(QgsVectorLayer *layer, const QgsAbstract3DSymbol *symbol)
Creates a feature handler for a symbol, for the specified vector layer.
static QgsRectangle worldToMapExtent(const QgsAABB &bbox, const QgsVector3D &mapOrigin)
Converts axis aligned bounding box in 3D world coordinates to extent in map coordinates.
Definition: qgs3dutils.cpp:493
static QgsAABB layerToWorldExtent(const QgsRectangle &extent, double zMin, double zMax, const QgsCoordinateReferenceSystem &layerCrs, const QgsVector3D &mapOrigin, const QgsCoordinateReferenceSystem &mapCrs, const QgsCoordinateTransformContext &context)
Converts extent (in map layer's CRS) to axis aligned bounding box in 3D world coordinates.
Definition: qgs3dutils.cpp:470
static QgsExpressionContext globalProjectLayerExpressionContext(QgsVectorLayer *layer)
Returns expression context for use in preparation of 3D data of a layer.
Definition: qgs3dutils.cpp:556
3
Definition: qgsaabb.h:34
float yMax
Definition: qgsaabb.h:85
float xMax
Definition: qgsaabb.h:84
float xMin
Definition: qgsaabb.h:81
float zMax
Definition: qgsaabb.h:86
float yMin
Definition: qgsaabb.h:82
float zMin
Definition: qgsaabb.h:83
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
A rectangle specified with double values.
Definition: qgsrectangle.h:42
bool showBoundingBoxes() const
Returns whether to display bounding boxes of entity's tiles (for debugging)
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsRectangle extent() const FINAL
Returns the extent of the layer.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define QgsDebugMsg(str)
Definition: qgslogger.h:38