QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
18 #include <QElapsedTimer>
36 , mSourceType( layer->sourceType() )
37 , mSourcePath( layer->sourcePath() )
38 , mRenderer( layer->renderer()->clone() )
39 , mDrawTileBoundaries( layer->isTileBorderRenderingEnabled() )
41 , mLayerOpacity( layer->opacity() )
42 , mTileMatrixSet( layer->tileMatrixSet() )
57 engine->addProvider( mLabelProvider );
74 if ( !mClippingRegions.empty() )
76 bool needsPainterClipPath =
false;
78 if ( needsPainterClipPath )
87 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles map scale 1 : %1" ).arg( tileRenderScale ), 2 );
90 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles zoom level: %1" ).arg( mTileZoom ), 2 );
92 mTileMatrix = mTileMatrixSet.
tileMatrix( mTileZoom );
95 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles range X: %1 - %2 Y: %3 - %4" )
108 const bool isAsync = ( mSourceType == QLatin1String(
"xyz" ) );
110 if ( mSourceType == QLatin1String(
"xyz" ) && mSourcePath.contains( QLatin1String(
"{usage}" ) ) )
115 mSourcePath.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
118 mSourcePath.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
121 mSourcePath.replace( QLatin1String(
"{usage}" ), QString() );
126 std::unique_ptr<QgsVectorTileLoader> asyncLoader;
127 QList<QgsVectorTileRawData> rawTiles;
130 QElapsedTimer tFetch;
133 QgsDebugMsgLevel( QStringLiteral(
"Tile fetching time: %1" ).arg( tFetch.elapsed() / 1000. ), 2 );
134 QgsDebugMsgLevel( QStringLiteral(
"Fetched tiles: %1" ).arg( rawTiles.count() ), 2 );
138 asyncLoader.reset(
new QgsVectorTileLoader( mSourcePath, mTileMatrix, mTileRange, viewCenter, mAuthCfg, mHeaders, mFeedback.get() ) );
141 QgsDebugMsgLevel( QStringLiteral(
"Got tile asynchronously: " ) + rawTile.id.toString(), 2 );
142 if ( !rawTile.data.isEmpty() )
143 decodeAndDrawTile( rawTile );
152 scope->
setVariable( QStringLiteral(
"zoom_level" ), mTileZoom,
true );
153 scope->
setVariable( QStringLiteral(
"vector_tile_zoom" ), mTileMatrixSet.
scaleToZoom( tileRenderScale ),
true );
156 mRenderer->startRender( *
renderContext(), mTileZoom, mTileRange );
158 QMap<QString, QSet<QString> > requiredFields = mRenderer->usedAttributes( ctx );
160 if ( mLabelProvider )
162 const QMap<QString, QSet<QString> > requiredFieldsLabeling = mLabelProvider->
usedAttributes( ctx, mTileZoom );
163 for (
auto it = requiredFieldsLabeling.begin(); it != requiredFieldsLabeling.end(); ++it )
165 requiredFields[it.key()].unite( it.value() );
169 for (
auto it = requiredFields.constBegin(); it != requiredFields.constEnd(); ++it )
172 mRequiredLayers = mRenderer->requiredLayers( ctx, mTileZoom );
174 if ( mLabelProvider )
176 mLabelProvider->
setFields( mPerLayerFields );
177 QSet<QString> attributeNames;
178 if ( !mLabelProvider->
prepare( ctx, attributeNames ) )
181 mLabelProvider =
nullptr;
185 mRequiredLayers.unite( mLabelProvider->
requiredLayers( ctx, mTileZoom ) );
196 decodeAndDrawTile( rawTile );
203 asyncLoader->downloadBlocking();
204 if ( !asyncLoader->error().isEmpty() )
205 mErrors.append( asyncLoader->error() );
208 mRenderer->stopRender( ctx );
210 QgsDebugMsgLevel( QStringLiteral(
"Total time for decoding: %1" ).arg( mTotalDecodeTime / 1000. ), 2 );
211 QgsDebugMsgLevel( QStringLiteral(
"Drawing time: %1" ).arg( mTotalDrawTime / 1000. ), 2 );
212 QgsDebugMsgLevel( QStringLiteral(
"Total time: %1" ).arg( tTotal.elapsed() / 1000. ), 2 );
233 if ( !decoder.decode( rawTile.
id, rawTile.
data ) )
245 tile.setFields( mPerLayerFields );
246 tile.setFeatures( decoder.layerFeatures( mPerLayerFields, ct, &mRequiredLayers ) );
258 mTotalDecodeTime += tLoad.elapsed();
270 ctx.
painter()->setClipRegion( QRegion( tile.tilePolygon() ), Qt::IntersectClip );
275 mRenderer->renderTile( tile, ctx );
276 mTotalDrawTime += tDraw.elapsed();
279 if ( mLabelProvider )
282 if ( mDrawTileBoundaries )
285 ctx.
painter()->setClipping(
false );
289 QBrush brush( QColor( 255, 0, 0, 40 ), Qt::BrushStyle::Dense3Pattern );
292 ctx.
painter()->setBrush( brush );
293 ctx.
painter()->drawPolygon( tile.tilePolygon() );
295 ctx.
painter()->setBrush( QBrush( QColor( 255, 0, 0 ) ) );
296 ctx.
painter()->drawText( tile.tilePolygon().boundingRect().center(), tile.id().toString() );
static QList< QgsMapClippingRegion > collectClippingRegionsForLayer(const QgsRenderContext &context, const QgsMapLayer *layer)
Collects the list of map clipping regions from a context which apply to a map layer.
RAII class to pop scope from an expression context on destruction.
Class for storing the component parts of a RDBMS data source URI (e.g. a Postgres data source).
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
Implements a map layer that is dedicated to rendering of vector tiles. Vector tiles compared to "ordi...
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
QgsExpressionContext & expressionContext()
Gets the expression context.
#define QgsDebugMsgLevel(str, level)
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
void removeProvider(QgsAbstractLabelProvider *provider)
Remove provider if the provider's initialization failed. Provider instance is deleted.
Qgis::RendererUsage rendererUsage() const
Returns the renderer usage.
bool testFlag(Qgis::RenderContextFlag flag) const
Check whether a particular flag is enabled.
QgsVectorTileLayerRenderer(QgsVectorTileLayer *layer, QgsRenderContext &context)
Creates the renderer. Always called from main thread, should copy whatever necessary from the layer.
Contains information about the context of a rendering operation.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
Contains decoded features of a single vector tile and any other data necessary for rendering of it.
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
This class is responsible for decoding raw tile data written with Mapbox Vector Tiles encoding.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
virtual QgsVectorTileLabelProvider * provider(QgsVectorTileLayer *layer) const SIP_SKIP
Factory for label provider implementation.
@ Export
Renderer used for printing or exporting to a file.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
int endRow() const
Returns index of the last row in the range.
Custom exception class for Coordinate Reference System related exceptions.
virtual void setFields(const QMap< QString, QgsFields > &perLayerFields)=0
Sets fields for each sub-layer.
void tileRequestFinished(const QgsVectorTileRawData &rawTile)
Emitted when a tile request has finished. If a tile request has failed, the returned raw tile byte ar...
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QString &sourceType, const QString &sourcePath, const QgsTileMatrix &tileMatrix, const QPointF &viewCenter, const QgsTileRange &range, const QString &authid, const QgsHttpHeaders &headers, QgsFeedback *feedback=nullptr)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
virtual QMap< QString, QSet< QString > > usedAttributes(const QgsRenderContext &context, int tileZoom) const =0
Returns field names for each sub-layer that are required for labeling.
int scaleToZoomLevel(double scale) const
Finds the best fitting (integer) zoom level given a map scale denominator.
static QPainterPath calculatePainterClipRegion(const QList< QgsMapClippingRegion > ®ions, const QgsRenderContext &context, QgsMapLayerType layerType, bool &shouldClip)
Returns a QPainterPath representing the intersection of clipping regions from context which should be...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Base class for feedback objects to be used for cancellation of something running in a worker thread.
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent) const
Returns tile range that fully covers the given extent.
virtual QSet< QString > requiredLayers(QgsRenderContext &context, int tileZoom) const
Returns a list of the layers required for labeling.
QString toString() const
Returns tile coordinates in a formatted string.
The loader class takes care of loading raw vector tile data from a tile source.
double scaleForRenderContext(const QgsRenderContext &context) const
Calculates the correct scale to use for the tiles when rendered using the specified render context.
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
int endColumn() const
Returns index of the last column in the range.
Scoped object for saving and restoring a QPainter object's state.
@ Unknown
Renderer used for unknown usage.
Single scope for storing variables and functions for use within a QgsExpressionContext....
double scaleToZoom(double scale) const
Calculates a fractional zoom level given a map scale denominator.
The QgsLabelingEngine class provides map labeling functionality. The input for the engine is a list o...
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QgsLabelingEngine * labelingEngine() const
Gets access to new labeling engine (may be nullptr).
QString source() const
Returns the source for the layer.
int startRow() const
Returns index of the first row in the range.
virtual void registerTileFeatures(const QgsVectorTileRendererData &tile, QgsRenderContext &context)=0
Registers label features for given tile to the labeling engine.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QgsVectorTileLabeling * labeling() const
Returns currently assigned labeling.
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
Keeps track of raw tile data that need to be decoded.
@ VectorTileLayer
Vector tile layer. Added in QGIS 3.14.
static QgsFields makeQgisFields(const QSet< QString > &flds)
Returns QgsFields instance based on the set of field names.
QgsTileXYZ id
Tile position in tile matrix set.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
QPainter * painter()
Returns the destination QPainter for the render operation.
@ View
Renderer used for displaying on screen.
@ UseAdvancedEffects
Enable layer opacity and blending effects.
static QPolygon tilePolygon(QgsTileXYZ id, const QgsCoordinateTransform &ct, const QgsTileMatrix &tm, const QgsMapToPixel &mtp)
Returns polygon (made by four corners of the tile) in screen coordinates.
int startColumn() const
Returns index of the first column in the range.
virtual bool render() override
Do the rendering (based on data stored in the class).
QgsHttpHeaders httpHeaders() const
Returns http headers.
QByteArray data
Raw tile data.