QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 );
159 u"Vector tiles range X: %1 - %2 Y: %3 - %4 (%5 tiles total)"_s.arg( mTileRange.startColumn() )
160 .arg( mTileRange.endColumn() )
161 .arg( mTileRange.startRow() )
162 .arg( mTileRange.endRow() )
163 .arg( mTileRange.count() ),
164 2
165 );
166
167 // view center is used to sort the order of tiles for fetching and rendering
168 const QPointF viewCenter = mTileMatrix.mapToTileCoordinates( extent.center() );
169
170 if ( !mTileRange.isValid() )
171 {
172 QgsDebugMsgLevel( u"Vector tiles - outside of range"_s, 2 );
173 return true; // nothing to do
174 }
175
176 preparingProfile.reset();
177 std::unique_ptr< QgsScopedRuntimeProfile > renderingProfile;
178 if ( mEnableProfile )
179 {
180 renderingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Rendering" ), u"rendering"_s );
181 }
182
183 std::unique_ptr<QgsVectorTileLoader> asyncLoader;
184 QList<QgsVectorTileRawData> rawTiles;
185 if ( !mDataProvider->supportsAsync() )
186 {
187 QElapsedTimer tFetch;
188 tFetch.start();
189 rawTiles = QgsVectorTileLoader::blockingFetchTileRawData( mDataProvider.get(), mTileMatrixSet, viewCenter, mTileRange, mTileZoomToFetch, mFeedback.get() );
190 QgsDebugMsgLevel( u"Tile fetching time: %1"_s.arg( tFetch.elapsed() / 1000. ), 2 );
191 QgsDebugMsgLevel( u"Fetched tiles: %1"_s.arg( rawTiles.count() ), 2 );
192 }
193 else
194 {
195 asyncLoader = std::make_unique<QgsVectorTileLoader>( mDataProvider.get(), mTileMatrixSet, mTileRange, mTileZoomToFetch, viewCenter, mFeedback.get(), renderContext()->rendererUsage() );
196 QObject::connect( asyncLoader.get(), &QgsVectorTileLoader::tileRequestFinished, asyncLoader.get(), [this]( const QgsVectorTileRawData &rawTile ) {
197 QgsDebugMsgLevel( u"Got tile asynchronously: "_s + rawTile.id.toString(), 2 );
198 if ( !rawTile.data.isEmpty() )
199 decodeAndDrawTile( rawTile );
200 } );
201 }
202
203 if ( ctx.renderingStopped() )
204 return false;
205
206 // add @zoom_level and @vector_tile_zoom variables which can be used in styling
207 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Tiles" ) ); // will be deleted by popper
208 scope->setVariable( u"zoom_level"_s, mTileZoomToRender, true );
209 scope->setVariable( u"vector_tile_zoom"_s, mTileMatrixSet.scaleToZoom( tileRenderScale ), true );
210 const QgsExpressionContextScopePopper popper( ctx.expressionContext(), scope );
211
212 mRenderer->startRender( *renderContext(), mTileZoomToRender, mTileRange );
213
214 // Draw background style if present
215 mRenderer->renderBackground( ctx );
216
217 QMap<QString, QSet<QString> > requiredFields = mRenderer->usedAttributes( ctx );
218
219 if ( mLabelProvider )
220 {
221 const QMap<QString, QSet<QString> > requiredFieldsLabeling = mLabelProvider->usedAttributes( ctx, mTileZoomToRender );
222 for ( auto it = requiredFieldsLabeling.begin(); it != requiredFieldsLabeling.end(); ++it )
223 {
224 requiredFields[it.key()].unite( it.value() );
225 }
226 }
227
228 for ( auto it = requiredFields.constBegin(); it != requiredFields.constEnd(); ++it )
229 mPerLayerFields[it.key()] = QgsVectorTileUtils::makeQgisFields( it.value() );
230
231 mRequiredLayers = mRenderer->requiredLayers( ctx, mTileZoomToRender );
232
233 if ( mLabelProvider )
234 {
235 mLabelProvider->setFields( mPerLayerFields );
236 QSet<QString> attributeNames; // we don't need this - already got referenced columns in provider constructor
237 if ( !mLabelProvider->prepare( ctx, attributeNames ) )
238 {
239 ctx.labelingEngine()->removeProvider( mLabelProvider );
240 mLabelProvider = nullptr; // provider is deleted by the engine
241 }
242 else
243 {
244 mRequiredLayers.unite( mLabelProvider->requiredLayers( ctx, mTileZoomToRender ) );
245 }
246 }
247
248 if ( !mDataProvider->supportsAsync() )
249 {
250 for ( const QgsVectorTileRawData &rawTile : std::as_const( rawTiles ) )
251 {
252 if ( ctx.renderingStopped() )
253 break;
254
255 decodeAndDrawTile( rawTile );
256 }
257 }
258 else
259 {
260 // Block until tiles are fetched and rendered. If the rendering gets canceled at some point,
261 // the async loader will catch the signal, abort requests and return from downloadBlocking()
262 asyncLoader->downloadBlocking();
263 if ( !asyncLoader->error().isEmpty() )
264 mErrors.append( asyncLoader->error() );
265 }
266
267 // Register labels features when all tiles are fetched to ensure consistent labeling
268 if ( mLabelProvider )
269 {
270 for ( const auto &tile : mTileDataMap )
271 {
272 mLabelProvider->registerTileFeatures( tile, ctx );
273 }
274 }
275
277 mRenderer->renderSelectedFeatures( mSelectedFeatures, ctx );
278
279 mRenderer->stopRender( ctx );
280
281 QgsDebugMsgLevel( u"Total time for decoding: %1"_s.arg( mTotalDecodeTime / 1000. ), 2 );
282 QgsDebugMsgLevel( u"Drawing time: %1"_s.arg( mTotalDrawTime / 1000. ), 2 );
283 QgsDebugMsgLevel( u"Total time: %1"_s.arg( tTotal.elapsed() / 1000. ), 2 );
284
285 return !ctx.renderingStopped();
286}
287
289{
290 switch ( renderContext()->rasterizedRenderingPolicy() )
291 {
294 break;
295
297 return false;
298 }
299
300 if ( !qgsDoubleNear( mLayerOpacity, 1.0 ) )
301 return true;
302
303 if ( mLayerBlendMode != QPainter::CompositionMode_SourceOver )
304 return true;
305
306 return false;
307}
308
309void QgsVectorTileLayerRenderer::decodeAndDrawTile( const QgsVectorTileRawData &rawTile )
310{
312
313 QgsDebugMsgLevel( u"Drawing tile "_s + rawTile.id.toString(), 2 );
314
315 QElapsedTimer tLoad;
316 tLoad.start();
317
318 // currently only MVT encoding supported
319 QgsVectorTileMVTDecoder decoder( mTileMatrixSet );
320 if ( !decoder.decode( rawTile ) )
321 {
322 QgsDebugMsgLevel( u"Failed to parse raw tile data! "_s + rawTile.id.toString(), 2 );
323 return;
324 }
325
326 if ( ctx.renderingStopped() )
327 return;
328
329 const QgsCoordinateTransform ct = ctx.coordinateTransform();
330
331 QgsVectorTileRendererData tile( rawTile.id );
332 tile.setRenderZoomLevel( mTileZoomToRender );
333
334 tile.setFields( mPerLayerFields );
335 tile.setFeatures( decoder.layerFeatures( mPerLayerFields, ct, &mRequiredLayers ) );
336
337 try
338 {
339 tile.setTilePolygon( QgsVectorTileUtils::tilePolygon( rawTile.id, ct, mTileMatrixSet.tileMatrix( rawTile.id.zoomLevel() ), ctx.mapToPixel() ) );
340 }
341 catch ( QgsCsException & )
342 {
343 QgsDebugMsgLevel( u"Failed to generate tile polygon "_s + rawTile.id.toString(), 2 );
344 return;
345 }
346
347 mTotalDecodeTime += tLoad.elapsed();
348
349 // calculate tile polygon in screen coordinates
350
351 if ( ctx.renderingStopped() )
352 return;
353
354 {
355 // set up clipping so that rendering does not go behind tile's extent
356 const QgsScopedQPainterState savePainterState( ctx.painter() );
357 // we have to intersect with any existing painter clip regions, or we risk overwriting valid clip
358 // regions setup outside of the vector tile renderer (e.g. layout map clip region)
359 ctx.painter()->setClipRegion( QRegion( tile.tilePolygon() ), Qt::IntersectClip );
360
361 QElapsedTimer tDraw;
362 tDraw.start();
363
364 mRenderer->renderTile( tile, ctx );
365 mTotalDrawTime += tDraw.elapsed();
366 }
367
368 // Store tile for later use
369 if ( mLabelProvider )
370 mTileDataMap.insert( tile.id().toString(), tile );
371
372 if ( mDrawTileBoundaries )
373 {
374 const QgsScopedQPainterState savePainterState( ctx.painter() );
375 ctx.painter()->setClipping( false );
376
377 QPen pen( Qt::red );
378 pen.setWidth( 3 );
379 QBrush brush( QColor( 255, 0, 0, 40 ), Qt::BrushStyle::Dense3Pattern );
380
381 ctx.painter()->setPen( pen );
382 ctx.painter()->setBrush( brush );
383 ctx.painter()->drawPolygon( tile.tilePolygon() );
384#if 1
385 QgsTextFormat format;
386 format.setColor( QColor( 255, 0, 0 ) );
387 format.buffer().setEnabled( true );
388
390 drawText( QRectF( QPoint( 0, 0 ), ctx.outputSize() ).intersected( tile.tilePolygon().boundingRect() ), 0, Qgis::TextHorizontalAlignment::Center, { tile.id().toString() }, ctx, format, true, Qgis::TextVerticalAlignment::VerticalCenter );
391#endif
392 }
393}
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2799
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2800
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2801
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:211
@ DrawSelection
Whether vector selections should be shown in the rendered map.
Definition qgis.h:2850
@ VerticalCenter
Center align.
Definition qgis.h:3064
@ Center
Center align.
Definition qgis.h:3045
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
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 rounded to the spec...
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:60
int zoomLevel() const
Returns tile's zoom level (Z).
Definition qgstiles.h:57
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:6975
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59