Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsvectortilewriter.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 QGSVECTORTILEWRITER_H 17 : : #define QGSVECTORTILEWRITER_H 18 : : 19 : : #include <QCoreApplication> 20 : : #include "qgsrectangle.h" 21 : : #include "qgscoordinatetransformcontext.h" 22 : : 23 : : class QgsFeedback; 24 : : class QgsTileMatrix; 25 : : class QgsTileXYZ; 26 : : class QgsVectorLayer; 27 : : 28 : : 29 : : /** 30 : : * \ingroup core 31 : : * \brief Takes care of writing vector tiles. The intended use is to set up the class 32 : : * by setting the destination URI, extent, zoom level range and input vector 33 : : * layers. Then with a call to writeTiles() the data gets written. In case 34 : : * of a failure, errorMessage() returns the cause of the problem during writing. 35 : : * 36 : : * The syntax of destination URIs is the same like the data source string 37 : : * used by vector tile layers: parameters are encoded as a HTTP query string, 38 : : * where "type" key determines the type of the destination container, 39 : : * the "url" key is normally the path. Currently supported types: 40 : : * 41 : : * - "xyz" - tile data written as local files, using a template where {x},{y},{z} 42 : : * are replaced by the actual tile column, row and zoom level numbers, e.g.: 43 : : * file:///home/qgis/tiles/{z}/{x}/{y}.pbf 44 : : * - "mbtiles" - tile data written to a new MBTiles file, the "url" key should 45 : : * be ordinary file system path, e.g.: /home/qgis/output.mbtiles 46 : : * 47 : : * Currently the writer only support MVT encoding of data. 48 : : * 49 : : * Metadata support: it is possible to pass a QVariantMap with metadata. This 50 : : * is backend dependent. Currently only "mbtiles" source type supports writing 51 : : * of metadata. The key-value pairs will be written to the "metadata" table 52 : : * in the MBTiles file. Some useful metadata keys listed here, but see MBTiles spec 53 : : * for more details: 54 : : * 55 : : * - "name" - human-readable name of the tileset 56 : : * - "bounds" - extent in WGS 84: "minlon,minlat,maxlon,maxlat" 57 : : * - "center" - default view of the map: "longitude,latitude,zoomlevel" 58 : : * - "minzoom" - lowest zoom level 59 : : * - "maxzoom" - highest zoom level 60 : : * - "attribution" - string that explains the sources of data 61 : : * - "description" - descriptions of the content 62 : : * - "type" - whether this is an overlay or a basemap 63 : : * - "version" - version of the tileset 64 : : * 65 : : * Vector tile writer always writes "format" and "json" metadata. If not specified 66 : : * in metadata by the client, tile writer also writes "name", "bounds", "minzoom" 67 : : * and "maxzoom". 68 : : * 69 : : * 70 : : * \since QGIS 3.14 71 : : */ 72 : 0 : class CORE_EXPORT QgsVectorTileWriter 73 : : { 74 : 0 : Q_DECLARE_TR_FUNCTIONS( QgsVectorTileWriter ) 75 : : 76 : : public: 77 : : QgsVectorTileWriter(); 78 : : 79 : : /** 80 : : * \ingroup core 81 : : * \brief Configuration of a single input vector layer to be included in the output 82 : : * \since QGIS 3.14 83 : : */ 84 : 0 : class Layer 85 : : { 86 : : public: 87 : : //! Constructs an entry for a vector layer 88 : 0 : explicit Layer( QgsVectorLayer *layer ) 89 : 0 : : mLayer( layer ) 90 : : { 91 : 0 : } 92 : : 93 : : //! Returns vector layer of this entry 94 : 0 : QgsVectorLayer *layer() const { return mLayer; } 95 : : 96 : : //! Returns filter expression. If not empty, only features matching the expression will be used 97 : 0 : QString filterExpression() const { return mFilterExpression; } 98 : : //! Sets filter expression. If not empty, only features matching the expression will be used 99 : 0 : void setFilterExpression( const QString &expr ) { mFilterExpression = expr; } 100 : : 101 : : //! Returns layer name in the output. If not set, layer()->name() will be used. 102 : 0 : QString layerName() const { return mLayerName; } 103 : : //! Sets layer name in the output. If not set, layer()->name() will be used. 104 : 0 : void setLayerName( const QString &name ) { mLayerName = name; } 105 : : 106 : : //! Returns minimum zoom level at which this layer will be used. Negative value means no min. zoom level 107 : 0 : int minZoom() const { return mMinZoom; } 108 : : //! Sets minimum zoom level at which this layer will be used. Negative value means no min. zoom level 109 : 0 : void setMinZoom( int minzoom ) { mMinZoom = minzoom; } 110 : : 111 : : //! Returns maximum zoom level at which this layer will be used. Negative value means no max. zoom level 112 : 0 : int maxZoom() const { return mMaxZoom; } 113 : : //! Sets maximum zoom level at which this layer will be used. Negative value means no max. zoom level 114 : 0 : void setMaxZoom( int maxzoom ) { mMaxZoom = maxzoom; } 115 : : 116 : : private: 117 : : QgsVectorLayer *mLayer; 118 : : QString mFilterExpression; 119 : : QString mLayerName; 120 : 0 : int mMinZoom = -1; 121 : 0 : int mMaxZoom = -1; 122 : : }; 123 : : 124 : : /** 125 : : * Sets where and how the vector tiles will be written. 126 : : * See the class description about the syntax of destination URIs. 127 : : */ 128 : 0 : void setDestinationUri( const QString &uri ) { mDestinationUri = uri; } 129 : : 130 : : /** 131 : : * Sets extent of vector tile output. Currently always in EPSG:3857. 132 : : * If unset, it will use the full extent of all input layers combined 133 : : */ 134 : 0 : void setExtent( const QgsRectangle &extent ) { mExtent = extent; } 135 : : 136 : : //! Sets the minimum zoom level of tiles. Allowed values are in interval [0,24] 137 : 0 : void setMinZoom( int minZoom ) { mMinZoom = minZoom; } 138 : : //! Sets the maximum zoom level of tiles. Allowed values are in interval [0,24] 139 : 0 : void setMaxZoom( int maxZoom ) { mMaxZoom = maxZoom; } 140 : : 141 : : //! Sets vector layers and their configuration for output of vector tiles 142 : 0 : void setLayers( const QList<QgsVectorTileWriter::Layer> &layers ) { mLayers = layers; } 143 : : 144 : : //! Sets that will be written to the output dataset. See class description for more on metadata support 145 : 0 : void setMetadata( const QVariantMap &metadata ) { mMetadata = metadata; } 146 : : 147 : : //! Sets coordinate transform context for transforms between layers and tile matrix CRS 148 : 0 : void setTransformContext( const QgsCoordinateTransformContext &transformContext ) { mTransformContext = transformContext; } 149 : : 150 : : /** 151 : : * Writes vector tiles according to the configuration. 152 : : * Returns TRUE on success (upon failure one can get error cause using errorMessage()) 153 : : * 154 : : * If a pointer to a feedback object is provided, it can be used to track progress or 155 : : * provide cancellation functionality. 156 : : */ 157 : : bool writeTiles( QgsFeedback *feedback = nullptr ); 158 : : 159 : : /** 160 : : * Returns error message related to the previous call to writeTiles(). Will return 161 : : * an empty string if writing was successful. 162 : : */ 163 : 0 : QString errorMessage() const { return mErrorMessage; } 164 : : 165 : : //! Returns calculated extent that combines extent of all input layers 166 : : QgsRectangle fullExtent() const; 167 : : 168 : : private: 169 : : bool writeTileFileXYZ( const QString &sourcePath, QgsTileXYZ tileID, const QgsTileMatrix &tileMatrix, const QByteArray &tileData ); 170 : : QString mbtilesJsonSchema(); 171 : : 172 : : private: 173 : : QgsRectangle mExtent; 174 : : int mMinZoom = 0; 175 : : int mMaxZoom = 4; 176 : : QList<Layer> mLayers; 177 : : QString mDestinationUri; 178 : : QVariantMap mMetadata; 179 : : QgsCoordinateTransformContext mTransformContext; 180 : : 181 : : QString mErrorMessage; 182 : : }; 183 : : 184 : : #endif // QGSVECTORTILEWRITER_H