41#include <Qt3DCore/QTransform>
42#include <QtConcurrentRun>
44#include "moc_qgscategorizedchunkloader_p.cpp"
46using namespace Qt::StringLiterals;
51 : mRenderContext( context )
52 , mLayer( vectorLayer )
53 , mCategories( &renderer->categories() )
54 , mAttributeName( renderer->classAttribute() )
55 , mNodesAreLeafs( std::make_shared<NodeIsLeafMap>() )
56 , mMaxFeatures( maxFeatures )
62 QgsDebugError( u
"Vector layers in globe scenes are not supported yet!"_s );
63 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
71 extent = mLayer->extent();
73 QgsBox3D rootBox3D( extent, zMin, zMax );
76 rootBox3D.
grow( 1.0 );
79 setupQuadtree( rootBox3D, rootError );
82QgsCategorizedChunkLoader::~QgsCategorizedChunkLoader() =
default;
84void QgsCategorizedChunkLoader::processFeature( ChunkLoadingContext &ctx,
const QgsFeature &feature )
86 ctx.renderCtx.expressionContext().setFeature( feature );
91 if ( ctx.attributeIdx == -1 )
93 value = ctx.expression->evaluate( &ctx.renderCtx.expressionContext() );
97 value = attributes.value( ctx.attributeIdx );
100 auto handlerIt = ctx.featuresHandlerHash.constFind(
QgsVariantUtils::isNull( value ) ? QString() : value.toString() );
101 if ( handlerIt == ctx.featuresHandlerHash.constEnd() )
103 if ( ctx.featuresHandlerHash.isEmpty() )
109 QgsDebugMsgLevel( u
"Attribute value not found: %1"_s.arg( value.toString() ), 3 );
114 QgsFeature3DHandler *handler = *handlerIt;
115 handler->processFeature( feature, ctx.renderCtx );
119QFuture<QgsChunkLoaderResult> QgsCategorizedChunkLoader::loadChunk( QgsChunkNode *node )
121 ChunkLoadingContext ctx;
123 ctx.renderCtx = mRenderContext;
124 ctx.source = std::make_unique<QgsVectorLayerFeatureSource>( mLayer );
125 ctx.nodesAreLeafs = mNodesAreLeafs;
126 ctx.maxFeatures = mMaxFeatures;
130 exprContext.
setFields( mLayer->fields() );
131 ctx.renderCtx.setExpressionContext( exprContext );
134 QSet<QString> attributesNames;
137 ctx.attributeIdx = mLayer->fields().lookupField( mAttributeName );
138 if ( ctx.attributeIdx == -1 )
141 ctx.expression->prepare( &ctx.renderCtx.expressionContext() );
147 if ( !category.renderState() )
152 const QVariant value = category.value();
154 ctx.handlers.push_back( std::move( handler ) );
155 QgsFeature3DHandler *handlerPtr = ctx.handlers.back().get();
156 if ( value.userType() == QMetaType::Type::QVariantList )
158 const QVariantList variantList = value.toList();
159 for (
const QVariant &listElt : variantList )
161 ctx.featuresHandlerHash.insert( listElt.toString(), handlerPtr );
166 ctx.featuresHandlerHash.insert( value.toString(), handlerPtr );
169 handlerPtr->prepare( ctx.renderCtx, attributesNames, node->box3D() );
171 attributesNames.insert( mAttributeName );
177 request.
setDestinationCrs( ctx.renderCtx.crs(), ctx.renderCtx.transformContext() );
182 if ( !rendererFilter.isEmpty() )
187 return QtConcurrent::run( &QgsCategorizedChunkLoader::loadChunkInWorker, std::move( ctx ), request ).then(
this, [
this]( ChunkLoadingContext ctx ) {
188 return QgsChunkLoaderResult { std::bind_front( &QgsCategorizedChunkLoader::createEntity,
this, ctx ) };
192void QgsCategorizedChunkLoader::loadChunkInWorker( QPromise<ChunkLoadingContext> &promise, ChunkLoadingContext ctx,
QgsFeatureRequest request )
194 const QgsScopedEvent event( u
"3D"_s, u
"Categorized chunk load"_s );
197 int featureCount = 0;
198 bool featureLimitReached =
false;
201 if ( promise.isCanceled() )
206 if ( ++featureCount > ctx.maxFeatures )
208 featureLimitReached =
true;
212 processFeature( ctx, feature );
214 bool nodeIsLeaf =
false;
215 if ( !featureLimitReached )
217 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( ctx.node->tileId().text() ), 3 );
224 QMutexLocker<QMutex> locker( &ctx.nodesAreLeafs->mutex );
225 ctx.nodesAreLeafs->map[ctx.node->tileId().text()] = nodeIsLeaf;
228 promise.addResult( std::move( ctx ) );
231Qt3DCore::QEntity *QgsCategorizedChunkLoader::createEntity( ChunkLoadingContext ctx, Qt3DCore::QEntity *parent )
234 long long featureCount = 0;
235 for (
const auto &featureHandler : ctx.handlers )
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 : ctx.handlers )
250 featureHandler->finalize( entity, ctx.renderCtx );
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 ctx.node->setExactBox3D( box );
268 ctx.node->updateParentBoundingBoxesRecursively();
274QFuture<QVector<QgsChunkNode *>> QgsCategorizedChunkLoader::createChildren( QgsChunkNode *node )
277 QMutexLocker<QMutex> locker( &mNodesAreLeafs->mutex );
278 if ( mNodesAreLeafs->map.value( node->tileId().text(),
false ) )
279 return QtFuture::makeReadyValueFuture( QVector<QgsChunkNode *> {} );
282 return QgsQuadtreeChunkLoader::createChildren( node );
288QgsCategorizedChunkedEntity::QgsCategorizedChunkedEntity(
291 : QgsAbstractFeatureBasedChunkedEntity(
294 new QgsCategorizedChunkLoader(
Qgs3DRenderContext::fromMapSettings( mapSettings ), vectorLayer, renderer, zMin, zMax, tilingSettings.maximumChunkFeatures() ),
298 onTerrainElevationOffsetChanged();
302QgsCategorizedChunkedEntity::~QgsCategorizedChunkedEntity()
309bool QgsCategorizedChunkedEntity::applyTerrainOffset()
const
311 QgsCategorizedChunkLoader *loader =
static_cast<QgsCategorizedChunkLoader *
>( mChunkLoader );
314 for (
const auto &category : *loader->mCategories )
317 if ( category.symbol() )
319 QString symbolType = symbol->
type();
320 if ( symbolType ==
"line" )
328 else if ( symbolType ==
"point" )
336 else if ( symbolType ==
"polygon" )
346 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.
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...
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.
Represents a vector layer which manages a vector based dataset.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QGIS_CHECK_MAIN_THREAD_ACCESS