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