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