QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
18 #include <QElapsedTimer>
35 , mSourceType( layer->sourceType() )
36 , mSourcePath( layer->sourcePath() )
37 , mSourceMinZoom( layer->sourceMinZoom() )
38 , mSourceMaxZoom( layer->sourceMaxZoom() )
39 , mRenderer( layer->renderer()->clone() )
40 , mDrawTileBoundaries( layer->isTileBorderRenderingEnabled() )
51 engine->addProvider( mLabelProvider );
72 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles zoom level: %1" ).arg( mTileZoom ), 2 );
77 QgsDebugMsgLevel( QStringLiteral(
"Vector tiles range X: %1 - %2 Y: %3 - %4" )
90 bool isAsync = ( mSourceType == QStringLiteral(
"xyz" ) );
92 std::unique_ptr<QgsVectorTileLoader> asyncLoader;
93 QList<QgsVectorTileRawData> rawTiles;
99 QgsDebugMsgLevel( QStringLiteral(
"Tile fetching time: %1" ).arg( tFetch.elapsed() / 1000. ), 2 );
100 QgsDebugMsgLevel( QStringLiteral(
"Fetched tiles: %1" ).arg( rawTiles.count() ), 2 );
104 asyncLoader.reset(
new QgsVectorTileLoader( mSourcePath, mTileMatrix, mTileRange, viewCenter, mFeedback.get() ) );
107 QgsDebugMsgLevel( QStringLiteral(
"Got tile asynchronously: " ) + rawTile.id.toString(), 2 );
108 if ( !rawTile.data.isEmpty() )
109 decodeAndDrawTile( rawTile );
118 scope->
setVariable(
"zoom_level", mTileZoom,
true );
121 mRenderer->startRender( *
renderContext(), mTileZoom, mTileRange );
123 QMap<QString, QSet<QString> > requiredFields = mRenderer->usedAttributes( ctx );
125 if ( mLabelProvider )
127 QMap<QString, QSet<QString> > requiredFieldsLabeling = mLabelProvider->
usedAttributes( ctx, mTileZoom );
128 for ( QString layerName : requiredFieldsLabeling.keys() )
130 requiredFields[layerName].unite( requiredFieldsLabeling[layerName] );
134 QMap<QString, QgsFields> perLayerFields;
135 for ( QString layerName : requiredFields.keys() )
138 if ( mLabelProvider )
140 mLabelProvider->
setFields( mPerLayerFields );
141 QSet<QString> attributeNames;
142 if ( !mLabelProvider->
prepare( ctx, attributeNames ) )
145 mLabelProvider =
nullptr;
156 decodeAndDrawTile( rawTile );
163 asyncLoader->downloadBlocking();
166 mRenderer->stopRender( ctx );
168 ctx.
painter()->setClipping(
false );
170 QgsDebugMsgLevel( QStringLiteral(
"Total time for decoding: %1" ).arg( mTotalDecodeTime / 1000. ), 2 );
171 QgsDebugMsgLevel( QStringLiteral(
"Drawing time: %1" ).arg( mTotalDrawTime / 1000. ), 2 );
172 QgsDebugMsgLevel( QStringLiteral(
"Total time: %1" ).arg( tTotal.elapsed() / 1000. ), 2 );
200 tile.setFields( mPerLayerFields );
201 tile.setFeatures( decoder.
layerFeatures( mPerLayerFields, ct ) );
204 mTotalDecodeTime += tLoad.elapsed();
213 ctx.
painter()->setClipRegion( QRegion( tile.tilePolygon() ) );
218 mRenderer->renderTile( tile, ctx );
219 mTotalDrawTime += tDraw.elapsed();
221 if ( mLabelProvider )
224 if ( mDrawTileBoundaries )
226 ctx.
painter()->setClipping(
false );
231 ctx.
painter()->drawPolygon( tile.tilePolygon() );
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.
static QgsFields makeQgisFields(QSet< QString > flds)
Returns QgsFields instance based on the set of field names.
#define QgsDebugMsgLevel(str, level)
void removeProvider(QgsAbstractLabelProvider *provider)
Remove provider if the provider's initialization failed. Provider instance is deleted.
QgsVectorTileLayerRenderer(QgsVectorTileLayer *layer, QgsRenderContext &context)
Creates the renderer. Always called from main thread, should copy whatever necessary from the layer.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
virtual QgsVectorTileLabelProvider * provider(QgsVectorTileLayer *layer) const SIP_SKIP
Factory for label provider implementation.
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.
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...
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.
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QString &sourceType, const QString &sourcePath, const QgsTileMatrix &tileMatrix, const QPointF &viewCenter, const QgsTileRange &range)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
QString toString() const
Returns tile coordinates in a formatted string.
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.
static int scaleToZoomLevel(double mapScale, int sourceMinZoom, int sourceMaxZoom)
Finds best fitting zoom level (assuming GoogleCRS84Quad tile matrix set) given map scale denominator ...
Single scope for storing variables and functions for use within a QgsExpressionContext....
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)
int startRow() const
Returns index of the first row in the range.
QgsPointXY center() const
Returns the center point of the rectangle.
virtual void registerTileFeatures(const QgsVectorTileRendererData &tile, QgsRenderContext &context)=0
Registers label features for given tile to the labeling engine.
QgsVectorTileFeatures layerFeatures(const QMap< QString, QgsFields > &perLayerFields, const QgsCoordinateTransform &ct) const
Returns decoded features grouped by sub-layers. It can only be called after a successful decode()
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.
QgsTileXYZ id
Tile position in tile matrix set.
static QgsTileMatrix fromWebMercator(int mZoomLevel)
Returns a tile matrix for the usual web mercator.
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
QPainter * painter()
Returns the destination QPainter for the render operation.
double rendererScale() const
Returns the renderer map scale.
bool decode(QgsTileXYZ tileID, const QByteArray &rawTileData)
Tries to decode raw tile data, returns true on success.
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent)
Returns tile range that fully covers the given extent.
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)
QByteArray data
Raw tile data.