40#include <Qt3DCore/QTransform>
41#include <Qt3DRender/QGeometryRenderer>
42#include <QtConcurrentRun>
44#include "moc_qgsvectorlayerchunkloader_p.cpp"
46using namespace Qt::StringLiterals;
51QgsVectorLayerChunkLoader::QgsVectorLayerChunkLoader(
const QgsVectorLayerChunkLoaderFactory *factory, QgsChunkNode *node )
52 : QgsChunkLoader( node )
54 , mRenderContext( factory->mRenderContext )
58void QgsVectorLayerChunkLoader::start()
60 QgsChunkNode *node = chunk();
63 mLayerName = mFactory->mLayer->
name();
68 QgsDebugError( u
"Unknown 3D symbol type for vector layer: "_s + mFactory->mSymbol->type() );
71 mHandler.reset( handler );
76 mRenderContext.setExpressionContext( exprContext );
78 QSet<QString> attributeNames;
79 if ( !mHandler->prepare( mRenderContext, attributeNames, node->box3D() ) )
91 if ( mFactory->mIsGeocentric )
105 QgsDebugError( u
"Error transforming node box3D to layer CRS"_s );
110 const QgsRectangle lonLatRect = QgsGlobeUtils::nodeIdToLonLatRect( node->tileId() );
124 mFutureWatcher =
new QFutureWatcher<void>(
this );
126 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, [
this] {
128 mFactory->mNodesAreLeafs[mNode->tileId().text()] = mNodeIsLeaf;
131 connect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
133 const bool isGeocentric = mFactory->mIsGeocentric;
134 const QFuture<void> future = QtConcurrent::run( [req = std::move( req ), layerToRenderCrs, isGeocentric,
this] {
135 const QgsScopedEvent e( u
"3D"_s, u
"VL chunk load"_s );
139 int featureCount = 0;
140 bool featureLimitReached =
false;
146 if ( ++featureCount > mFactory->mMaxFeatures )
148 featureLimitReached =
true;
164 QgsDebugError( u
"Error transforming feature %1 geometry to globe CRS: %2"_s.arg( f.
id() ).arg( e.
what() ) );
170 mRenderContext.expressionContext().setFeature( f );
171 mHandler->processFeature( f, mRenderContext );
174 if ( !featureLimitReached )
176 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( mNode->tileId().text() ), 3 );
184 mFutureWatcher->setFuture( future );
187QgsVectorLayerChunkLoader::~QgsVectorLayerChunkLoader()
189 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
191 disconnect( mFutureWatcher, &QFutureWatcher<void>::finished,
this, &QgsChunkQueueJob::finished );
192 mFutureWatcher->waitForFinished();
196void QgsVectorLayerChunkLoader::cancel()
201Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( Qt3DCore::QEntity *parent )
203 if ( mHandler->featureCount() == 0 )
208 mNode->updateParentBoundingBoxesRecursively();
212 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
213 entity->setObjectName( mLayerName +
"_" + mNode->tileId().text() );
214 mHandler->finalize( entity, mRenderContext );
217 if ( mHandler->zMinimum() != std::numeric_limits<float>::max() && mHandler->zMaximum() != std::numeric_limits<float>::lowest() )
222 mNode->setExactBox3D( box );
223 mNode->updateParentBoundingBoxesRecursively();
234 : mRenderContext( context )
236 , mSymbol( symbol->clone() )
237 , mMaxFeatures( maxFeatures )
242 if ( QgsWkbTypes::geometryType( mLayer->wkbType() ) != Qgis::GeometryType::Point )
245 QgsDebugError( u
"Non-point vector layers in globe scenes are not supported yet!"_s );
246 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
250 mIsGeocentric =
true;
258 mRadius = QgsGlobeUtils::ellipsoidRadius( mCrsToLatLon );
262 QgsDebugError( u
"Error transforming globe ellipsoid extent: %1"_s.arg( e.
what() ) );
263 setupQuadtree(
QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
271 QgsCoordinateTransform layerToLatLon( mLayer->crs(), geographicCrs, context.transformContext() );
272 layerToLatLon.setBallparkTransformsAreAppropriate( true );
275 layerExtentLonLat = layerToLatLon.transformBox3D( mLayer->extent3D() ).toRectangle();
279 QgsDebugError( u
"Error transforming layer extent to lat/lon: %1"_s.arg( e.
what() ) );
287 if ( layerExtentLonLat.
isValid() )
290 layerExtentLonLat.grow( 0.01 );
291 layerExtentLonLat = layerExtentLonLat.intersect( QgsRectangle( -180, -90, 180, 90 ) );
294 mRootNodeId = QgsGlobeUtils::findSmallestIdContainingExtent( layerExtentLonLat );
297 if ( mRootNodeId.d == 0 )
298 rootBox3D =
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() );
299 else if ( mRootNodeId.d == 1 )
300 rootBox3D = mRootNodeId.x == 1 ?
QgsBox3D( -mRadius.x(), 0, -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() )
301 :
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), 0, mRadius.z() );
303 rootBox3D = QgsGlobeUtils::nodeIdToBox3D( mRootNodeId, mCrsToLatLon );
306 setupQuadtree( rootBox3D, rootError );
312 if ( layerExtentInMapCrs.
isValid() )
318 QgsBox3D rootBox3D( extent, zMin, zMax );
319 rootBox3D.grow( 1.0 );
322 setupQuadtree( rootBox3D, rootError );
326QgsChunkLoader *QgsVectorLayerChunkLoaderFactory::createChunkLoader( QgsChunkNode *node )
const
328 return new QgsVectorLayerChunkLoader(
this, node );
331QgsChunkNode *QgsVectorLayerChunkLoaderFactory::createRootNode()
const
334 return new QgsChunkNode( mRootNodeId, mRootBox3D, mRootError );
336 return QgsQuadtreeChunkLoaderFactory::createRootNode();
339bool QgsVectorLayerChunkLoaderFactory::canCreateChildren( QgsChunkNode *node )
341 return mNodesAreLeafs.contains( node->tileId().text() );
344QVector<QgsChunkNode *> QgsVectorLayerChunkLoaderFactory::createChildren( QgsChunkNode *node )
const
346 if ( mNodesAreLeafs.value( node->tileId().text(),
false ) )
349 if ( !mIsGeocentric )
350 return QgsQuadtreeChunkLoaderFactory::createChildren( node );
352 QVector<QgsChunkNode *> children;
353 if ( mMaxLevel != -1 && node->level() >= mMaxLevel )
356 const QgsChunkNodeId nodeId = node->tileId();
357 const float childError = node->error() / 2;
361 const QgsChunkNodeId westId( 1, 0, 0 );
362 const QgsChunkNodeId eastId( 1, 1, 0 );
363 children <<
new QgsChunkNode( westId,
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), 0, mRadius.z() ), childError, node );
364 children <<
new QgsChunkNode( eastId,
QgsBox3D( -mRadius.x(), 0, -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() ), childError, node );
368 for (
int i = 0; i < 4; ++i )
370 const int dx = i & 1, dy = !!( i & 2 );
371 const QgsChunkNodeId childId( nodeId.d + 1, nodeId.x * 2 + dx, nodeId.y * 2 + dy );
372 children <<
new QgsChunkNode( childId, QgsGlobeUtils::nodeIdToBox3D( childId, mCrsToLatLon ), childError, node );
381QgsVectorLayerChunkedEntity::QgsVectorLayerChunkedEntity(
384 : QgsAbstractFeatureBasedChunkedEntity( map, 3, new QgsVectorLayerChunkLoaderFactory(
Qgs3DRenderContext::fromMapSettings( map ), vl, symbol, zMin, zMax, tilingSettings.maximumChunkFeatures() ), true )
386 onTerrainElevationOffsetChanged();
390QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
397bool QgsVectorLayerChunkedEntity::applyTerrainOffset()
const
399 QgsVectorLayerChunkLoaderFactory *loaderFactory =
static_cast<QgsVectorLayerChunkLoaderFactory *
>( mChunkLoaderFactory );
402 QString symbolType = loaderFactory->mSymbol.get()->type();
403 if ( symbolType ==
"line" )
411 else if ( symbolType ==
"point" )
419 else if ( symbolType ==
"polygon" )
429 QgsDebugMsgLevel( u
"QgsVectorLayerChunkedEntity::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.
@ Forward
Forward transform (from source to destination).
@ Reverse
Reverse/inverse transform (from destination to source).
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.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsFeature3DHandler * createHandlerForSymbol(const QgsVectorLayer *layer, const QgsAbstract3DSymbol *symbol)
Creates a feature handler for a symbol, for the specified vector layer.
static QgsRectangle tryReprojectExtent2D(const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs1, const QgsCoordinateReferenceSystem &crs2, const QgsCoordinateTransformContext &context)
Reprojects extent from crs1 to crs2 coordinate reference system with context context.
Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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.
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Represents a coordinate reference system (CRS).
QgsCoordinateReferenceSystem toGeographicCrs() const
Returns the geographic CRS associated with this CRS object.
Qgis::CrsType type() const
Returns the type of the CRS.
Custom exception class for Coordinate Reference System related exceptions.
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.
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 & setCoordinateTransform(const QgsCoordinateTransform &transform)
Sets the coordinate transform which will be used to transform the feature's geometries.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
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...
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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).
QgsCoordinateReferenceSystem crs3D
QgsCoordinateReferenceSystem crs
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.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
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)