37#include <Qt3DCore/QTransform>
38#include <QtConcurrentRun>
40#include "moc_qgscategorizedchunkloader_p.cpp"
42using namespace Qt::StringLiterals;
47QgsCategorizedChunkLoader::QgsCategorizedChunkLoader(
const QgsCategorizedChunkLoaderFactory *factory, QgsChunkNode *node )
48 : QgsChunkLoader( node )
50 , mContext( factory->mRenderContext )
54const QSet<QString> QgsCategorizedChunkLoader::prepareHandlers(
const QgsBox3D &chunkExtent )
57 const QString attributeName = mFactory->mAttributeName;
59 QSet<QString> attributesNames;
63 if ( mAttributeIdx == -1 )
66 mExpression->prepare( &mContext.expressionContext() );
71 mFeaturesHandlerHash.clear();
80 const QVariant value = category.
value();
82 mHandlers.push_back( std::move( handler ) );
83 QgsFeature3DHandler *handlerPtr = mHandlers.back().get();
84 if ( value.userType() == QMetaType::Type::QVariantList )
86 const QVariantList variantList = value.toList();
87 for (
const QVariant &listElt : variantList )
89 mFeaturesHandlerHash.insert( listElt.toString(), handlerPtr );
94 mFeaturesHandlerHash.insert( value.toString(), handlerPtr );
97 handlerPtr->prepare( mContext, attributesNames, chunkExtent );
100 attributesNames.insert( attributeName );
101 return attributesNames;
104void QgsCategorizedChunkLoader::processFeature(
const QgsFeature &feature )
const
109 if ( mAttributeIdx == -1 )
111 Q_ASSERT( mExpression );
112 value = mExpression->evaluate( &mContext.expressionContext() );
116 value = attributes.value( mAttributeIdx );
119 auto handlerIt = mFeaturesHandlerHash.constFind(
QgsVariantUtils::isNull( value ) ? QString() : value.toString() );
120 if ( handlerIt == mFeaturesHandlerHash.constEnd() )
122 if ( mFeaturesHandlerHash.isEmpty() )
128 QgsDebugMsgLevel( u
"Attribute value not found: %1"_s.arg( value.toString() ), 3 );
133 QgsFeature3DHandler *handler = *handlerIt;
134 handler->processFeature( feature, mContext );
137QString QgsCategorizedChunkLoader::filter()
const
143void QgsCategorizedChunkLoader::start()
145 QgsChunkNode *node = chunk();
152 mContext.setExpressionContext( exprContext );
155 const QSet<QString> attributesNames = prepareHandlers( node->box3D() );
165 const QString rendererFilter = filter();
166 if ( !rendererFilter.isEmpty() )
174 mFutureWatcher =
new QFutureWatcher<void>(
this );
176 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, [
this] {
178 mFactory->mNodesAreLeafs[mNode->tileId().text()] = mNodeIsLeaf;
181 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
183 const QFuture<void> future = QtConcurrent::run( [request,
this] {
184 const QgsEventTracing::ScopedEvent event( u
"3D"_s, u
"Categorized chunk load"_s );
187 int featureCount = 0;
188 bool featureLimitReached =
false;
196 if ( ++featureCount > mFactory->mMaxFeatures )
198 featureLimitReached =
true;
202 mContext.expressionContext().setFeature( feature );
203 processFeature( feature );
205 if ( !featureLimitReached )
207 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( mNode->tileId().text() ), 3 );
215 mFutureWatcher->setFuture( future );
218QgsCategorizedChunkLoader::~QgsCategorizedChunkLoader()
220 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
222 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
223 mFutureWatcher->waitForFinished();
227void QgsCategorizedChunkLoader::cancel()
232Qt3DCore::QEntity *QgsCategorizedChunkLoader::createEntity( Qt3DCore::QEntity *parent )
234 long long featureCount = 0;
235 for (
const auto &featureHandler : mHandlers )
237 featureCount += featureHandler->featureCount();
239 if ( featureCount == 0 )
245 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
246 float zMin = std::numeric_limits<float>::max();
247 float zMax = std::numeric_limits<float>::lowest();
248 for (
const auto &featureHandler : mHandlers )
250 featureHandler->finalize( entity, mContext );
251 if ( featureHandler->zMinimum() < zMin )
253 zMin = featureHandler->zMinimum();
255 if ( featureHandler->zMaximum() > zMax )
257 zMax = featureHandler->zMaximum();
262 if ( zMin != std::numeric_limits<float>::max() && zMax != std::numeric_limits<float>::lowest() )
267 mNode->setExactBox3D( box );
268 mNode->updateParentBoundingBoxesRecursively();
278QgsCategorizedChunkLoaderFactory::QgsCategorizedChunkLoaderFactory(
281 : mRenderContext( context )
282 , mLayer( vectorLayer )
283 , mCategories( &renderer->categories() )
284 , mAttributeName( renderer->classAttribute() )
285 , mMaxFeatures( maxFeatures )
291 QgsDebugError( u
"Vector layers in globe scenes are not supported yet!"_s );
292 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
300 extent = mLayer->extent();
302 QgsBox3D rootBox3D( extent, zMin, zMax );
305 rootBox3D.
grow( 1.0 );
308 setupQuadtree( rootBox3D, rootError );
311QgsCategorizedChunkLoaderFactory::~QgsCategorizedChunkLoaderFactory() =
default;
313QgsChunkLoader *QgsCategorizedChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
315 return new QgsCategorizedChunkLoader(
this, node );
318bool QgsCategorizedChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
320 return mNodesAreLeafs.contains( node->tileId().text() );
323QVector<QgsChunkNode *> QgsCategorizedChunkLoaderFactory::createChildren( QgsChunkNode *node )
const
325 if ( mNodesAreLeafs.value( node->tileId().text(),
false ) )
328 return QgsQuadtreeChunkLoaderFactory::createChildren( node );
334QgsCategorizedChunkedEntity::QgsCategorizedChunkedEntity(
337 : QgsAbstractFeatureBasedChunkedEntity(
340 new QgsCategorizedChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( mapSettings ), vectorLayer, renderer, zMin, zMax, tilingSettings.maximumChunkFeatures() ),
344 onTerrainElevationOffsetChanged();
348QgsCategorizedChunkedEntity::~QgsCategorizedChunkedEntity()
355bool QgsCategorizedChunkedEntity::applyTerrainOffset()
const
357 QgsCategorizedChunkLoaderFactory *loaderFactory =
static_cast<QgsCategorizedChunkLoaderFactory *
>( mChunkLoaderFactory );
360 for (
const auto &category : *loaderFactory->mCategories )
365 QString symbolType = symbol->
type();
366 if ( symbolType ==
"line" )
374 else if ( symbolType ==
"point" )
382 else if ( symbolType ==
"polygon" )
392 QgsDebugMsgLevel( u
"QgsRuleBasedChunkedEntityChunkedEntity::applyTerrainOffset, unhandled symbol type %1"_s.arg( symbolType ), 2 );
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
@ Geocentric
Geocentric CRS.
Rendering context for preparation of 3D entities.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
QgsRectangle extent() const
Returns the 3D scene's 2D extent in the 3D scene's CRS.
Represents an individual category (class) from a QgsCategorized3DRenderer.
const QVariant value() const
Returns the value corresponding to this category.
bool renderState() const
Returns true if the category is currently enabled and should be rendered.
QgsAbstract3DSymbol * symbol() const
Returns the symbol which will be used to render this category.
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
virtual QString type() const =0
Returns identifier of symbol type. Each 3D symbol implementation should return a different type.
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
A 3-dimensional box composed of x, y, z coordinates.
void setZMinimum(double z)
Sets the minimum z value.
void setZMaximum(double z)
Sets the maximum z value.
void grow(double delta)
Grows the box in place by the specified amount in all dimensions.
static QString buildCategorizedFilter(const QString &attributeName, const QgsFields &fields, const Categories &categories)
Builds a filter expression from categories.
Qgis::CrsType type() const
Returns the type of the CRS.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
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 & setFilterExpression(const QString &expression)
Set the filter expression.
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...
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
3D symbol that draws linestring geometries as planar polygons (created from lines using a buffer with...
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
3D symbol that draws point geometries as 3D objects using one of the predefined shapes.
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
3D symbol that draws polygon geometries as planar polygons, optionally extruded (with added walls).
Qgis::AltitudeClamping altitudeClamping() const
Returns method that determines altitude (whether to clamp to feature to terrain).
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Defines configuration of how a vector layer gets tiled for 3D rendering.
static double tileGeometryErrorRatio()
This is the ratio of tile's largest size to geometry error and is used when setting the root tile's e...
bool showBoundingBoxes() const
Returns whether to display bounding boxes of entity's tiles (for debugging).
static double maximumLeafExtent()
This is the maximum width or height a tile can have and still be considered a leaf node.
Partial snapshot of vector layer's state (only the members necessary for access to features).
Represents a vector layer which manages a vector based dataset.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)