QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 "qgsline3dsymbol_p.h"
32 #include "qgspoint3dsymbol_p.h"
33 #include "qgspolygon3dsymbol_p.h"
34 
35 #include <QtConcurrent>
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 = nullptr;
56  QString symbolType = mFactory->mSymbol->type();
57  if ( symbolType == QLatin1String( "polygon" ) )
58  handler = Qgs3DSymbolImpl::handlerForPolygon3DSymbol( layer, *static_cast<QgsPolygon3DSymbol *>( mFactory->mSymbol.get() ) );
59  else if ( symbolType == QLatin1String( "point" ) )
60  handler = Qgs3DSymbolImpl::handlerForPoint3DSymbol( layer, *static_cast<QgsPoint3DSymbol *>( mFactory->mSymbol.get() ) );
61  else if ( symbolType == QLatin1String( "line" ) )
62  handler = Qgs3DSymbolImpl::handlerForLine3DSymbol( layer, *static_cast<QgsLine3DSymbol *>( mFactory->mSymbol.get() ) );
63  else
64  {
65  QgsDebugMsg( QStringLiteral( "Unknown 3D symbol type for vector layer: " ) + symbolType );
66  return;
67  }
68  mHandler.reset( handler );
69 
71  exprContext.setFields( layer->fields() );
72  mContext.setExpressionContext( exprContext );
73 
74  QSet<QString> attributeNames;
75  if ( !mHandler->prepare( mContext, attributeNames ) )
76  {
77  QgsDebugMsg( QStringLiteral( "Failed to prepare 3D feature handler!" ) );
78  return;
79  }
80 
81  // build the feature request
83  req.setDestinationCrs( map.crs(), map.transformContext() );
84  req.setSubsetOfAttributes( attributeNames, layer->fields() );
85 
86  // only a subset of data to be queried
87  QgsRectangle rect = Qgs3DUtils::worldToLayerExtent( node->bbox(), mFactory->mLayer->crs(), map.origin(), map.crs(), map.transformContext() );
88  req.setFilterRect( rect );
89 
90  //
91  // this will be run in a background thread
92  //
93 
94  QFuture<void> future = QtConcurrent::run( [req, this]
95  {
96  QgsEventTracing::ScopedEvent e( QStringLiteral( "3D" ), QStringLiteral( "VL chunk load" ) );
97 
98  QgsFeature f;
99  QgsFeatureIterator fi = mSource->getFeatures( req );
100  while ( fi.nextFeature( f ) )
101  {
102  if ( mCanceled )
103  break;
104  mContext.expressionContext().setFeature( f );
105  mHandler->processFeature( f, mContext );
106  }
107  } );
108 
109  // emit finished() as soon as the handler is populated with features
110  mFutureWatcher = new QFutureWatcher<void>( this );
111  mFutureWatcher->setFuture( future );
112  connect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
113 }
114 
115 QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
116 {
117  if ( mFutureWatcher && !mFutureWatcher->isFinished() )
118  {
119  disconnect( mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsChunkQueueJob::finished );
120  mFutureWatcher->waitForFinished();
121  }
122 }
123 
124 void QgsVectorLayerChunkLoader::cancel()
125 {
126  mCanceled = true;
127 }
128 
129 Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
130 {
131  if ( mNode->level() < mFactory->mLeafLevel )
132  {
133  return new Qt3DCore::QEntity( parent ); // dummy entity
134  }
135 
136  Qt3DCore::QEntity *entity = new Qt3DCore::QEntity( parent );
137  mHandler->finalize( entity, mContext );
138  return entity;
139 }
140 
141 
143 
144 
145 QgsVectorLayerChunkLoaderFactory::QgsVectorLayerChunkLoaderFactory( const Qgs3DMapSettings &map, QgsVectorLayer *vl, QgsAbstract3DSymbol *symbol, int leafLevel )
146  : mMap( map )
147  , mLayer( vl )
148  , mSymbol( symbol->clone() )
149  , mLeafLevel( leafLevel )
150 {
151 }
152 
153 QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node ) const
154 {
155  return new QgsVectorLayerChunkLoader( this, node );
156 }
157 
158 
160 
161 
162 QgsVectorLayerChunkedEntity::QgsVectorLayerChunkedEntity( QgsVectorLayer *vl, double zMin, double zMax, const QgsVectorLayer3DTilingSettings &tilingSettings, QgsAbstract3DSymbol *symbol, const Qgs3DMapSettings &map )
163  : QgsChunkedEntity( Qgs3DUtils::layerToWorldExtent( vl->extent(), zMin, zMax, vl->crs(), map.origin(), map.crs(), map.transformContext() ),
164  -1, // rootError (negative error means that the node does not contain anything)
165  -1, // max. allowed screen error (negative tau means that we need to go until leaves are reached)
166  tilingSettings.zoomLevelsCount() - 1,
167  new QgsVectorLayerChunkLoaderFactory( map, vl, symbol, tilingSettings.zoomLevelsCount() - 1 ) )
168 {
169  setShowBoundingBoxes( tilingSettings.showBoundingBoxes() );
170 }
171 
172 QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
173 {
174  // cancel / wait for jobs
175  cancelActiveJobs();
176 }
177 
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature&#39;s geometries.
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used in the 3D scene.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsVectorLayer3DTilingSettings tilingSettings() const
Returns tiling settings of the renderer.
bool showBoundingBoxes() const
Returns whether to display bounding boxes of entity&#39;s tiles (for debugging)
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
3 This class defines configuration of how a vector layer gets tiled for 3D rendering.
QgsVectorLayer * layer() const
Returns vector layer associated with the renderer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
3 Definition of the world
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
QgsVectorLayer3DRenderer * clone() const override
Returns a cloned instance.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
const QgsAbstract3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
Partial snapshot of vector layer&#39;s state (only the members necessary for access to features) ...
static QgsRectangle worldToLayerExtent(const QgsAABB &bbox, const QgsCoordinateReferenceSystem &layerCrs, const QgsVector3D &mapOrigin, const QgsCoordinateReferenceSystem &mapCrs, const QgsCoordinateTransformContext &context)
Converts axis aligned bounding box in 3D world coordinates to extent in map layer CRS...
Definition: qgs3dutils.cpp:471
static QgsExpressionContext globalProjectLayerExpressionContext(QgsVectorLayer *layer)
Returns expression context for use in preparation of 3D data of a layer.
Definition: qgs3dutils.cpp:566
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
3 Miscellaneous utility functions used from 3D code.
Definition: qgs3dutils.h:48