1 /***************************************************************************
2   qgsvectortilerenderer.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 QGSVECTORTILERENDERER_H
17 #define QGSVECTORTILERENDERER_H
18 
19 #include "qgis_core.h"
20 
21 #include "qgsfeature.h"
22 
23 #include "qgstiles.h"
24 
25 class QgsRenderContext;
26 class QgsReadWriteContext;
27 class QgsProject;
28 
29 //! Features of a vector tile, grouped by sub-layer names (key of the map)
30 typedef QMap<QString, QVector<QgsFeature> > QgsVectorTileFeatures SIP_SKIP;
31 
32 /**
33  * \ingroup core
34  * \brief Contains decoded features of a single vector tile and any other data necessary
35  * for rendering of it.
36  *
37  * \since QGIS 3.14
38  */
39 class CORE_EXPORT QgsVectorTileRendererData
40 {
41   public:
42     //! Constructs the object
QgsVectorTileRendererData(QgsTileXYZ id)43     explicit QgsVectorTileRendererData( QgsTileXYZ id )
44       : mId( id )
45     {}
46 
47     //! Returns coordinates of the tile
id()48     QgsTileXYZ id() const { return mId; }
49 
50     //! Sets polygon of the tile
setTilePolygon(QPolygon polygon)51     void setTilePolygon( QPolygon polygon ) { mTilePolygon = polygon; }
52     //! Returns polygon (made out of four corners of the tile) in screen coordinates calculated from render context
tilePolygon()53     QPolygon tilePolygon() const { return mTilePolygon; }
54 
55     //! Sets per-layer fields
setFields(const QMap<QString,QgsFields> & fields)56     void setFields( const QMap<QString, QgsFields> &fields ) { mFields = fields; }
57     //! Returns per-layer fields
fields()58     QMap<QString, QgsFields> fields() const { return mFields; }
59 
60     //! Sets features of the tile
setFeatures(const QgsVectorTileFeatures & features)61     void setFeatures( const QgsVectorTileFeatures &features ) SIP_SKIP { mFeatures = features; }
62     //! Returns features of the tile grouped by sub-layer names
features()63     QgsVectorTileFeatures features() const SIP_SKIP { return mFeatures; }
64     //! Returns list of layer names present in the tile
layers()65     QStringList layers() const { return mFeatures.keys(); }
66     //! Returns list of all features within a single sub-layer
layerFeatures(const QString & layerName)67     QVector<QgsFeature> layerFeatures( const QString &layerName ) const { return mFeatures[layerName]; }
68 
69   private:
70     //! Position of the tile in the tile matrix set
71     QgsTileXYZ mId;
72     //! Per-layer fields
73     QMap<QString, QgsFields> mFields;
74     //! Features of the tile grouped into sub-layers
75     QgsVectorTileFeatures mFeatures;
76     //! Polygon (made out of four corners of the tile) in screen coordinates calculated from render context
77     QPolygon mTilePolygon;
78 };
79 
80 /**
81  * \ingroup core
82  * \brief Abstract base class for all vector tile renderer implementations.
83  *
84  * For rendering it is expected that client code calls:
85  *
86  * # startRender() to prepare renderer
87  * # renderTile() for each tile
88  * # stopRender() to clean up renderer and free resources
89  *
90  * \since QGIS 3.14
91  */
92 class CORE_EXPORT QgsVectorTileRenderer
93 {
94 
95 #ifdef SIP_RUN
96     SIP_CONVERT_TO_SUBCLASS_CODE
97 
98     const QString type = sipCpp->type();
99 
100     if ( type == QLatin1String( "basic" ) )
101       sipType = sipType_QgsVectorTileBasicRenderer;
102     else
103       sipType = 0;
104     SIP_END
105 #endif
106 
107   public:
108     virtual ~QgsVectorTileRenderer() = default;
109 
110     //! Returns unique type name of the renderer implementation
111     virtual QString type() const = 0;
112 
113     //! Returns a clone of the renderer
114     virtual QgsVectorTileRenderer *clone() const = 0 SIP_FACTORY;
115 
116     //! Initializes rendering. It should be paired with a stopRender() call.
117     virtual void startRender( QgsRenderContext &context, int tileZoom, const QgsTileRange &tileRange ) = 0;
118 
119     //! Returns field names of sub-layers that will be used for rendering. Must be called between startRender/stopRender.
usedAttributes(const QgsRenderContext &)120     virtual QMap<QString, QSet<QString> > usedAttributes( const QgsRenderContext & ) SIP_SKIP { return QMap<QString, QSet<QString> >(); }
121 
122     //TODO QGIS 4.0 -- make pure virtual
123 
124     /**
125      * Returns a list of the layers required for rendering.
126      *
127      * Only layers which are visible at the specified \a tileZoom should be included in this list.
128      *
129      * An empty string present in the list indicates that all layer in the tiles are required.
130      *
131      * \since QGIS 3.16
132      */
requiredLayers(QgsRenderContext & context,int tileZoom)133     virtual QSet< QString > requiredLayers( QgsRenderContext &context, int tileZoom ) const { Q_UNUSED( context ); Q_UNUSED( tileZoom ); return QSet< QString >() << QString(); }
134 
135     //! Finishes rendering and cleans up any resources
136     virtual void stopRender( QgsRenderContext &context ) = 0;
137 
138     //! Renders given vector tile. Must be called between startRender/stopRender.
139     virtual void renderTile( const QgsVectorTileRendererData &tile, QgsRenderContext &context ) = 0;
140 
141     //! Writes renderer's properties to given XML element
142     virtual void writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const = 0;
143     //! Reads renderer's properties from given XML element
144     virtual void readXml( const QDomElement &elem, const QgsReadWriteContext &context ) = 0;
145     //! Resolves references to other objects - second phase of loading - after readXml()
resolveReferences(const QgsProject & project)146     virtual void resolveReferences( const QgsProject &project ) { Q_UNUSED( project ) }
147 
148 };
149 
150 #endif // QGSVECTORTILERENDERER_H
151