QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsvectortilebasicrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectortilebasicrenderer.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 "qgsapplication.h"
19 #include "qgscolorschemeregistry.h"
21 #include "qgsfillsymbollayer.h"
22 #include "qgslinesymbollayer.h"
23 #include "qgsmarkersymbollayer.h"
24 #include "qgssymbollayerutils.h"
25 #include "qgsvectortileutils.h"
26 #include "qgsfillsymbol.h"
27 #include "qgslinesymbol.h"
28 #include "qgsmarkersymbol.h"
29 
31  : mStyleName( stName )
32  , mLayerName( laName )
33  , mGeometryType( geomType )
34 {
35 }
36 
38 {
39  operator=( other );
40 }
41 
43 {
44  mStyleName = other.mStyleName;
45  mLayerName = other.mLayerName;
46  mGeometryType = other.mGeometryType;
47  mSymbol.reset( other.mSymbol ? other.mSymbol->clone() : nullptr );
48  mEnabled = other.mEnabled;
49  mExpression = other.mExpression;
50  mMinZoomLevel = other.mMinZoomLevel;
51  mMaxZoomLevel = other.mMaxZoomLevel;
52  return *this;
53 }
54 
56 
58 {
59  mSymbol.reset( sym );
60 }
61 
62 void QgsVectorTileBasicRendererStyle::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
63 {
64  elem.setAttribute( QStringLiteral( "name" ), mStyleName );
65  elem.setAttribute( QStringLiteral( "layer" ), mLayerName );
66  elem.setAttribute( QStringLiteral( "geometry" ), mGeometryType );
67  elem.setAttribute( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
68  elem.setAttribute( QStringLiteral( "expression" ), mExpression );
69  elem.setAttribute( QStringLiteral( "min-zoom" ), mMinZoomLevel );
70  elem.setAttribute( QStringLiteral( "max-zoom" ), mMaxZoomLevel );
71 
72  QDomDocument doc = elem.ownerDocument();
73  QgsSymbolMap symbols;
74  symbols[QStringLiteral( "0" )] = mSymbol.get();
75  QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( symbols, QStringLiteral( "symbols" ), doc, context );
76  elem.appendChild( symbolsElem );
77 }
78 
79 void QgsVectorTileBasicRendererStyle::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
80 {
81  mStyleName = elem.attribute( QStringLiteral( "name" ) );
82  mLayerName = elem.attribute( QStringLiteral( "layer" ) );
83  mGeometryType = static_cast<QgsWkbTypes::GeometryType>( elem.attribute( QStringLiteral( "geometry" ) ).toInt() );
84  mEnabled = elem.attribute( QStringLiteral( "enabled" ) ).toInt();
85  mExpression = elem.attribute( QStringLiteral( "expression" ) );
86  mMinZoomLevel = elem.attribute( QStringLiteral( "min-zoom" ) ).toInt();
87  mMaxZoomLevel = elem.attribute( QStringLiteral( "max-zoom" ) ).toInt();
88 
89  mSymbol.reset();
90  QDomElement symbolsElem = elem.firstChildElement( QStringLiteral( "symbols" ) );
91  if ( !symbolsElem.isNull() )
92  {
93  QgsSymbolMap symbolMap = QgsSymbolLayerUtils::loadSymbols( symbolsElem, context );
94  if ( symbolMap.contains( QStringLiteral( "0" ) ) )
95  {
96  mSymbol.reset( symbolMap.take( QStringLiteral( "0" ) ) );
97  }
98  }
99 }
100 
102 
103 
105 {
106 }
107 
109 {
110  return QStringLiteral( "basic" );
111 }
112 
114 {
116  r->mStyles = mStyles;
117  r->mStyles.detach(); // make a deep copy to make sure symbols get cloned
118  return r;
119 }
120 
121 void QgsVectorTileBasicRenderer::startRender( QgsRenderContext &context, int tileZoom, const QgsTileRange &tileRange )
122 {
123  Q_UNUSED( context )
124  Q_UNUSED( tileRange )
125  // figure out required fields for different layers
126  for ( const QgsVectorTileBasicRendererStyle &layerStyle : std::as_const( mStyles ) )
127  {
128  if ( layerStyle.isActive( tileZoom ) )
129  {
130  if ( !layerStyle.filterExpression().isEmpty() )
131  {
132  QgsExpression expr( layerStyle.filterExpression() );
133  mRequiredFields[layerStyle.layerName()].unite( expr.referencedColumns() );
134  }
135  if ( auto *lSymbol = layerStyle.symbol() )
136  {
137  mRequiredFields[layerStyle.layerName()].unite( lSymbol->usedAttributes( context ) );
138  }
139  }
140  }
141 }
142 
143 QMap<QString, QSet<QString> > QgsVectorTileBasicRenderer::usedAttributes( const QgsRenderContext & )
144 {
145  return mRequiredFields;
146 }
147 
148 QSet<QString> QgsVectorTileBasicRenderer::requiredLayers( QgsRenderContext &, int tileZoom ) const
149 {
150  QSet< QString > res;
151  for ( const QgsVectorTileBasicRendererStyle &layerStyle : std::as_const( mStyles ) )
152  {
153  if ( layerStyle.isActive( tileZoom ) )
154  {
155  res.insert( layerStyle.layerName() );
156  }
157  }
158  return res;
159 }
160 
162 {
163  Q_UNUSED( context )
164 }
165 
167 {
168  const QgsVectorTileFeatures tileData = tile.features();
169  int zoomLevel = tile.id().zoomLevel();
170 
171  for ( const QgsVectorTileBasicRendererStyle &layerStyle : std::as_const( mStyles ) )
172  {
173  if ( !layerStyle.isActive( zoomLevel ) )
174  continue;
175 
176  QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) ); // will be deleted by popper
177  scope->setFields( tile.fields()[layerStyle.layerName()] );
178  QgsExpressionContextScopePopper popper( context.expressionContext(), scope );
179 
180  QgsExpression filterExpression( layerStyle.filterExpression() );
181  filterExpression.prepare( &context.expressionContext() );
182 
183  QgsSymbol *sym = layerStyle.symbol();
184  sym->startRender( context, QgsFields() );
185  if ( layerStyle.layerName().isEmpty() )
186  {
187  // matching all layers
188  for ( QString layerName : tileData.keys() )
189  {
190  for ( const QgsFeature &f : tileData[layerName] )
191  {
192  scope->setFeature( f );
193  if ( filterExpression.isValid() && !filterExpression.evaluate( &context.expressionContext() ).toBool() )
194  continue;
195 
196  const QgsWkbTypes::GeometryType featureType = QgsWkbTypes::geometryType( f.geometry().wkbType() );
197  if ( featureType == layerStyle.geometryType() )
198  {
199  sym->renderFeature( f, context );
200  }
201  else if ( featureType == QgsWkbTypes::PolygonGeometry && layerStyle.geometryType() == QgsWkbTypes::LineGeometry )
202  {
203  // be tolerant and permit rendering polygons with a line layer style, as some style definitions use this approach
204  // to render the polygon borders only
205  QgsFeature exterior = f;
206  exterior.setGeometry( QgsGeometry( f.geometry().constGet()->boundary() ) );
207  sym->renderFeature( exterior, context );
208  }
209  }
210  }
211  }
212  else if ( tileData.contains( layerStyle.layerName() ) )
213  {
214  // matching one particular layer
215  for ( const QgsFeature &f : tileData[layerStyle.layerName()] )
216  {
217  scope->setFeature( f );
218  if ( filterExpression.isValid() && !filterExpression.evaluate( &context.expressionContext() ).toBool() )
219  continue;
220 
221  const QgsWkbTypes::GeometryType featureType = QgsWkbTypes::geometryType( f.geometry().wkbType() );
222  if ( featureType == layerStyle.geometryType() )
223  {
224  sym->renderFeature( f, context );
225  }
226  else if ( featureType == QgsWkbTypes::PolygonGeometry && layerStyle.geometryType() == QgsWkbTypes::LineGeometry )
227  {
228  // be tolerant and permit rendering polygons with a line layer style, as some style definitions use this approach
229  // to render the polygon borders only
230  QgsFeature exterior = f;
231  exterior.setGeometry( QgsGeometry( f.geometry().constGet()->boundary() ) );
232  sym->renderFeature( exterior, context );
233  }
234  }
235  }
236  sym->stopRender( context );
237  }
238 }
239 
240 void QgsVectorTileBasicRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
241 {
242  QDomDocument doc = elem.ownerDocument();
243  QDomElement elemStyles = doc.createElement( QStringLiteral( "styles" ) );
244  for ( const QgsVectorTileBasicRendererStyle &layerStyle : mStyles )
245  {
246  QDomElement elemStyle = doc.createElement( QStringLiteral( "style" ) );
247  layerStyle.writeXml( elemStyle, context );
248  elemStyles.appendChild( elemStyle );
249  }
250  elem.appendChild( elemStyles );
251 }
252 
253 void QgsVectorTileBasicRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
254 {
255  mStyles.clear();
256 
257  QDomElement elemStyles = elem.firstChildElement( QStringLiteral( "styles" ) );
258  QDomElement elemStyle = elemStyles.firstChildElement( QStringLiteral( "style" ) );
259  while ( !elemStyle.isNull() )
260  {
262  layerStyle.readXml( elemStyle, context );
263  mStyles.append( layerStyle );
264  elemStyle = elemStyle.nextSiblingElement( QStringLiteral( "style" ) );
265  }
266 }
267 
268 void QgsVectorTileBasicRenderer::setStyles( const QList<QgsVectorTileBasicRendererStyle> &styles )
269 {
270  mStyles = styles;
271 }
272 
273 QList<QgsVectorTileBasicRendererStyle> QgsVectorTileBasicRenderer::styles() const
274 {
275  return mStyles;
276 }
277 
278 QList<QgsVectorTileBasicRendererStyle> QgsVectorTileBasicRenderer::simpleStyleWithRandomColors()
279 {
280  QColor polygonFillColor = QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
281  QColor polygonStrokeColor = polygonFillColor;
282  polygonFillColor.setAlpha( 100 );
283  double polygonStrokeWidth = DEFAULT_LINE_WIDTH;
284 
285  QColor lineStrokeColor = QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
286  double lineStrokeWidth = DEFAULT_LINE_WIDTH;
287 
288  QColor pointFillColor = QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
289  QColor pointStrokeColor = pointFillColor;
290  pointFillColor.setAlpha( 100 );
291  double pointSize = DEFAULT_POINT_SIZE;
292 
293  return simpleStyle( polygonFillColor, polygonStrokeColor, polygonStrokeWidth,
294  lineStrokeColor, lineStrokeWidth,
295  pointFillColor, pointStrokeColor, pointSize );
296 }
297 
298 QList<QgsVectorTileBasicRendererStyle> QgsVectorTileBasicRenderer::simpleStyle(
299  const QColor &polygonFillColor, const QColor &polygonStrokeColor, double polygonStrokeWidth,
300  const QColor &lineStrokeColor, double lineStrokeWidth,
301  const QColor &pointFillColor, const QColor &pointStrokeColor, double pointSize )
302 {
303  QgsSimpleFillSymbolLayer *fillSymbolLayer = new QgsSimpleFillSymbolLayer();
304  fillSymbolLayer->setFillColor( polygonFillColor );
305  fillSymbolLayer->setStrokeColor( polygonStrokeColor );
306  fillSymbolLayer->setStrokeWidth( polygonStrokeWidth );
307  QgsFillSymbol *fillSymbol = new QgsFillSymbol( QgsSymbolLayerList() << fillSymbolLayer );
308 
309  QgsSimpleLineSymbolLayer *lineSymbolLayer = new QgsSimpleLineSymbolLayer;
310  lineSymbolLayer->setColor( lineStrokeColor );
311  lineSymbolLayer->setWidth( lineStrokeWidth );
312  QgsLineSymbol *lineSymbol = new QgsLineSymbol( QgsSymbolLayerList() << lineSymbolLayer );
313 
315  markerSymbolLayer->setFillColor( pointFillColor );
316  markerSymbolLayer->setStrokeColor( pointStrokeColor );
317  markerSymbolLayer->setSize( pointSize );
318  QgsMarkerSymbol *markerSymbol = new QgsMarkerSymbol( QgsSymbolLayerList() << markerSymbolLayer );
319 
320  QgsVectorTileBasicRendererStyle st1( QStringLiteral( "Polygons" ), QString(), QgsWkbTypes::PolygonGeometry );
321  st1.setSymbol( fillSymbol );
322 
323  QgsVectorTileBasicRendererStyle st2( QStringLiteral( "Lines" ), QString(), QgsWkbTypes::LineGeometry );
324  st2.setSymbol( lineSymbol );
325 
326  QgsVectorTileBasicRendererStyle st3( QStringLiteral( "Points" ), QString(), QgsWkbTypes::PointGeometry );
327  st3.setSymbol( markerSymbol );
328 
329  QList<QgsVectorTileBasicRendererStyle> lst;
330  lst << st1 << st2 << st3;
331  return lst;
332 }
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
QVariant evaluate()
Evaluate the feature and return the result.
bool isValid() const
Checks if this expression is valid.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:145
Container of fields for a vector layer.
Definition: qgsfields.h:45
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:30
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
virtual void setWidth(double width)
Sets the width of the line symbol layer.
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:30
virtual void setSize(double size)
Sets the symbol size.
A marker symbol type, for rendering Point and MultiPoint geometries.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setStrokeWidth(double strokeWidth)
void setFillColor(const QColor &color) override
Set fill color.
void setStrokeColor(const QColor &strokeColor) override
Set stroke color.
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
Simple marker symbol layer, consisting of a rendered shape with solid fill color and an stroke.
void setFillColor(const QColor &color) override
Set fill color.
void setStrokeColor(const QColor &color) override
Sets the marker's stroke color.
static QgsSymbolMap loadSymbols(QDomElement &element, const QgsReadWriteContext &context)
Reads a collection of symbols from XML and returns them in a map. Caller is responsible for deleting ...
static QDomElement saveSymbols(QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a collection of symbols to XML with specified tagName for the top-level element.
virtual void setColor(const QColor &color)
The fill color.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:516
void renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false, int currentVertexMarkerType=0, double currentVertexMarkerSize=0.0) SIP_THROW(QgsCsException)
Render a feature.
Definition: qgssymbol.cpp:903
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:489
Range of tiles in a tile matrix to be rendered.
Definition: qgstiles.h:66
int zoomLevel() const
Returns tile's zoom level (Z)
Definition: qgstiles.h:46
Definition of map rendering of a subset of vector tile data.
void setSymbol(QgsSymbol *sym)
Sets symbol for rendering. Takes ownership of the symbol.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const
Writes object content to given DOM element.
QgsVectorTileBasicRendererStyle(const QString &stName=QString(), const QString &laName=QString(), QgsWkbTypes::GeometryType geomType=QgsWkbTypes::UnknownGeometry)
Constructs a style object.
QgsVectorTileBasicRendererStyle & operator=(const QgsVectorTileBasicRendererStyle &other)
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads object content from given DOM element.
The default vector tile renderer implementation.
void renderTile(const QgsVectorTileRendererData &tile, QgsRenderContext &context) override
Renders given vector tile. Must be called between startRender/stopRender.
QList< QgsVectorTileBasicRendererStyle > styles() const
Returns list of styles of the renderer.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
QMap< QString, QSet< QString > > usedAttributes(const QgsRenderContext &) override
Returns field names of sub-layers that will be used for rendering. Must be called between startRender...
QgsVectorTileBasicRenderer()
Constructs renderer with no styles.
void setStyles(const QList< QgsVectorTileBasicRendererStyle > &styles)
Sets list of styles of the renderer.
void startRender(QgsRenderContext &context, int tileZoom, const QgsTileRange &tileRange) override
Initializes rendering. It should be paired with a stopRender() call.
QString type() const override
Returns unique type name of the renderer implementation.
static QList< QgsVectorTileBasicRendererStyle > simpleStyle(const QColor &polygonFillColor, const QColor &polygonStrokeColor, double polygonStrokeWidth, const QColor &lineStrokeColor, double lineStrokeWidth, const QColor &pointFillColor, const QColor &pointStrokeColor, double pointSize)
Returns a list of styles to render all layers with the given fill/stroke colors, stroke widths and ma...
QgsVectorTileBasicRenderer * clone() const override
Returns a clone of the renderer.
static QList< QgsVectorTileBasicRendererStyle > simpleStyleWithRandomColors()
Returns a list of styles to render all layers, using random colors.
QSet< QString > requiredLayers(QgsRenderContext &context, int tileZoom) const override
Returns a list of the layers required for rendering.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
void stopRender(QgsRenderContext &context) override
Finishes rendering and cleans up any resources.
Contains decoded features of a single vector tile and any other data necessary for rendering of it.
QgsVectorTileFeatures features() const
Returns features of the tile grouped by sub-layer names.
QMap< QString, QgsFields > fields() const
Returns per-layer fields.
QgsTileXYZ id() const
Returns coordinates of the tile.
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:938
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
const double DEFAULT_LINE_WIDTH
Definition: qgis.h:1036
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:1035
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:44
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27
QMap< QString, QVector< QgsFeature > > QgsVectorTileFeatures
Features of a vector tile, grouped by sub-layer names (key of the map)