18 #include <QElapsedTimer>
36 , mSourceType( layer->sourceType() )
37 , mSourcePath( layer->sourcePath() )
38 , mSourceMinZoom( layer->sourceMinZoom() )
39 , mSourceMaxZoom( layer->sourceMaxZoom() )
40 , mRenderer( layer->renderer()->clone() )
41 , mDrawTileBoundaries( layer->isTileBorderRenderingEnabled() )
43 , mLayerOpacity( layer->opacity() )
49 mReferer = dsUri.
param( QStringLiteral(
"referer" ) );
58 engine->addProvider( mLabelProvider );
75 if ( !mClippingRegions.empty() )
77 bool needsPainterClipPath =
false;
79 if ( needsPainterClipPath )
90 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles zoom level: %1" ).arg( mTileZoom ), 2 );
95 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles range X: %1 - %2 Y: %3 - %4" )
108 const bool isAsync = ( mSourceType == QLatin1String(
"xyz" ) );
110 std::unique_ptr<QgsVectorTileLoader> asyncLoader;
111 QList<QgsVectorTileRawData> rawTiles;
114 QElapsedTimer tFetch;
117 QgsDebugMsgLevel( QStringLiteral(
"Tile fetching time: %1" ).arg( tFetch.elapsed() / 1000. ), 2 );
118 QgsDebugMsgLevel( QStringLiteral(
"Fetched tiles: %1" ).arg( rawTiles.count() ), 2 );
122 asyncLoader.reset(
new QgsVectorTileLoader( mSourcePath, mTileMatrix, mTileRange, viewCenter, mAuthCfg, mReferer, mFeedback.get() ) );
125 QgsDebugMsgLevel( QStringLiteral(
"Got tile asynchronously: " ) + rawTile.id.toString(), 2 );
126 if ( !rawTile.data.isEmpty() )
127 decodeAndDrawTile( rawTile );
136 scope->
setVariable( QStringLiteral(
"zoom_level" ), mTileZoom,
true );
140 mRenderer->startRender( *
renderContext(), mTileZoom, mTileRange );
142 QMap<QString, QSet<QString> > requiredFields = mRenderer->usedAttributes( ctx );
144 if ( mLabelProvider )
146 const QMap<QString, QSet<QString> > requiredFieldsLabeling = mLabelProvider->
usedAttributes( ctx, mTileZoom );
147 for (
auto it = requiredFieldsLabeling.begin(); it != requiredFieldsLabeling.end(); ++it )
149 requiredFields[it.key()].unite( it.value() );
153 for (
auto it = requiredFields.constBegin(); it != requiredFields.constEnd(); ++it )
156 mRequiredLayers = mRenderer->requiredLayers( ctx, mTileZoom );
158 if ( mLabelProvider )
160 mLabelProvider->
setFields( mPerLayerFields );
161 QSet<QString> attributeNames;
162 if ( !mLabelProvider->
prepare( ctx, attributeNames ) )
165 mLabelProvider =
nullptr;
169 mRequiredLayers.unite( mLabelProvider->
requiredLayers( ctx, mTileZoom ) );
180 decodeAndDrawTile( rawTile );
187 asyncLoader->downloadBlocking();
190 mRenderer->stopRender( ctx );
192 QgsDebugMsgLevel( QStringLiteral(
"Total time for decoding: %1" ).arg( mTotalDecodeTime / 1000. ), 2 );
193 QgsDebugMsgLevel( QStringLiteral(
"Drawing time: %1" ).arg( mTotalDrawTime / 1000. ), 2 );
194 QgsDebugMsgLevel( QStringLiteral(
"Total time: %1" ).arg( tTotal.elapsed() / 1000. ), 2 );
227 tile.setFields( mPerLayerFields );
228 tile.setFeatures( decoder.
layerFeatures( mPerLayerFields, ct, &mRequiredLayers ) );
240 mTotalDecodeTime += tLoad.elapsed();
251 ctx.
painter()->setClipRegion( QRegion( tile.tilePolygon() ), Qt::IntersectClip );
256 mRenderer->renderTile( tile, ctx );
257 mTotalDrawTime += tDraw.elapsed();
259 if ( mLabelProvider )
262 if ( mDrawTileBoundaries )
265 ctx.
painter()->setClipping(
false );
270 ctx.
painter()->drawPolygon( tile.tilePolygon() );
@ UseAdvancedEffects
Enable layer opacity and blending effects.
Custom exception class for Coordinate Reference System related exceptions.
Class for storing the component parts of a RDBMS data source URI (e.g.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
The QgsLabelingEngine class provides map labeling functionality.
void removeProvider(QgsAbstractLabelProvider *provider)
Remove provider if the provider's initialization failed. Provider instance is deleted.
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.
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...
Base class for utility classes that encapsulate information necessary for rendering of map layers.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
QString source() const
Returns the source for the layer.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
double rendererScale() const
Returns the renderer map scale.
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
bool testFlag(Qgis::RenderContextFlag flag) const
Check whether a particular flag is enabled.
QgsLabelingEngine * labelingEngine() const
Gets access to new labeling engine (may be nullptr)
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.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
Scoped object for saving and restoring a QPainter object's state.
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent)
Returns tile range that fully covers the given extent.
static QgsTileMatrix fromWebMercator(int zoomLevel)
Returns a tile matrix for the usual web mercator.
int endColumn() const
Returns index of the last column in the range.
int endRow() const
Returns index of the last row in the range.
int startRow() const
Returns index of the first row in the range.
int startColumn() const
Returns index of the first column in the range.
bool isValid() const
Returns whether the range is valid (when all row/column numbers are not negative)
QString toString() const
Returns tile coordinates in a formatted string.
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
virtual void setFields(const QMap< QString, QgsFields > &perLayerFields)=0
Sets fields for each sub-layer.
virtual QSet< QString > requiredLayers(QgsRenderContext &context, int tileZoom) const
Returns a list of the layers required for labeling.
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.
virtual void registerTileFeatures(const QgsVectorTileRendererData &tile, QgsRenderContext &context)=0
Registers label features for given tile to the labeling engine.
virtual QgsVectorTileLabelProvider * provider(QgsVectorTileLayer *layer) const SIP_SKIP
Factory for label provider implementation.
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
QgsVectorTileLayerRenderer(QgsVectorTileLayer *layer, QgsRenderContext &context)
Creates the renderer. Always called from main thread, should copy whatever necessary from the layer.
virtual bool render() override
Do the rendering (based on data stored in the class).
Implements a map layer that is dedicated to rendering of vector tiles.
QgsVectorTileLabeling * labeling() const
Returns currently assigned labeling.
The loader class takes care of loading raw vector tile data from a tile source.
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 QString &referer)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
This class is responsible for decoding raw tile data written with Mapbox Vector Tiles encoding.
QgsVectorTileFeatures layerFeatures(const QMap< QString, QgsFields > &perLayerFields, const QgsCoordinateTransform &ct, const QSet< QString > *layerSubset=nullptr) const
Returns decoded features grouped by sub-layers.
bool decode(QgsTileXYZ tileID, const QByteArray &rawTileData)
Tries to decode raw tile data, returns true on success.
Keeps track of raw tile data that need to be decoded.
QByteArray data
Raw tile data.
QgsTileXYZ id
Tile position in tile matrix set.
Contains decoded features of a single vector tile and any other data necessary for rendering of it.
static int scaleToZoomLevel(double mapScale, int sourceMinZoom, int sourceMaxZoom)
Finds best fitting zoom level (assuming GoogleCRS84Quad tile matrix set) given map scale denominator ...
static double scaleToZoom(double mapScale)
Finds zoom level (assuming GoogleCRS84Quad tile matrix set) given map scale denominator.
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.
static QgsFields makeQgisFields(QSet< QString > flds)
Returns QgsFields instance based on the set of field names.
@ VectorTileLayer
Added in 3.14.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
#define QgsDebugMsgLevel(str, level)