QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsprocessingparametervectortilewriterlayers.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingparametervectortilewriterlayers.cpp
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 
18 #include "qgsvectorlayer.h"
19 
20 
22  : QgsProcessingParameterDefinition( name, description, QVariant(), false )
23 {
24 }
25 
27 {
29 }
30 
32 {
33  return typeName();
34 }
35 
37 {
38  if ( !input.isValid() )
39  return mFlags & FlagOptional;
40 
41  if ( input.type() != QVariant::List )
42  return false;
43 
44  const QVariantList inputList = input.toList();
45  for ( const QVariant &inputItem : inputList )
46  {
47  if ( inputItem.type() != QVariant::Map )
48  return false;
49  QVariantMap inputItemMap = inputItem.toMap();
50 
51  // "layer" is required - pointing to a vector layer
52  if ( !inputItemMap.contains( "layer" ) )
53  return false;
54 
55  const QVariant inputItemLayer = inputItemMap["layer"];
56 
57  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( inputItemLayer ) ) )
58  continue;
59 
60  if ( !context )
61  continue; // when called without context, we will skip checking whether the layer can be resolved
62 
63  if ( !QgsProcessingUtils::mapLayerFromString( inputItemLayer.toString(), *context ) )
64  return false;
65  }
66 
67  return true;
68 }
69 
71 {
72  QStringList parts;
73  const QList<QgsVectorTileWriter::Layer> layers = parameterAsLayers( value, context );
74  for ( const QgsVectorTileWriter::Layer &layer : layers )
75  {
76  QStringList layerDefParts;
77  layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
78  if ( !layer.filterExpression().isEmpty() )
79  layerDefParts << QStringLiteral( "'filterExpression': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.filterExpression() );
80  if ( !layer.layerName().isEmpty() )
81  layerDefParts << QStringLiteral( "'layerName': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerName() );
82  if ( layer.minZoom() >= 0 )
83  layerDefParts << QStringLiteral( "'minZoom': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.minZoom() );
84  if ( layer.maxZoom() >= 0 )
85  layerDefParts << QStringLiteral( "'maxZoom': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.maxZoom() );
86 
87  const QString layerDef = QStringLiteral( "{ %1 }" ).arg( layerDefParts.join( ',' ) );
88  parts << layerDef;
89  }
90  return parts.join( ',' ).prepend( '[' ).append( ']' );
91 }
92 
94 {
95  switch ( outputType )
96  {
98  {
99  QString code = QStringLiteral( "QgsProcessingParameterVectorTileWriterLayers('%1', %2)" )
101  return code;
102  }
103  }
104  return QString();
105 }
106 
107 QList<QgsVectorTileWriter::Layer> QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
108 {
109  QList<QgsVectorTileWriter::Layer> layers;
110  const QVariantList layersVariantList = layersVariant.toList();
111  for ( const QVariant &layerItem : layersVariantList )
112  {
113  const QVariantMap layerVariantMap = layerItem.toMap();
114  layers << variantMapAsLayer( layerVariantMap, context );
115  }
116  return layers;
117 }
118 
120 {
121  const QVariant layerVariant = layerVariantMap["layer"];
122 
123  QgsVectorLayer *inputLayer = nullptr;
124  if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
125  {
126  // good
127  }
128  else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
129  {
130  // good
131  }
132  else
133  {
134  // bad
135  }
136 
137  QgsVectorTileWriter::Layer writerLayer( inputLayer );
138  if ( layerVariantMap.contains( "filterExpression" ) )
139  writerLayer.setFilterExpression( layerVariantMap["filterExpression"].toString() );
140  if ( layerVariantMap.contains( "minZoom" ) )
141  writerLayer.setMinZoom( layerVariantMap["minZoom"].toInt() );
142  if ( layerVariantMap.contains( "maxZoom" ) )
143  writerLayer.setMaxZoom( layerVariantMap["maxZoom"].toInt() );
144  if ( layerVariantMap.contains( "layerName" ) )
145  writerLayer.setLayerName( layerVariantMap["layerName"].toString() );
146 
147  return writerLayer;
148 }
149 
151 {
152  QVariantMap vm;
153  if ( !layer.layer() )
154  return vm;
155 
156  vm["layer"] = layer.layer()->id();
157  vm["minZoom"] = layer.minZoom();
158  vm["maxZoom"] = layer.maxZoom();
159  vm["layerName"] = layer.layerName();
160  vm["filterExpression"] = layer.filterExpression();
161  return vm;
162 }
QgsVectorTileWriter::Layer::minZoom
int minZoom() const
Returns minimum zoom level at which this layer will be used. Negative value means no min....
Definition: qgsvectortilewriter.h:109
QgsProcessingParameterVectorTileWriterLayers::type
QString type() const override
Unique parameter type name.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:31
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:502
QgsVectorTileWriter::Layer::setLayerName
void setLayerName(const QString &name)
Sets layer name in the output. If not set, layer()->name() will be used.
Definition: qgsvectortilewriter.h:106
QgsVectorTileWriter::Layer::setMinZoom
void setMinZoom(int minzoom)
Sets minimum zoom level at which this layer will be used. Negative value means no min....
Definition: qgsvectortilewriter.h:111
QgsProcessingUtils::normalizeLayerSource
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
Definition: qgsprocessingutils.cpp:592
QgsProcessingParameterVectorTileWriterLayers::layerAsVariantMap
static QVariantMap layerAsVariantMap(const QgsVectorTileWriter::Layer &layer)
Converts a single input layer to QVariant representation (a QVariantMap)
Definition: qgsprocessingparametervectortilewriterlayers.cpp:150
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:334
QgsVectorTileWriter::Layer::setMaxZoom
void setMaxZoom(int maxzoom)
Sets maximum zoom level at which this layer will be used. Negative value means no max....
Definition: qgsvectortilewriter.h:116
QgsVectorTileWriter::Layer::filterExpression
QString filterExpression() const
Returns filter expression. If not empty, only features matching the expression will be used.
Definition: qgsvectortilewriter.h:99
QgsProcessingUtils::stringToPythonLiteral
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
Definition: qgsprocessingutils.cpp:701
QgsVectorTileWriter::Layer::setFilterExpression
void setFilterExpression(const QString &expr)
Sets filter expression. If not empty, only features matching the expression will be used.
Definition: qgsvectortilewriter.h:101
QgsVectorTileWriter::Layer::maxZoom
int maxZoom() const
Returns maximum zoom level at which this layer will be used. Negative value means no max....
Definition: qgsvectortilewriter.h:114
QgsProcessing::PythonQgsProcessingAlgorithmSubclass
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:64
QgsProcessingParameterDefinition::mFlags
Flags mFlags
Parameter flags.
Definition: qgsprocessingparameters.h:861
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsprocessingparametervectortilewriterlayers.h
QgsProcessingParameterDefinition::name
QString name() const
Returns the name of the parameter.
Definition: qgsprocessingparameters.h:488
QgsProcessingParameterVectorTileWriterLayers::asPythonString
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
Definition: qgsprocessingparametervectortilewriterlayers.cpp:93
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:169
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
QgsProcessingParameterVectorTileWriterLayers::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparametervectortilewriterlayers.h:70
qgsvectorlayer.h
QgsVectorTileWriter::Layer::layer
QgsVectorLayer * layer() const
Returns vector layer of this entry.
Definition: qgsvectortilewriter.h:96
QgsProcessingParameterVectorTileWriterLayers::QgsProcessingParameterVectorTileWriterLayers
QgsProcessingParameterVectorTileWriterLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingParameterVectorTileWriterLayers.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:21
QgsProcessingParameterVectorTileWriterLayers::checkValueIsAcceptable
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:36
QgsProcessingParameterVectorTileWriterLayers::variantMapAsLayer
static QgsVectorTileWriter::Layer variantMapAsLayer(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:119
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsProcessingUtils::variantToPythonLiteral
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
Definition: qgsprocessingutils.cpp:599
QgsVectorTileWriter::Layer
Configuration of a single input vector layer to be included in the output.
Definition: qgsvectortilewriter.h:86
QgsProcessingParameterVectorTileWriterLayers::valueAsPythonString
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
Definition: qgsprocessingparametervectortilewriterlayers.cpp:70
QgsProcessingUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
Definition: qgsprocessingutils.cpp:376
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:453
QgsProcessing::PythonOutputType
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:62
QgsVectorTileWriter::Layer::layerName
QString layerName() const
Returns layer name in the output. If not set, layer()->name() will be used.
Definition: qgsvectortilewriter.h:104
QgsProcessingParameterVectorTileWriterLayers::clone
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Definition: qgsprocessingparametervectortilewriterlayers.cpp:26