QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsvectortilelayerrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortilelayerrenderer.cpp
3 --------------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <memory>
19
20#include "qgsapplication.h"
22#include "qgsfeedback.h"
23#include "qgslabelingengine.h"
24#include "qgslogger.h"
25#include "qgsmapclippingutils.h"
26#include "qgsrendercontext.h"
27#include "qgsruntimeprofiler.h"
28#include "qgstextrenderer.h"
29#include "qgsthreadingutils.h"
32#include "qgsvectortilelayer.h"
33#include "qgsvectortileloader.h"
35#include "qgsvectortileutils.h"
36
37#include <QElapsedTimer>
38#include <QString>
39#include <QThread>
40
41using namespace Qt::StringLiterals;
42
44 : QgsMapLayerRenderer( layer->id(), &context )
45 , mLayerName( layer->name() )
46 , mDataProvider( qgis::down_cast< const QgsVectorTileDataProvider* >( layer->dataProvider() )->clone() )
47 , mRenderer( layer->renderer()->clone() )
48 , mLayerBlendMode( layer->blendMode() )
49 , mDrawTileBoundaries( layer->isTileBorderRenderingEnabled() )
50 , mLabelsEnabled( layer->labelsEnabled() )
51 , mFeedback( new QgsFeedback )
52 , mSelectedFeatures( layer->selectedFeatures() )
53 , mLayerOpacity( layer->opacity() )
54 , mTileMatrixSet( layer->tileMatrixSet() )
55 , mEnableProfile( context.flags() & Qgis::RenderContextFlag::RecordProfile )
56{
57 QElapsedTimer timer;
58 timer.start();
59
60 if ( QgsLabelingEngine *engine = context.labelingEngine() )
61 {
62 if ( layer->labelsEnabled() )
63 {
64 mLabelProvider = layer->labeling()->provider( layer );
65 if ( mLabelProvider )
66 {
67 engine->addProvider( mLabelProvider );
68 }
69 }
70 }
71
72 mClippingRegions = QgsMapClippingUtils::collectClippingRegionsForLayer( *renderContext(), layer );
73
74 mDataProvider->moveToThread( nullptr );
75
76 mPreparationTime = timer.elapsed();
77}
78
80
82{
83 QgsScopedThreadName threadName( u"render:%1"_s.arg( mLayerName ) );
84
85 std::unique_ptr< QgsScopedRuntimeProfile > profile;
86 if ( mEnableProfile )
87 {
88 profile = std::make_unique< QgsScopedRuntimeProfile >( mLayerName, u"rendering"_s, layerId() );
89 if ( mPreparationTime > 0 )
90 QgsApplication::profiler()->record( QObject::tr( "Create renderer" ), mPreparationTime / 1000.0, u"rendering"_s );
91 }
92
94
95 if ( ctx.renderingStopped() )
96 return false;
97
98 std::unique_ptr< QgsScopedRuntimeProfile > preparingProfile;
99 if ( mEnableProfile )
100 {
101 preparingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Preparing render" ), u"rendering"_s );
102 }
103
104 mDataProvider->moveToThread( QThread::currentThread() );
105
106 const QgsScopedQPainterState painterState( ctx.painter() );
107
108 if ( !mClippingRegions.empty() )
109 {
110 bool needsPainterClipPath = false;
111 const QPainterPath path = QgsMapClippingUtils::calculatePainterClipRegion( mClippingRegions, *renderContext(), Qgis::LayerType::VectorTile, needsPainterClipPath );
112 if ( needsPainterClipPath )
113 renderContext()->painter()->setClipPath( path, Qt::IntersectClip );
114 }
115
116 QElapsedTimer tTotal;
117 tTotal.start();
118
119 const double tileRenderScale = mTileMatrixSet.scaleForRenderContext( ctx );
120
121 QgsRectangle extent = ctx.extent();
122 if ( extent.isMaximal() )
123 {
124 // invalid extent. We don't want to fetch ALL tiles at the zoom level!
125 // This has occurred because the map renderer is being pessimistic and thinks a worst-case
126 // scenario is acceptable when it failed to transform the map extent to the layer's crs.
127 // But while that's permissible eg for a local raster layer, it's entirely inappropriate
128 // for vector tiles!
129 // So let's try and determine a MINIMAL extent for the layer, avoiding the pessimism used
130 // in the generic map renderer code
131 QgsCoordinateTransform layerToMapTransform = ctx.coordinateTransform();
132 layerToMapTransform.setAllowFallbackTransforms( true );
133 layerToMapTransform.setBallparkTransformsAreAppropriate( true );
134
135 QgsRectangle extentInMapCrs = ctx.mapExtent();
136 try
137 {
138 extent = layerToMapTransform.transformBoundingBox( extentInMapCrs, Qgis::TransformDirection::Reverse );
139 }
140 catch ( QgsCsException &cs )
141 {
142 QgsDebugError( u"Could not transform map extent to layer extent -- cannot calculate valid extent for vector tiles, aborting: %1"_s.arg( cs.what() ) );
143 return false;
144 }
145 }
146
147 QgsDebugMsgLevel( u"Vector tiles rendering extent: "_s + extent.toString( -1 ), 2 );
148 QgsDebugMsgLevel( u"Vector tiles map scale 1 : %1"_s.arg( tileRenderScale ), 2 );
149
150 mTileZoomToFetch = mTileMatrixSet.scaleToZoomLevel( tileRenderScale );
151 QgsDebugMsgLevel( u"Vector tiles zoom level: %1"_s.arg( mTileZoomToFetch ), 2 );
152 mTileZoomToRender = mTileMatrixSet.scaleToZoomLevel( tileRenderScale, false );
153 QgsDebugMsgLevel( u"Render zoom level: %1"_s.arg( mTileZoomToRender ), 2 );
154
155 mTileMatrix = mTileMatrixSet.tileMatrix( mTileZoomToFetch );
156
157 mTileRange = mTileMatrix.tileRangeFromExtent( extent );
158 QgsDebugMsgLevel( u"Vector tiles range X: %1 - %2 Y: %3 - %4 (%5 tiles total)"_s
159 .arg( mTileRange.startColumn() ).arg( mTileRange.endColumn() )
160 .arg( mTileRange.startRow() ).arg( mTileRange.endRow() ).arg( mTileRange.count() ), 2 );
161
162 // view center is used to sort the order of tiles for fetching and rendering
163 const QPointF viewCenter = mTileMatrix.mapToTileCoordinates( extent.center() );
164
165 if ( !mTileRange.isValid() )
166 {
167 QgsDebugMsgLevel( u"Vector tiles - outside of range"_s, 2 );
168 return true; // nothing to do
169 }
170
171 preparingProfile.reset();
172 std::unique_ptr< QgsScopedRuntimeProfile > renderingProfile;
173 if ( mEnableProfile )
174 {
175 renderingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Rendering" ), u"rendering"_s );
176 }
177
178 std::unique_ptr<QgsVectorTileLoader> asyncLoader;
179 QList<QgsVectorTileRawData> rawTiles;
180 if ( !mDataProvider->supportsAsync() )
181 {
182 QElapsedTimer tFetch;
183 tFetch.start();
184 rawTiles = QgsVectorTileLoader::blockingFetchTileRawData( mDataProvider.get(), mTileMatrixSet, viewCenter, mTileRange, mTileZoomToFetch, mFeedback.get() );
185 QgsDebugMsgLevel( u"Tile fetching time: %1"_s.arg( tFetch.elapsed() / 1000. ), 2 );
186 QgsDebugMsgLevel( u"Fetched tiles: %1"_s.arg( rawTiles.count() ), 2 );
187 }
188 else
189 {
190 asyncLoader = std::make_unique<QgsVectorTileLoader>( mDataProvider.get(), mTileMatrixSet, mTileRange, mTileZoomToFetch, viewCenter, mFeedback.get(), renderContext()->rendererUsage() );
191 QObject::connect( asyncLoader.get(), &QgsVectorTileLoader::tileRequestFinished, asyncLoader.get(), [this]( const QgsVectorTileRawData & rawTile )
192 {
193 QgsDebugMsgLevel( u"Got tile asynchronously: "_s + rawTile.id.toString(), 2 );
194 if ( !rawTile.data.isEmpty() )
195 decodeAndDrawTile( rawTile );
196 } );
197 }
198
199 if ( ctx.renderingStopped() )
200 return false;
201
202 // add @zoom_level and @vector_tile_zoom variables which can be used in styling
203 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Tiles" ) ); // will be deleted by popper
204 scope->setVariable( u"zoom_level"_s, mTileZoomToRender, true );
205 scope->setVariable( u"vector_tile_zoom"_s, mTileMatrixSet.scaleToZoom( tileRenderScale ), true );
206 const QgsExpressionContextScopePopper popper( ctx.expressionContext(), scope );
207
208 mRenderer->startRender( *renderContext(), mTileZoomToRender, mTileRange );
209
210 // Draw background style if present
211 mRenderer->renderBackground( ctx );
212
213 QMap<QString, QSet<QString> > requiredFields = mRenderer->usedAttributes( ctx );
214
215 if ( mLabelProvider )
216 {
217 const QMap<QString, QSet<QString> > requiredFieldsLabeling = mLabelProvider->usedAttributes( ctx, mTileZoomToRender );
218 for ( auto it = requiredFieldsLabeling.begin(); it != requiredFieldsLabeling.end(); ++it )
219 {
220 requiredFields[it.key()].unite( it.value() );
221 }
222 }
223
224 for ( auto it = requiredFields.constBegin(); it != requiredFields.constEnd(); ++it )
225 mPerLayerFields[it.key()] = QgsVectorTileUtils::makeQgisFields( it.value() );
226
227 mRequiredLayers = mRenderer->requiredLayers( ctx, mTileZoomToRender );
228
229 if ( mLabelProvider )
230 {
231 mLabelProvider->setFields( mPerLayerFields );
232 QSet<QString> attributeNames; // we don't need this - already got referenced columns in provider constructor
233 if ( !mLabelProvider->prepare( ctx, attributeNames ) )
234 {
235 ctx.labelingEngine()->removeProvider( mLabelProvider );
236 mLabelProvider = nullptr; // provider is deleted by the engine
237 }
238 else
239 {
240 mRequiredLayers.unite( mLabelProvider->requiredLayers( ctx, mTileZoomToRender ) );
241 }
242 }
243
244 if ( !mDataProvider->supportsAsync() )
245 {
246 for ( const QgsVectorTileRawData &rawTile : std::as_const( rawTiles ) )
247 {
248 if ( ctx.renderingStopped() )
249 break;
250
251 decodeAndDrawTile( rawTile );
252 }
253 }
254 else
255 {
256 // Block until tiles are fetched and rendered. If the rendering gets canceled at some point,
257 // the async loader will catch the signal, abort requests and return from downloadBlocking()
258 asyncLoader->downloadBlocking();
259 if ( !asyncLoader->error().isEmpty() )
260 mErrors.append( asyncLoader->error() );
261 }
262
263 // Register labels features when all tiles are fetched to ensure consistent labeling
264 if ( mLabelProvider )
265 {
266 for ( const auto &tile : mTileDataMap )
267 {
268 mLabelProvider->registerTileFeatures( tile, ctx );
269 }
270 }
271
273 mRenderer->renderSelectedFeatures( mSelectedFeatures, ctx );
274
275 mRenderer->stopRender( ctx );
276
277 QgsDebugMsgLevel( u"Total time for decoding: %1"_s.arg( mTotalDecodeTime / 1000. ), 2 );
278 QgsDebugMsgLevel( u"Drawing time: %1"_s.arg( mTotalDrawTime / 1000. ), 2 );
279 QgsDebugMsgLevel( u"Total time: %1"_s.arg( tTotal.elapsed() / 1000. ), 2 );
280
281 return !ctx.renderingStopped();
282}
283
285{
286 switch ( renderContext()->rasterizedRenderingPolicy() )
287 {
290 break;
291
293 return false;
294 }
295
296 if ( !qgsDoubleNear( mLayerOpacity, 1.0 ) )
297 return true;
298
299 if ( mLayerBlendMode != QPainter::CompositionMode_SourceOver )
300 return true;
301
302 return false;
303}
304
305void QgsVectorTileLayerRenderer::decodeAndDrawTile( const QgsVectorTileRawData &rawTile )
306{
308
309 QgsDebugMsgLevel( u"Drawing tile "_s + rawTile.id.toString(), 2 );
310
311 QElapsedTimer tLoad;
312 tLoad.start();
313
314 // currently only MVT encoding supported
315 QgsVectorTileMVTDecoder decoder( mTileMatrixSet );
316 if ( !decoder.decode( rawTile ) )
317 {
318 QgsDebugMsgLevel( u"Failed to parse raw tile data! "_s + rawTile.id.toString(), 2 );
319 return;
320 }
321
322 if ( ctx.renderingStopped() )
323 return;
324
325 const QgsCoordinateTransform ct = ctx.coordinateTransform();
326
327 QgsVectorTileRendererData tile( rawTile.id );
328 tile.setRenderZoomLevel( mTileZoomToRender );
329
330 tile.setFields( mPerLayerFields );
331 tile.setFeatures( decoder.layerFeatures( mPerLayerFields, ct, &mRequiredLayers ) );
332
333 try
334 {
335 tile.setTilePolygon( QgsVectorTileUtils::tilePolygon( rawTile.id, ct, mTileMatrixSet.tileMatrix( rawTile.id.zoomLevel() ), ctx.mapToPixel() ) );
336 }
337 catch ( QgsCsException & )
338 {
339 QgsDebugMsgLevel( u"Failed to generate tile polygon "_s + rawTile.id.toString(), 2 );
340 return;
341 }
342
343 mTotalDecodeTime += tLoad.elapsed();
344
345 // calculate tile polygon in screen coordinates
346
347 if ( ctx.renderingStopped() )
348 return;
349
350 {
351 // set up clipping so that rendering does not go behind tile's extent
352 const QgsScopedQPainterState savePainterState( ctx.painter() );
353 // we have to intersect with any existing painter clip regions, or we risk overwriting valid clip
354 // regions setup outside of the vector tile renderer (e.g. layout map clip region)
355 ctx.painter()->setClipRegion( QRegion( tile.tilePolygon() ), Qt::IntersectClip );
356
357 QElapsedTimer tDraw;
358 tDraw.start();
359
360 mRenderer->renderTile( tile, ctx );
361 mTotalDrawTime += tDraw.elapsed();
362 }
363
364 // Store tile for later use
365 if ( mLabelProvider )
366 mTileDataMap.insert( tile.id().toString(), tile );
367
368 if ( mDrawTileBoundaries )
369 {
370 const QgsScopedQPainterState savePainterState( ctx.painter() );
371 ctx.painter()->setClipping( false );
372
373 QPen pen( Qt::red );
374 pen.setWidth( 3 );
375 QBrush brush( QColor( 255, 0, 0, 40 ), Qt::BrushStyle::Dense3Pattern );
376
377 ctx.painter()->setPen( pen );
378 ctx.painter()->setBrush( brush );
379 ctx.painter()->drawPolygon( tile.tilePolygon() );
380#if 1
381 QgsTextFormat format;
382 format.setColor( QColor( 255, 0, 0 ) );
383 format.buffer().setEnabled( true );
384
385 QgsTextRenderer::drawText( QRectF( QPoint( 0, 0 ), ctx.outputSize() ).intersected( tile.tilePolygon().boundingRect() ),
386 0, Qgis::TextHorizontalAlignment::Center, { tile.id().toString() },
388#endif
389 }
390}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2762
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2763
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2764
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:198
@ DrawSelection
Whether vector selections should be shown in the rendered map.
Definition qgis.h:2811
@ VerticalCenter
Center align.
Definition qgis.h:3021
@ Center
Center align.
Definition qgis.h:3002
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2731
static QgsRuntimeProfiler * profiler()
Returns the application runtime profiler.
Handles coordinate transforms between two coordinate systems.
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
void setAllowFallbackTransforms(bool allowed)
Sets whether "ballpark" fallback transformations can be used in the case that the specified coordinat...
Custom exception class for Coordinate Reference System related exceptions.
QString what() const
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.
Definition qgsfeedback.h:44
Provides map labeling functionality.
void removeProvider(QgsAbstractLabelProvider *provider)
Remove provider if the provider's initialization failed. Provider instance is deleted.
static QPainterPath calculatePainterClipRegion(const QList< QgsMapClippingRegion > &regions, const QgsRenderContext &context, Qgis::LayerType layerType, bool &shouldClip)
Returns a QPainterPath representing the intersection of clipping regions from context which should be...
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.
QString layerId() const
Gets access to the ID of the layer rendered by this class.
virtual Qgis::MapLayerRendererFlags flags() const
Returns flags which control how the map layer rendering behaves.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
QgsMapLayerRenderer(const QString &layerID, QgsRenderContext *context=nullptr)
Constructor for QgsMapLayerRenderer, with the associated layerID and render context.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
bool isMaximal() const
Test if the rectangle is the maximal possible rectangle.
QgsPointXY center
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
QSize outputSize() const
Returns the size of the resulting rendered image, in pixels.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
QgsLabelingEngine * labelingEngine() const
Gets access to new labeling engine (may be nullptr).
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Qgis::RenderContextFlags flags() const
Returns combination of flags used for rendering.
void record(const QString &name, double time, const QString &group="startup", const QString &id=QString())
Manually adds a profile event with the given name and total time (in seconds).
Scoped object for saving and restoring a QPainter object's state.
Scoped object for setting the current thread name.
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
QString toString() const
Returns tile coordinates in a formatted string.
Definition qgstiles.h:59
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:56
Base class for vector tile layer data providers.
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
~QgsVectorTileLayerRenderer() override
QgsVectorTileLayerRenderer(QgsVectorTileLayer *layer, QgsRenderContext &context)
Creates the renderer. Always called from main thread, should copy whatever necessary from the layer.
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.
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 QgsVectorTileDataProvider *provider, const QgsTileMatrixSet &tileMatrixSet, const QPointF &viewCenter, const QgsTileRange &range, int zoomLevel, QgsFeedback *feedback=nullptr, Qgis::RendererUsage usage=Qgis::RendererUsage::Unknown)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
Responsible for decoding raw tile data written with Mapbox Vector Tiles encoding.
Keeps track of raw tile data from one or more sources that need to be decoded.
QgsTileXYZ id
Tile position in tile matrix set.
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(const QSet< QString > &flds)
Returns QgsFields instance based on the set of field names.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59