1 /***************************************************************************
2   qgsvectortilebasicrenderer.h
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 
16 #ifndef QGSVECTORTILEBASICRENDERER_H
17 #define QGSVECTORTILEBASICRENDERER_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 
22 #include "qgsvectortilerenderer.h"
23 
24 class QgsLineSymbol;
25 class QgsFillSymbol;
26 class QgsMarkerSymbol;
27 
28 class QgsSymbol;
29 
30 /**
31  * \ingroup core
32  * \brief Definition of map rendering of a subset of vector tile data. The subset of data is defined by:
33  *
34  * - sub-layer name
35  * - geometry type (a single sub-layer may have multiple geometry types)
36  * - filter expression
37  *
38  * Rendering is determined by the associated symbol (QgsSymbol). Symbol has to be of the same
39  * type as the chosen geometryType() - i.e. QgsMarkerSymbol for points, QgsLineSymbol for linestrings
40  * and QgsFillSymbol for polygons.
41  *
42  * It is possible to further constrain when this style is applied by setting a range of allowed
43  * zoom levels, or by disabling it.
44  *
45  * \since QGIS 3.14
46  */
47 class CORE_EXPORT QgsVectorTileBasicRendererStyle
48 {
49   public:
50     //! Constructs a style object
51     QgsVectorTileBasicRendererStyle( const QString &stName = QString(), const QString &laName = QString(), QgsWkbTypes::GeometryType geomType = QgsWkbTypes::UnknownGeometry );
52     //! Constructs a style object as a copy of another style
53     QgsVectorTileBasicRendererStyle( const QgsVectorTileBasicRendererStyle &other );
54     QgsVectorTileBasicRendererStyle &operator=( const QgsVectorTileBasicRendererStyle &other );
55     ~QgsVectorTileBasicRendererStyle();
56 
57     //! Sets human readable name of this style
setStyleName(const QString & name)58     void setStyleName( const QString &name ) { mStyleName = name; }
59     //! Returns human readable name of this style
styleName()60     QString styleName() const { return mStyleName; }
61 
62     //! Sets name of the sub-layer to render (empty layer means that all layers match)
setLayerName(const QString & name)63     void setLayerName( const QString &name ) { mLayerName = name; }
64     //! Returns name of the sub-layer to render (empty layer means that all layers match)
layerName()65     QString layerName() const { return mLayerName; }
66 
67     //! Sets type of the geometry that will be used (point / line / polygon)
setGeometryType(QgsWkbTypes::GeometryType geomType)68     void setGeometryType( QgsWkbTypes::GeometryType geomType ) { mGeometryType = geomType; }
69     //! Returns type of the geometry that will be used (point / line / polygon)
geometryType()70     QgsWkbTypes::GeometryType geometryType() const { return mGeometryType; }
71 
72     //! Sets filter expression (empty filter means that all features match)
setFilterExpression(const QString & expr)73     void setFilterExpression( const QString &expr ) { mExpression = expr; }
74     //! Returns filter expression (empty filter means that all features match)
filterExpression()75     QString filterExpression() const { return mExpression; }
76 
77     //! Sets symbol for rendering. Takes ownership of the symbol.
78     void setSymbol( QgsSymbol *sym SIP_TRANSFER );
79     //! Returns symbol for rendering
symbol()80     QgsSymbol *symbol() const { return mSymbol.get(); }
81 
82     //! Sets whether this style is enabled (used for rendering)
setEnabled(bool enabled)83     void setEnabled( bool enabled ) { mEnabled = enabled; }
84     //! Returns whether this style is enabled (used for rendering)
isEnabled()85     bool isEnabled() const { return mEnabled; }
86 
87     //! Sets minimum zoom level index (negative number means no limit)
setMinZoomLevel(int minZoom)88     void setMinZoomLevel( int minZoom ) { mMinZoomLevel = minZoom; }
89     //! Returns minimum zoom level index (negative number means no limit)
minZoomLevel()90     int minZoomLevel() const { return mMinZoomLevel; }
91 
92     //! Sets maximum zoom level index (negative number means no limit)
setMaxZoomLevel(int maxZoom)93     void setMaxZoomLevel( int maxZoom ) { mMaxZoomLevel = maxZoom; }
94     //! Returns maxnimum zoom level index (negative number means no limit)
maxZoomLevel()95     int maxZoomLevel() const { return mMaxZoomLevel; }
96 
97     //! Returns whether the style is active at given zoom level (also checks "enabled" flag)
isActive(int zoomLevel)98     bool isActive( int zoomLevel ) const
99     {
100       return mEnabled && ( mMinZoomLevel == -1 || zoomLevel >= mMinZoomLevel ) && ( mMaxZoomLevel == -1 || zoomLevel <= mMaxZoomLevel );
101     }
102 
103     //! Writes object content to given DOM element
104     void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const;
105     //! Reads object content from given DOM element
106     void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
107 
108   private:
109     QString mStyleName;
110     QString mLayerName;
111     QgsWkbTypes::GeometryType mGeometryType;
112     std::unique_ptr<QgsSymbol> mSymbol;
113     bool mEnabled = true;
114     QString mExpression;
115     int mMinZoomLevel = -1;
116     int mMaxZoomLevel = -1;
117 };
118 
119 
120 /**
121  * \ingroup core
122  * \brief The default vector tile renderer implementation. It has an ordered list of "styles",
123  * each defines a rendering rule.
124  *
125  * \since QGIS 3.14
126  */
127 class CORE_EXPORT QgsVectorTileBasicRenderer : public QgsVectorTileRenderer
128 {
129   public:
130     //! Constructs renderer with no styles
131     QgsVectorTileBasicRenderer();
132 
133     QString type() const override;
134     QgsVectorTileBasicRenderer *clone() const override SIP_FACTORY;
135     void startRender( QgsRenderContext &context, int tileZoom, const QgsTileRange &tileRange ) override;
136     QMap<QString, QSet<QString> > usedAttributes( const QgsRenderContext & ) override SIP_SKIP;
137     QSet< QString > requiredLayers( QgsRenderContext &context, int tileZoom ) const override;
138     void stopRender( QgsRenderContext &context ) override;
139     void renderTile( const QgsVectorTileRendererData &tile, QgsRenderContext &context ) override;
140     void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const override;
141     void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) override;
142 
143     //! Sets list of styles of the renderer
144     void setStyles( const QList<QgsVectorTileBasicRendererStyle> &styles );
145     //! Returns list of styles of the renderer
146     QList<QgsVectorTileBasicRendererStyle> styles() const;
147     //! Updates style definition at the paricular index of the list (the index must be in interval [0,N-1] otherwise this function does nothing)
setStyle(int index,const QgsVectorTileBasicRendererStyle & style)148     void setStyle( int index, const QgsVectorTileBasicRendererStyle &style ) { mStyles[index] = style; }
149     //! Returns style definition at the particular index
style(int index)150     QgsVectorTileBasicRendererStyle style( int index ) const { return mStyles[index]; }
151 
152     //! Returns a list of styles to render all layers with the given fill/stroke colors, stroke widths and marker sizes
153     static QList<QgsVectorTileBasicRendererStyle> simpleStyle(
154       const QColor &polygonFillColor, const QColor &polygonStrokeColor, double polygonStrokeWidth,
155       const QColor &lineStrokeColor, double lineStrokeWidth,
156       const QColor &pointFillColor, const QColor &pointStrokeColor, double pointSize );
157 
158     //! Returns a list of styles to render all layers, using random colors
159     static QList<QgsVectorTileBasicRendererStyle> simpleStyleWithRandomColors();
160 
161   private:
162     void setDefaultStyle();
163 
164   private:
165     //! List of rendering styles
166     QList<QgsVectorTileBasicRendererStyle> mStyles;
167 
168     // temporary bits
169 
170     //! Names of required fields for each sub-layer (only valid between startRender/stopRender calls)
171     QMap<QString, QSet<QString> > mRequiredFields;
172 
173 };
174 
175 #endif // QGSVECTORTILEBASICRENDERER_H
176