QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
24
29
34
36{
37 if ( !input.isValid() )
39
40 if ( input.userType() != QMetaType::Type::QVariantList )
41 return false;
42
43 const QVariantList inputList = input.toList();
44 for ( const QVariant &inputItem : inputList )
45 {
46 if ( inputItem.userType() != QMetaType::Type::QVariantMap )
47 return false;
48 QVariantMap inputItemMap = inputItem.toMap();
49
50 // "layer" is required - pointing to a vector layer
51 if ( !inputItemMap.contains( "layer" ) )
52 return false;
53
54 const QVariant inputItemLayer = inputItemMap["layer"];
55
56 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( inputItemLayer ) ) )
57 continue;
58
59 if ( !context )
60 continue; // when called without context, we will skip checking whether the layer can be resolved
61
62 if ( !QgsProcessingUtils::mapLayerFromString( inputItemLayer.toString(), *context ) )
63 return false;
64 }
65
66 return true;
67}
68
70{
71 QStringList parts;
72 const QList<QgsVectorTileWriter::Layer> layers = parameterAsLayers( value, context );
73 for ( const QgsVectorTileWriter::Layer &layer : layers )
74 {
75 QStringList layerDefParts;
76 layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
77 if ( !layer.filterExpression().isEmpty() )
78 layerDefParts << QStringLiteral( "'filterExpression': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.filterExpression() );
79 if ( !layer.layerName().isEmpty() )
80 layerDefParts << QStringLiteral( "'layerName': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerName() );
81 if ( layer.minZoom() >= 0 )
82 layerDefParts << QStringLiteral( "'minZoom': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.minZoom() );
83 if ( layer.maxZoom() >= 0 )
84 layerDefParts << QStringLiteral( "'maxZoom': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.maxZoom() );
85
86 const QString layerDef = QStringLiteral( "{ %1 }" ).arg( layerDefParts.join( ',' ) );
87 parts << layerDef;
88 }
89 return parts.join( ',' ).prepend( '[' ).append( ']' );
90}
91
93{
94 switch ( outputType )
95 {
97 {
98 QString code = QStringLiteral( "QgsProcessingParameterVectorTileWriterLayers('%1', %2)" )
100 return code;
101 }
102 }
103 return QString();
104}
105
106QList<QgsVectorTileWriter::Layer> QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
107{
108 QList<QgsVectorTileWriter::Layer> layers;
109 const QVariantList layersVariantList = layersVariant.toList();
110 for ( const QVariant &layerItem : layersVariantList )
111 {
112 const QVariantMap layerVariantMap = layerItem.toMap();
113 layers << variantMapAsLayer( layerVariantMap, context );
114 }
115 return layers;
116}
117
119{
120 const QVariant layerVariant = layerVariantMap["layer"];
121
122 QgsVectorLayer *inputLayer = nullptr;
123 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
124 {
125 // good
126 }
127 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
128 {
129 // good
130 }
131 else
132 {
133 // bad
134 }
135
136 QgsVectorTileWriter::Layer writerLayer( inputLayer );
137 if ( layerVariantMap.contains( "filterExpression" ) )
138 writerLayer.setFilterExpression( layerVariantMap["filterExpression"].toString() );
139 if ( layerVariantMap.contains( "minZoom" ) )
140 writerLayer.setMinZoom( layerVariantMap["minZoom"].toInt() );
141 if ( layerVariantMap.contains( "maxZoom" ) )
142 writerLayer.setMaxZoom( layerVariantMap["maxZoom"].toInt() );
143 if ( layerVariantMap.contains( "layerName" ) )
144 writerLayer.setLayerName( layerVariantMap["layerName"].toString() );
145
146 return writerLayer;
147}
148
150{
151 QVariantMap vm;
152 if ( !layer.layer() )
153 return vm;
154
155 vm["layer"] = layer.layer()->id();
156 vm["minZoom"] = layer.minZoom();
157 vm["maxZoom"] = layer.maxZoom();
158 vm["layerName"] = layer.layerName();
159 vm["filterExpression"] = layer.filterExpression();
160 return vm;
161}
@ Optional
Parameter is optional.
Definition qgis.h:3765
QString id
Definition qgsmaplayer.h:83
Contains information about the context in which a processing algorithm is executed.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &help=QString())
Constructor for QgsProcessingParameterDefinition.
QString name() const
Returns the name of the parameter.
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...
static QList< QgsVectorTileWriter::Layer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsVectorTileWriter::Layer variantMapAsLayer(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
static QVariantMap layerAsVariantMap(const QgsVectorTileWriter::Layer &layer)
Converts a single input layer to QVariant representation (a QVariantMap).
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterVectorTileWriterLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingParameterVectorTileWriterLayers.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonOutputType::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
static QString typeName()
Returns the type name for the parameter class.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Represents a vector layer which manages a vector based dataset.
Configuration of a single input vector layer to be included in the output.
QString layerName() const
Returns layer name in the output. If not set, layer()->name() will be used.
void setLayerName(const QString &name)
Sets layer name in the output. If not set, layer()->name() will be used.
QgsVectorLayer * layer() const
Returns vector layer of this entry.
void setMaxZoom(int maxzoom)
Sets maximum zoom level at which this layer will be used. Negative value means no max....
void setFilterExpression(const QString &expr)
Sets filter expression. If not empty, only features matching the expression will be used.
void setMinZoom(int minzoom)
Sets minimum zoom level at which this layer will be used. Negative value means no min....
QString filterExpression() const
Returns filter expression. If not empty, only features matching the expression will be used.
int maxZoom() const
Returns maximum zoom level at which this layer will be used. Negative value means no max....
int minZoom() const
Returns minimum zoom level at which this layer will be used. Negative value means no min....