44#include <Qt3DCore/QTransform>
45#include <Qt3DRender/QGeometryRenderer>
46#include <QtConcurrentRun>
48#include "moc_qgsvectorlayerchunkloader_p.cpp"
50using namespace Qt::StringLiterals;
58 : mRenderContext( context )
60 , mSymbol( symbol->clone() )
61 , mNodesAreLeafs( std::make_shared<NodeIsLeafMap>() )
62 , mMaxFeatures( maxFeatures )
67 if ( QgsWkbTypes::geometryType( mLayer->wkbType() ) != Qgis::GeometryType::Point )
70 QgsDebugError( u
"Non-point vector layers in globe scenes are not supported yet!"_s );
71 setupQuadtree( QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
83 mRadius = QgsGlobeUtils::ellipsoidRadius( mCrsToLatLon );
87 QgsDebugError( u
"Error transforming globe ellipsoid extent: %1"_s.arg( e.
what() ) );
88 setupQuadtree(
QgsBox3D( -7e6, -7e6, -7e6, 7e6, 7e6, 7e6 ), -1, 3 );
96 QgsCoordinateTransform layerToLatLon( mLayer->crs(), geographicCrs, context.transformContext() );
97 layerToLatLon.setBallparkTransformsAreAppropriate( true );
100 layerExtentLonLat = layerToLatLon.transformBox3D( mLayer->extent3D() ).toRectangle();
104 QgsDebugError( u
"Error transforming layer extent to lat/lon: %1"_s.arg( e.
what() ) );
112 if ( layerExtentLonLat.
isValid() )
115 layerExtentLonLat.grow( 0.01 );
116 layerExtentLonLat = layerExtentLonLat.intersect( QgsRectangle( -180, -90, 180, 90 ) );
119 mRootNodeId = QgsGlobeUtils::findSmallestIdContainingExtent( layerExtentLonLat );
122 if ( mRootNodeId.d == 0 )
123 rootBox3D =
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() );
124 else if ( mRootNodeId.d == 1 )
125 rootBox3D = mRootNodeId.x == 1 ?
QgsBox3D( -mRadius.x(), 0, -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() )
126 :
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), 0, mRadius.z() );
128 rootBox3D = QgsGlobeUtils::nodeIdToBox3D( mRootNodeId, mCrsToLatLon );
131 setupQuadtree( rootBox3D, rootError );
137 if ( layerExtentInMapCrs.
isValid() )
143 QgsBox3D rootBox3D( extent, zMin, zMax );
144 rootBox3D.grow( 1.0 );
147 setupQuadtree( rootBox3D, rootError );
151QFuture<QgsChunkLoaderResult> QgsVectorLayerChunkLoader::loadChunk( QgsChunkNode *node )
156 QgsDebugError( u
"Unknown 3D symbol type for vector layer: "_s + mSymbol->type() );
157 return QtFuture::makeReadyValueFuture( QgsChunkLoaderResult::sEmpty );
160 std::shared_ptr<QgsFeature3DHandler> handler( handlerPtr );
166 exprContext.
setFields( mLayer->fields() );
169 QSet<QString> attributeNames;
170 if ( !handler->prepare( renderCtx, attributeNames, node->box3D() ) )
173 return QtFuture::makeReadyValueFuture( QgsChunkLoaderResult::sEmpty );
196 QgsDebugError( u
"Error transforming node box3D to layer CRS"_s );
201 const QgsRectangle lonLatRect = QgsGlobeUtils::nodeIdToLonLatRect( node->tileId() );
212 auto source = std::make_unique<QgsVectorLayerFeatureSource>( mLayer );
214 return QtConcurrent::
215 run( &QgsVectorLayerChunkLoader::loadChunkInWorker, std::move( handler ), std::move( renderCtx ), std::move( source ), std::move( req ), node, mMaxFeatures, mNodesAreLeafs, layerToRenderCrs, mIsGeocentric )
216 .then(
this, [
this, node]( ChunkData data ) {
return QgsChunkLoaderResult { std::bind_front( &QgsVectorLayerChunkLoader::createEntity,
this, node, data ) }; } );
219void QgsVectorLayerChunkLoader::loadChunkInWorker(
220 QPromise<ChunkData> &promise,
221 std::shared_ptr<QgsFeature3DHandler> handler,
223 const std::unique_ptr<QgsVectorLayerFeatureSource> &source,
227 const std::shared_ptr<NodeIsLeafMap> &nodesAreLeafs,
232 const QgsScopedEvent e( u
"3D"_s, u
"VL chunk load"_s );
236 int featureCount = 0;
237 bool featureLimitReached =
false;
240 if ( promise.isCanceled() )
243 if ( ++featureCount > maxFeatures )
245 featureLimitReached =
true;
261 QgsDebugError( u
"Error transforming feature %1 geometry to globe CRS: %2"_s.arg( f.
id() ).arg( e.
what() ) );
268 handler->processFeature( f, renderCtx );
271 bool nodeIsLeaf =
false;
272 if ( !featureLimitReached )
274 QgsDebugMsgLevel( u
"All features fetched for node: %1"_s.arg( node->tileId().text() ), 3 );
281 QMutexLocker<QMutex> locker( &nodesAreLeafs->mutex );
282 nodesAreLeafs->map[node->tileId().text()] = nodeIsLeaf;
285 promise.addResult( ChunkData { .handler = std::move( handler ), .renderCtx = std::move( renderCtx ) } );
288Qt3DCore::QEntity *QgsVectorLayerChunkLoader::createEntity( QgsChunkNode *node, ChunkData data, Qt3DCore::QEntity *parent )
291 if ( data.handler->featureCount() == 0 )
296 node->updateParentBoundingBoxesRecursively();
300 Qt3DCore::QEntity *entity =
new Qt3DCore::QEntity( parent );
301 entity->setObjectName( mLayer->name() +
"_" + node->tileId().text() );
302 data.handler->finalize( entity, data.renderCtx );
305 if ( data.handler->zMinimum() != std::numeric_limits<float>::max() && data.handler->zMaximum() != std::numeric_limits<float>::lowest() )
310 node->setExactBox3D( box );
311 node->updateParentBoundingBoxesRecursively();
317QgsChunkNode *QgsVectorLayerChunkLoader::createRootNode()
const
320 return new QgsChunkNode( mRootNodeId, mRootBox3D, mRootError );
322 return QgsQuadtreeChunkLoader::createRootNode();
325QFuture<QVector<QgsChunkNode *>> QgsVectorLayerChunkLoader::createChildren( QgsChunkNode *node )
328 QMutexLocker locker( &mNodesAreLeafs->mutex );
329 if ( mNodesAreLeafs->map.value( node->tileId().text(),
false ) )
330 return QtFuture::makeReadyValueFuture( QVector<QgsChunkNode *> {} );
333 if ( !mIsGeocentric )
334 return QgsQuadtreeChunkLoader::createChildren( node );
336 QVector<QgsChunkNode *> children;
337 if ( mMaxLevel != -1 && node->level() >= mMaxLevel )
338 return QtFuture::makeReadyValueFuture( children );
340 const QgsChunkNodeId nodeId = node->tileId();
341 const float childError = node->error() / 2;
345 const QgsChunkNodeId westId( 1, 0, 0 );
346 const QgsChunkNodeId eastId( 1, 1, 0 );
347 children <<
new QgsChunkNode( westId,
QgsBox3D( -mRadius.x(), -mRadius.y(), -mRadius.z(), mRadius.x(), 0, mRadius.z() ), childError, node );
348 children <<
new QgsChunkNode( eastId,
QgsBox3D( -mRadius.x(), 0, -mRadius.z(), mRadius.x(), mRadius.y(), mRadius.z() ), childError, node );
349 return QtFuture::makeReadyValueFuture( children );
352 for (
int i = 0; i < 4; ++i )
354 const int dx = i & 1, dy = !!( i & 2 );
355 const QgsChunkNodeId childId( nodeId.d + 1, nodeId.x * 2 + dx, nodeId.y * 2 + dy );
356 children <<
new QgsChunkNode( childId, QgsGlobeUtils::nodeIdToBox3D( childId, mCrsToLatLon ), childError, node );
358 return QtFuture::makeReadyValueFuture( children );
365QgsVectorLayerChunkedEntity::QgsVectorLayerChunkedEntity(
368 : QgsAbstractFeatureBasedChunkedEntity( map, 3, new QgsVectorLayerChunkLoader(
Qgs3DRenderContext::fromMapSettings( map ), vl, symbol, zMin, zMax, tilingSettings.maximumChunkFeatures() ), true )
370 onTerrainElevationOffsetChanged();
374QgsVectorLayerChunkedEntity::~QgsVectorLayerChunkedEntity()
381bool QgsVectorLayerChunkedEntity::applyTerrainOffset()
const
383 QgsVectorLayerChunkLoader *loader =
static_cast<QgsVectorLayerChunkLoader *
>( mChunkLoader );
386 QString symbolType = loader->mSymbol.get()->type();
387 if ( symbolType ==
"line" )
395 else if ( symbolType ==
"point" )
403 else if ( symbolType ==
"polygon" )
413 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.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system used in the 3D scene.
QgsExpressionContext & expressionContext()
Gets the expression context.
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 setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
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).
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.
Represents a vector layer which manages a vector based dataset.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QGIS_CHECK_MAIN_THREAD_ACCESS