QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
28
33
38
40{
41 if ( !input.isValid() )
43
44 if ( input.userType() != QMetaType::Type::QVariantList )
45 return false;
46
47 const QVariantList inputList = input.toList();
48 for ( const QVariant &inputItem : inputList )
49 {
50 if ( inputItem.userType() != QMetaType::Type::QVariantMap )
51 return false;
52 QVariantMap inputItemMap = inputItem.toMap();
53
54 // "layer" is required - pointing to a vector layer
55 if ( !inputItemMap.contains( "layer" ) )
56 return false;
57
58 const QVariant inputItemLayer = inputItemMap["layer"];
59
60 if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( inputItemLayer ) ) )
61 continue;
62
63 if ( !context )
64 continue; // when called without context, we will skip checking whether the layer can be resolved
65
66 if ( !QgsProcessingUtils::mapLayerFromString( inputItemLayer.toString(), *context ) )
67 return false;
68 }
69
70 return true;
71}
72
74{
75 QStringList parts;
76 const QList<QgsVectorTileWriter::Layer> layers = parameterAsLayers( value, context );
77 for ( const QgsVectorTileWriter::Layer &layer : layers )
78 {
79 QStringList layerDefParts;
80 layerDefParts << u"'layer': "_s + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
81 if ( !layer.filterExpression().isEmpty() )
82 layerDefParts << u"'filterExpression': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.filterExpression() );
83 if ( !layer.layerName().isEmpty() )
84 layerDefParts << u"'layerName': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.layerName() );
85 if ( layer.minZoom() >= 0 )
86 layerDefParts << u"'minZoom': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.minZoom() );
87 if ( layer.maxZoom() >= 0 )
88 layerDefParts << u"'maxZoom': "_s + QgsProcessingUtils::variantToPythonLiteral( layer.maxZoom() );
89
90 const QString layerDef = u"{ %1 }"_s.arg( layerDefParts.join( ',' ) );
91 parts << layerDef;
92 }
93 return parts.join( ',' ).prepend( '[' ).append( ']' );
94}
95
97{
98 switch ( outputType )
99 {
101 {
102 QString code = u"QgsProcessingParameterVectorTileWriterLayers('%1', %2)"_s
104 return code;
105 }
106 }
107 return QString();
108}
109
110QList<QgsVectorTileWriter::Layer> QgsProcessingParameterVectorTileWriterLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
111{
112 QList<QgsVectorTileWriter::Layer> layers;
113 const QVariantList layersVariantList = layersVariant.toList();
114 for ( const QVariant &layerItem : layersVariantList )
115 {
116 const QVariantMap layerVariantMap = layerItem.toMap();
117 layers << variantMapAsLayer( layerVariantMap, context );
118 }
119 return layers;
120}
121
123{
124 const QVariant layerVariant = layerVariantMap["layer"];
125
126 QgsVectorLayer *inputLayer = nullptr;
127 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
128 {
129 // good
130 }
131 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
132 {
133 // good
134 }
135 else
136 {
137 // bad
138 }
139
140 QgsVectorTileWriter::Layer writerLayer( inputLayer );
141 if ( layerVariantMap.contains( "filterExpression" ) )
142 writerLayer.setFilterExpression( layerVariantMap["filterExpression"].toString() );
143 if ( layerVariantMap.contains( "minZoom" ) )
144 writerLayer.setMinZoom( layerVariantMap["minZoom"].toInt() );
145 if ( layerVariantMap.contains( "maxZoom" ) )
146 writerLayer.setMaxZoom( layerVariantMap["maxZoom"].toInt() );
147 if ( layerVariantMap.contains( "layerName" ) )
148 writerLayer.setLayerName( layerVariantMap["layerName"].toString() );
149
150 return writerLayer;
151}
152
154{
155 QVariantMap vm;
156 if ( !layer.layer() )
157 return vm;
158
159 vm["layer"] = layer.layer()->id();
160 vm["minZoom"] = layer.minZoom();
161 vm["maxZoom"] = layer.maxZoom();
162 vm["layerName"] = layer.layerName();
163 vm["filterExpression"] = layer.filterExpression();
164 return vm;
165}
@ Optional
Parameter is optional.
Definition qgis.h:3836
QString id
Definition qgsmaplayer.h:86
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....