1 /***************************************************************************
2   qgsvectortilemvtencoder.h
3   --------------------------------------
4   Date                 : April 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 QGSVECTORTILEMVTENCODER_H
17 #define QGSVECTORTILEMVTENCODER_H
18 
19 #define SIP_NO_FILE
20 
21 #include "qgstiles.h"
22 #include "qgsvectortilerenderer.h"
23 #include "vector_tile.pb.h"
24 #include "qgscoordinatetransform.h"
25 
26 /**
27  * \ingroup core
28  * \brief Handles conversion of vector features to Mapbox vector tiles encoding.
29  *
30  * Geometries are stored as a series of MoveTo / LineTo / ClosePath commands
31  * with coordinates in integer values (see resolution(), called "extent" in the spec).
32  * Attributes are stored as key-value pairs of tags: keys are always strings, values
33  * can be integers, floating point numbers, booleans or strings.
34  *
35  * See the specification for details:
36  * https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
37  *
38  * \since QGIS 3.14
39  */
40 class CORE_EXPORT QgsVectorTileMVTEncoder
41 {
42   public:
43     //! Creates MVT encoder for the given tile coordinates for Web Mercator
44     explicit QgsVectorTileMVTEncoder( QgsTileXYZ tileID );
45 
46     //! Creates MVT encoder for the given tile coordinates and tile matrix
47     explicit QgsVectorTileMVTEncoder( QgsTileXYZ tileID, const QgsTileMatrix &tileMatrix );
48 
49     //! Returns resolution of coordinates of geometries within the tile. The default is 4096.
resolution()50     int resolution() const { return mResolution; }
51     //! Sets the resolution of coordinates of geometries within the tile
setResolution(int extent)52     void setResolution( int extent ) { mResolution = extent; }
53 
54     //! Returns size of the buffer zone around tile edges in integer tile coordinates. The default is 256 (~6%)
tileBuffer()55     int tileBuffer() const { return mBuffer; }
56     //! Sets size of the buffer zone around tile edges in integer tile coordinates
setTileBuffer(int buffer)57     void setTileBuffer( int buffer ) { mBuffer = buffer; }
58 
59     //! Sets coordinate transform context for transforms between layers and tile matrix CRS
setTransformContext(const QgsCoordinateTransformContext & transformContext)60     void setTransformContext( const QgsCoordinateTransformContext &transformContext ) { mTransformContext = transformContext; }
61 
62     /**
63      * Fetches data from vector layer for the given tile, does reprojection and clipping
64      *
65      * Optional feedback object may be provided to support cancellation.
66      */
67     void addLayer( QgsVectorLayer *layer, QgsFeedback *feedback = nullptr, QString filterExpression = QString(), QString layerName = QString() );
68 
69     //! Encodes MVT using data stored previously with addLayer() calls
70     QByteArray encode() const;
71 
72   private:
73     void addFeature( vector_tile::Tile_Layer *tileLayer, const QgsFeature &f );
74 
75   private:
76     QgsTileXYZ mTileID;
77     int mResolution = 4096;
78     int mBuffer = 256;
79     QgsCoordinateTransformContext mTransformContext;
80 
81     QgsRectangle mTileExtent;
82     QgsCoordinateReferenceSystem mCrs;
83 
84     QgsVectorTileFeatures mFeatures;
85 
86     vector_tile::Tile tile;
87 
88     QMap<QVariant, int> mKnownValues;
89 
90 };
91 
92 #endif // QGSVECTORTILEMVTENCODER_H
93