QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 
24 
25 QString QgsWriteVectorTilesBaseAlgorithm::group() const
26 {
27  return QObject::tr( "Vector tiles" );
28 }
29 
30 QString QgsWriteVectorTilesBaseAlgorithm::groupId() const
31 {
32  return QStringLiteral( "vectortiles" );
33 }
34 
35 QString QgsWriteVectorTilesBaseAlgorithm::shortHelpString() const
36 {
37  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." );
38 }
39 
40 void QgsWriteVectorTilesBaseAlgorithm::addBaseParameters()
41 {
42  addParameter( new QgsProcessingParameterVectorTileWriterLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
43 
44  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MIN_ZOOM" ), QObject::tr( "Minimum zoom level" ), QgsProcessingParameterNumber::Integer, 0, false, 0, 24 ) );
45  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MAX_ZOOM" ), QObject::tr( "Maximum zoom level" ), QgsProcessingParameterNumber::Integer, 3, false, 0, 24 ) );
46 
47  // optional extent
48  addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ), QVariant(), true ) );
49 }
50 
51 QVariantMap QgsWriteVectorTilesBaseAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
52 {
53  int minZoom = parameterAsInt( parameters, QStringLiteral( "MIN_ZOOM" ), context );
54  int maxZoom = parameterAsInt( parameters, QStringLiteral( "MAX_ZOOM" ), context );
55 
56  QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
57  const QList<QgsVectorTileWriter::Layer> layers = QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers( layersVariant, context );
58 
59  for ( const QgsVectorTileWriter::Layer &layer : layers )
60  {
61  if ( !layer.layer() )
62  throw QgsProcessingException( QObject::tr( "Unknown input layer" ) );
63  }
64 
65  QgsVectorTileWriter writer;
66  QVariantMap outputs;
67  prepareWriter( writer, parameters, context, outputs );
68 
69  writer.setMinZoom( minZoom );
70  writer.setMaxZoom( maxZoom );
71  writer.setLayers( layers );
72  writer.setTransformContext( context.transformContext() );
73 
74  if ( parameters.contains( QStringLiteral( "EXTENT" ) ) )
75  {
76  QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, QgsCoordinateReferenceSystem( "EPSG:3857" ) );
77  writer.setExtent( extent );
78  }
79 
80  bool res = writer.writeTiles( feedback );
81 
82  if ( !res )
83  throw QgsProcessingException( QObject::tr( "Failed to write vector tiles: " ) + writer.errorMessage() );
84 
85  return outputs;
86 }
87 
88 //
89 // QgsWriteVectorTilesXyzAlgorithm
90 //
91 
92 QString QgsWriteVectorTilesXyzAlgorithm::name() const
93 {
94  return QStringLiteral( "writevectortiles_xyz" );
95 }
96 
97 QString QgsWriteVectorTilesXyzAlgorithm::displayName() const
98 {
99  return QObject::tr( "Write Vector Tiles (XYZ)" );
100 }
101 
102 QgsProcessingAlgorithm *QgsWriteVectorTilesXyzAlgorithm::createInstance() const
103 {
104  return new QgsWriteVectorTilesXyzAlgorithm();
105 }
106 
107 void QgsWriteVectorTilesXyzAlgorithm::initAlgorithm( const QVariantMap & )
108 {
109  addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "OUTPUT_DIRECTORY" ), QObject::tr( "Output directory" ) ) );
110  addParameter( new QgsProcessingParameterString( QStringLiteral( "XYZ_TEMPLATE" ), QObject::tr( "File template" ), QStringLiteral( "{z}/{x}/{y}.pbf" ) ) );
111 
112  addBaseParameters();
113 }
114 
115 void QgsWriteVectorTilesXyzAlgorithm::prepareWriter( QgsVectorTileWriter &writer, const QVariantMap &parameters, QgsProcessingContext &context, QVariantMap &outputs )
116 {
117  QString outputDir = parameterAsString( parameters, QStringLiteral( "OUTPUT_DIRECTORY" ), context );
118  QString xyzTemplate = parameterAsString( parameters, QStringLiteral( "XYZ_TEMPLATE" ), context );
119  QgsDataSourceUri dsUri;
120  dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
121  dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( outputDir + "/" + xyzTemplate ).toString() );
122  QString uri = dsUri.encodedUri();
123 
124  writer.setDestinationUri( uri );
125 
126  outputs.insert( QStringLiteral( "OUTPUT_DIRECTORY" ), outputDir );
127 }
128 
129 //
130 // QgsWriteVectorTilesMbtilesAlgorithm
131 //
132 
133 QString QgsWriteVectorTilesMbtilesAlgorithm::name() const
134 {
135  return QStringLiteral( "writevectortiles_mbtiles" );
136 }
137 
138 QString QgsWriteVectorTilesMbtilesAlgorithm::displayName() const
139 {
140  return QObject::tr( "Write Vector Tiles (MBTiles)" );
141 }
142 
143 QgsProcessingAlgorithm *QgsWriteVectorTilesMbtilesAlgorithm::createInstance() const
144 {
145  return new QgsWriteVectorTilesMbtilesAlgorithm();
146 }
147 
148 void QgsWriteVectorTilesMbtilesAlgorithm::initAlgorithm( const QVariantMap & )
149 {
150  addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Destination MBTiles" ), QObject::tr( "MBTiles files (*.mbtiles)" ) ) );
151 
152  addBaseParameters();
153 
154  // optional metadata for MBTiles
155  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_NAME" ), QObject::tr( "Metadata: Name" ), QVariant(), false, true ) );
156  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_DESCRIPTION" ), QObject::tr( "Metadata: Description" ), QVariant(), false, true ) );
157  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_ATTRIBUTION" ), QObject::tr( "Metadata: Attribution" ), QVariant(), false, true ) );
158  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_VERSION" ), QObject::tr( "Metadata: Version" ), QVariant(), false, true ) );
159  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_TYPE" ), QObject::tr( "Metadata: Type" ), QVariant(), false, true ) );
160  addParameter( new QgsProcessingParameterString( QStringLiteral( "META_CENTER" ), QObject::tr( "Metadata: Center" ), QVariant(), false, true ) );
161 }
162 
163 void QgsWriteVectorTilesMbtilesAlgorithm::prepareWriter( QgsVectorTileWriter &writer, const QVariantMap &parameters, QgsProcessingContext &context, QVariantMap &outputs )
164 {
165  QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
166  QgsDataSourceUri dsUri;
167  dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
168  dsUri.setParam( QStringLiteral( "url" ), outputFile );
169  QString uri = dsUri.encodedUri();
170 
171  writer.setDestinationUri( uri );
172 
173  QString metaName = parameterAsString( parameters, QStringLiteral( "META_NAME" ), context );
174  QString metaDesciption = parameterAsString( parameters, QStringLiteral( "META_DESCRIPTION" ), context );
175  QString metaAttribution = parameterAsString( parameters, QStringLiteral( "META_ATTRIBUTION" ), context );
176  QString metaVersion = parameterAsString( parameters, QStringLiteral( "META_VERSION" ), context );
177  QString metaType = parameterAsString( parameters, QStringLiteral( "META_TYPE" ), context );
178  QString metaCenter = parameterAsString( parameters, QStringLiteral( "META_CENTER" ), context );
179 
180  QVariantMap meta;
181  if ( !metaName.isEmpty() )
182  meta["name"] = metaName;
183  if ( !metaDesciption.isEmpty() )
184  meta["description"] = metaDesciption;
185  if ( !metaAttribution.isEmpty() )
186  meta["attribution"] = metaAttribution;
187  if ( !metaVersion.isEmpty() )
188  meta["version"] = metaVersion;
189  if ( !metaType.isEmpty() )
190  meta["type"] = metaType;
191  if ( !metaCenter.isEmpty() )
192  meta["center"] = metaCenter;
193 
194  writer.setMetadata( meta );
195 
196  outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
197 }
198 
199 
QgsDataSourceUri
Definition: qgsdatasourceuri.h:35
QgsProcessingParameterNumber
Definition: qgsprocessingparameters.h:1838
QgsVectorTileWriter::errorMessage
QString errorMessage() const
Returns error message related to the previous call to writeTiles().
Definition: qgsvectortilewriter.h:163
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsRectangle
Definition: qgsrectangle.h:41
QgsVectorTileWriter::setDestinationUri
void setDestinationUri(const QString &uri)
Sets where and how the vector tiles will be written.
Definition: qgsvectortilewriter.h:128
QgsVectorTileWriter::setMetadata
void setMetadata(const QVariantMap &metadata)
Sets that will be written to the output dataset. See class description for more on metadata support.
Definition: qgsvectortilewriter.h:145
QgsVectorTileWriter::setLayers
void setLayers(const QList< QgsVectorTileWriter::Layer > &layers)
Sets vector layers and their configuration for output of vector tiles.
Definition: qgsvectortilewriter.h:142
QgsVectorTileWriter::setMinZoom
void setMinZoom(int minZoom)
Sets the minimum zoom level of tiles. Allowed values are in interval [0,24].
Definition: qgsvectortilewriter.h:137
QgsVectorTileWriter::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &transformContext)
Sets coordinate transform context for transforms between layers and tile matrix CRS.
Definition: qgsvectortilewriter.h:148
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
qgsprocessingparametervectortilewriterlayers.h
QgsProcessingParameterFileDestination
Definition: qgsprocessingparameters.h:3005
qgsalgorithmwritevectortiles.h
QgsDataSourceUri::setParam
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Definition: qgsdatasourceuri.cpp:778
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
QgsProcessingParameterExtent
Definition: qgsprocessingparameters.h:1502
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:135
QgsVectorTileWriter::setMaxZoom
void setMaxZoom(int maxZoom)
Sets the maximum zoom level of tiles. Allowed values are in interval [0,24].
Definition: qgsvectortilewriter.h:139
QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers
static QList< QgsVectorTileWriter::Layer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:106
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
qgsvectorlayer.h
qgsvectortilewriter.h
QgsProcessingParameterVectorTileWriterLayers
A parameter for processing algorithms that need a list of input vector layers for writing of vector t...
Definition: qgsprocessingparametervectortilewriterlayers.h:45
QgsDataSourceUri::encodedUri
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
Definition: qgsdatasourceuri.cpp:610
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
QgsVectorTileWriter
Definition: qgsvectortilewriter.h:72
QgsVectorTileWriter::Layer
Definition: qgsvectortilewriter.h:84
QgsVectorTileWriter::writeTiles
bool writeTiles(QgsFeedback *feedback=nullptr)
Writes vector tiles according to the configuration.
Definition: qgsvectortilewriter.cpp:41
QgsProcessingException
Definition: qgsexception.h:82
QgsVectorTileWriter::setExtent
void setExtent(const QgsRectangle &extent)
Sets extent of vector tile output.
Definition: qgsvectortilewriter.h:134
QgsProcessingParameterFolderDestination
Definition: qgsprocessingparameters.h:3068