QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmwritevectortiles.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmwritevectortiles.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
17
19#include "qgsvectorlayer.h"
20#include "qgsvectortilewriter.h"
21
22#include <QUrl>
23
25
26
27QString QgsWriteVectorTilesBaseAlgorithm::group() const
28{
29 return QObject::tr( "Vector tiles" );
30}
31
32QString QgsWriteVectorTilesBaseAlgorithm::groupId() const
33{
34 return QStringLiteral( "vectortiles" );
35}
36
37QString QgsWriteVectorTilesBaseAlgorithm::shortHelpString() const
38{
39 return QObject::tr( "This algorithm exports one or more vector layers to vector tiles - a data format optimized for fast map rendering and small data size." );
40}
41
42QString QgsWriteVectorTilesBaseAlgorithm::shortDescription() const
43{
44 return QObject::tr( "Exports one or more vector layers to vector tiles." );
45}
46
47void QgsWriteVectorTilesBaseAlgorithm::addBaseParameters()
48{
49 addParameter( new QgsProcessingParameterVectorTileWriterLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
50
51 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MIN_ZOOM" ), QObject::tr( "Minimum zoom level" ), Qgis::ProcessingNumberParameterType::Integer, 0, false, 0, 24 ) );
52 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_ZOOM" ), QObject::tr( "Maximum zoom level" ), Qgis::ProcessingNumberParameterType::Integer, 3, false, 0, 24 ) );
53
54 // optional extent
55 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ), QVariant(), true ) );
56}
57
58QVariantMap QgsWriteVectorTilesBaseAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
59{
60 const int minZoom = parameterAsInt( parameters, QStringLiteral( "MIN_ZOOM" ), context );
61 const int maxZoom = parameterAsInt( parameters, QStringLiteral( "MAX_ZOOM" ), context );
62
63 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
64 const QList<QgsVectorTileWriter::Layer> layers = QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers( layersVariant, context );
65
66 for ( const QgsVectorTileWriter::Layer &layer : layers )
67 {
68 if ( !layer.layer() )
69 throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );
70 }
71
73 QVariantMap outputs;
74 prepareWriter( writer, parameters, context, outputs );
75
76 writer.setMinZoom( minZoom );
77 writer.setMaxZoom( maxZoom );
78 writer.setLayers( layers );
79 writer.setTransformContext( context.transformContext() );
80
81 if ( parameters.contains( QStringLiteral( "EXTENT" ) ) )
82 {
83 const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, QgsCoordinateReferenceSystem( "EPSG:3857" ) );
84 writer.setExtent( extent );
85 }
86
87 const bool res = writer.writeTiles( feedback );
88
89 if ( !res )
90 throw QgsProcessingException( QObject::tr( "Failed to write vector tiles: " ) + writer.errorMessage() );
91
92 return outputs;
93}
94
95//
96// QgsWriteVectorTilesXyzAlgorithm
97//
98
99QString QgsWriteVectorTilesXyzAlgorithm::name() const
100{
101 return QStringLiteral( "writevectortiles_xyz" );
102}
103
104QString QgsWriteVectorTilesXyzAlgorithm::displayName() const
105{
106 return QObject::tr( "Write Vector Tiles (XYZ)" );
107}
108
109QStringList QgsWriteVectorTilesXyzAlgorithm::tags() const
110{
111 return QObject::tr( "xyz,vector,tiles" ).split( ',' );
112}
113
114QgsProcessingAlgorithm *QgsWriteVectorTilesXyzAlgorithm::createInstance() const
115{
116 return new QgsWriteVectorTilesXyzAlgorithm();
117}
118
119void QgsWriteVectorTilesXyzAlgorithm::initAlgorithm( const QVariantMap & )
120{
121 addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "OUTPUT_DIRECTORY" ), QObject::tr( "Output directory" ) ) );
122 addParameter( new QgsProcessingParameterString( QStringLiteral( "XYZ_TEMPLATE" ), QObject::tr( "File template" ), QStringLiteral( "{z}/{x}/{y}.pbf" ) ) );
123
124 addBaseParameters();
125}
126
127void QgsWriteVectorTilesXyzAlgorithm::prepareWriter( QgsVectorTileWriter &writer, const QVariantMap &parameters, QgsProcessingContext &context, QVariantMap &outputs )
128{
129 const QString outputDir = parameterAsString( parameters, QStringLiteral( "OUTPUT_DIRECTORY" ), context );
130 const QString xyzTemplate = parameterAsString( parameters, QStringLiteral( "XYZ_TEMPLATE" ), context );
131 QgsDataSourceUri dsUri;
132 dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
133 dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( outputDir + "/" + xyzTemplate ).toString() );
134 const QString uri = dsUri.encodedUri();
135
136 writer.setDestinationUri( uri );
137
138 outputs.insert( QStringLiteral( "OUTPUT_DIRECTORY" ), outputDir );
139}
140
141//
142// QgsWriteVectorTilesMbtilesAlgorithm
143//
144
145QString QgsWriteVectorTilesMbtilesAlgorithm::name() const
146{
147 return QStringLiteral( "writevectortiles_mbtiles" );
148}
149
150QString QgsWriteVectorTilesMbtilesAlgorithm::displayName() const
151{
152 return QObject::tr( "Write Vector Tiles (MBTiles)" );
153}
154
155QStringList QgsWriteVectorTilesMbtilesAlgorithm::tags() const
156{
157 return QObject::tr( "mbtiles,vector" ).split( ',' );
158}
159
160QgsProcessingAlgorithm *QgsWriteVectorTilesMbtilesAlgorithm::createInstance() const
161{
162 return new QgsWriteVectorTilesMbtilesAlgorithm();
163}
164
165void QgsWriteVectorTilesMbtilesAlgorithm::initAlgorithm( const QVariantMap & )
166{
167 addParameter( new QgsProcessingParameterVectorTileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Destination MBTiles" ) ) );
168
169 addBaseParameters();
170
171 // optional metadata for MBTiles
172 addParameter( new QgsProcessingParameterString( QStringLiteral( "META_NAME" ), QObject::tr( "Metadata: Name" ), QVariant(), false, true ) );
173 addParameter( new QgsProcessingParameterString( QStringLiteral( "META_DESCRIPTION" ), QObject::tr( "Metadata: Description" ), QVariant(), false, true ) );
174 addParameter( new QgsProcessingParameterString( QStringLiteral( "META_ATTRIBUTION" ), QObject::tr( "Metadata: Attribution" ), QVariant(), false, true ) );
175 addParameter( new QgsProcessingParameterString( QStringLiteral( "META_VERSION" ), QObject::tr( "Metadata: Version" ), QVariant(), false, true ) );
176 auto metaTypeParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "META_TYPE" ), QObject::tr( "Metadata: Type" ), QVariant(), false, true );
177 metaTypeParam->setMetadata( { { QStringLiteral( "widget_wrapper" ), QVariantMap( { { QStringLiteral( "value_hints" ), QStringList() << QStringLiteral( "overlay" ) << QStringLiteral( "baselayer" ) } } ) }
178 } );
179 addParameter( metaTypeParam.release() );
180 addParameter( new QgsProcessingParameterString( QStringLiteral( "META_CENTER" ), QObject::tr( "Metadata: Center" ), QVariant(), false, true ) );
181}
182
183void QgsWriteVectorTilesMbtilesAlgorithm::prepareWriter( QgsVectorTileWriter &writer, const QVariantMap &parameters, QgsProcessingContext &context, QVariantMap &outputs )
184{
185 const QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
186 QgsDataSourceUri dsUri;
187 dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
188 dsUri.setParam( QStringLiteral( "url" ), outputFile );
189 const QString uri = dsUri.encodedUri();
190
191 writer.setDestinationUri( uri );
192
193 const QString metaName = parameterAsString( parameters, QStringLiteral( "META_NAME" ), context );
194 const QString metaDescription = parameterAsString( parameters, QStringLiteral( "META_DESCRIPTION" ), context );
195 const QString metaAttribution = parameterAsString( parameters, QStringLiteral( "META_ATTRIBUTION" ), context );
196 const QString metaVersion = parameterAsString( parameters, QStringLiteral( "META_VERSION" ), context );
197 const QString metaType = parameterAsString( parameters, QStringLiteral( "META_TYPE" ), context );
198 const QString metaCenter = parameterAsString( parameters, QStringLiteral( "META_CENTER" ), context );
199
200 QVariantMap meta;
201 if ( !metaName.isEmpty() )
202 meta["name"] = metaName;
203 if ( !metaDescription.isEmpty() )
204 meta["description"] = metaDescription;
205 if ( !metaAttribution.isEmpty() )
206 meta["attribution"] = metaAttribution;
207 if ( !metaVersion.isEmpty() )
208 meta["version"] = metaVersion;
209 if ( !metaType.isEmpty() )
210 meta["type"] = metaType;
211 if ( !metaCenter.isEmpty() )
212 meta["center"] = metaCenter;
213
214 writer.setMetadata( meta );
215
216 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
217}
218
219
Represents a coordinate reference system (CRS).
Stores the component parts of a data source URI (e.g.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A rectangular map extent parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
A numeric parameter for processing algorithms.
A string parameter for processing algorithms.
A vector tile layer destination parameter, for specifying the destination path for a vector tile laye...
A parameter for Processing algorithms that need a list of input vector layers for writing of vector t...
static QList< QgsVectorTileWriter::Layer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
A rectangle specified with double values.
Configuration of a single input vector layer to be included in the output.
Takes care of writing vector tiles.
QString errorMessage() const
Returns error message related to the previous call to writeTiles().
void setMaxZoom(int maxZoom)
Sets the maximum zoom level of tiles. Allowed values are in interval [0,24].
void setDestinationUri(const QString &uri)
Sets where and how the vector tiles will be written.
void setTransformContext(const QgsCoordinateTransformContext &transformContext)
Sets coordinate transform context for transforms between layers and tile matrix CRS.
void setExtent(const QgsRectangle &extent)
Sets extent of vector tile output.
bool writeTiles(QgsFeedback *feedback=nullptr)
Writes vector tiles according to the configuration.
void setLayers(const QList< QgsVectorTileWriter::Layer > &layers)
Sets vector layers and their configuration for output of vector tiles.
void setMinZoom(int minZoom)
Sets the minimum zoom level of tiles. Allowed values are in interval [0,24].
void setMetadata(const QVariantMap &metadata)
Sets that will be written to the output dataset. See class description for more on metadata support.