QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsprocessingparameteralignrasterlayers.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparameteralignrasterlayers.cpp
3 ---------------------
4 Date : July 2023
5 Copyright : (C) 2023 by Alexander Bruy
6 Email : alexander dot bruy 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#include "qgsrasterlayer.h"
18#include "qgis.h"
19
20
22 : QgsProcessingParameterDefinition( name, description, QVariant(), false )
23{
24}
25
30
32{
33 return typeName();
34}
35
37{
38 if ( !input.isValid() )
40
41 if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
42 {
43 return true;
44 }
45
46 if ( input.userType() == QMetaType::Type::QString )
47 {
48 if ( input.toString().isEmpty() )
50
51 if ( !context )
52 return true;
53
54 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
55 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Raster );
56 }
57 else if ( input.userType() == QMetaType::Type::QVariantList )
58 {
59 if ( input.toList().isEmpty() )
61
62 const QVariantList layerList = input.toList();
63 for ( const QVariant &variantLayer : layerList )
64 {
65 if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( variantLayer ) ) )
66 continue;
67
68 if ( variantLayer.userType() == QMetaType::Type::QString )
69 {
70 if ( !context )
71 return true;
72
73 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
74 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Raster )
75 return false;
76 }
77 else if ( variantLayer.userType() == QMetaType::Type::QVariantMap )
78 {
79 const QVariantMap layerMap = variantLayer.toMap();
80
81 if ( !layerMap.contains( QStringLiteral( "inputFile" ) ) && !layerMap.contains( QStringLiteral( "outputFile" ) ) )
82 return false;
83
84 if ( !context )
85 return true;
86
87 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "inputFile" ) ).toString(), *context );
88 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Raster )
89 return false;
90
91 QgsRasterLayer *rasterLayer = static_cast<QgsRasterLayer *>( mapLayer );
92
93 if ( !rasterLayer )
94 return false;
95 }
96 else
97 {
98 return false;
99 }
100 }
101 return true;
102 }
103 else if ( input.userType() == QMetaType::Type::QStringList )
104 {
105 const auto constToStringList = input.toStringList();
106 if ( constToStringList.isEmpty() )
108
109 if ( !context )
110 return true;
111
112 for ( const QString &v : constToStringList )
113 {
114 if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
115 return false;
116 }
117 return true;
118 }
119
120 return false;
121}
122
124{
125 QStringList parts;
126 const QList<QgsAlignRasterData::RasterItem> items = parameterAsItems( value, context );
127 for ( const QgsAlignRasterData::RasterItem &item : items )
128 {
129 QStringList layerDefParts;
130 layerDefParts << QStringLiteral( "'inputFile': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( item.inputFilename ) );
131 layerDefParts << QStringLiteral( "'outputFile': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( item.outputFilename ) );
132 layerDefParts << QStringLiteral( "'resampleMethod': " ) + QgsProcessingUtils::variantToPythonLiteral( static_cast<int>( item.resampleMethod ) );
133 layerDefParts << QStringLiteral( "'rescale': " ) + QgsProcessingUtils::variantToPythonLiteral( item.rescaleValues );
134
135 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
136 parts << layerDef;
137 }
138 return parts.join( ',' ).prepend( '[' ).append( ']' );
139}
140
142{
143 switch ( outputType )
144 {
146 {
147 QString code = QStringLiteral( "QgsProcessingParameterAlignRasterLayers('%1', %2)" )
149 return code;
150 }
151 }
152 return QString();
153}
154
155QString QgsProcessingParameterAlignRasterLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
156{
158}
159
164
165QList<QgsAlignRasterData::RasterItem> QgsProcessingParameterAlignRasterLayers::parameterAsItems( const QVariant &layersVariant, QgsProcessingContext &context )
166{
167 QList<QgsAlignRasterData::RasterItem> items;
168
169 if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
170 {
171 QVariantMap vm;
172 vm["inputFile"] = layersVariant;
173 items << variantMapAsItem( vm, context );
174 }
175
176 if ( layersVariant.userType() == QMetaType::Type::QString )
177 {
178 QVariantMap vm;
179 vm["inputFile"] = layersVariant;
180 items << variantMapAsItem( vm, context );
181 }
182 else if ( layersVariant.userType() == QMetaType::Type::QVariantList )
183 {
184 const QVariantList layersVariantList = layersVariant.toList();
185 for ( const QVariant &layerItem : layersVariantList )
186 {
187 if ( layerItem.userType() == QMetaType::Type::QVariantMap )
188 {
189 const QVariantMap layerVariantMap = layerItem.toMap();
190 items << variantMapAsItem( layerVariantMap, context );
191 }
192 else if ( layerItem.userType() == QMetaType::Type::QString )
193 {
194 QVariantMap vm;
195 vm["inputFile"] = layerItem;
196 items << variantMapAsItem( vm, context );
197 }
198 }
199 }
200 else if ( layersVariant.userType() == QMetaType::Type::QStringList )
201 {
202 const auto layersStringList = layersVariant.toStringList();
203 for ( const QString &layerItem : layersStringList )
204 {
205 QVariantMap vm;
206 vm["inputFile"] = layerItem;
207 items << variantMapAsItem( vm, context );
208 }
209 }
210 else if ( layersVariant.userType() == QMetaType::Type::QVariantMap )
211 {
212 const QVariantMap layerVariantMap = layersVariant.toMap();
213 items << variantMapAsItem( layerVariantMap, context );
214 }
215
216 return items;
217}
218
220{
221 const QVariant layerVariant = layerVariantMap[ QStringLiteral( "inputFile" ) ];
222
223 QgsRasterLayer *inputLayer = nullptr;
224 if ( ( inputLayer = qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
225 {
226 // good
227 }
228 else if ( ( inputLayer = qobject_cast< QgsRasterLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
229 {
230 // good
231 }
232 else
233 {
234 QgsAlignRasterData::RasterItem item( "", "" );
235 return item;
236 }
237
238 QgsAlignRasterData::RasterItem item( inputLayer->source(), layerVariantMap[ QStringLiteral( "outputFile" ) ].toString() );
239 item.resampleMethod = static_cast<Qgis::GdalResampleAlgorithm>( layerVariantMap.value( QStringLiteral( "resampleMethod" ), 0 ).toInt() );
240 item.rescaleValues = layerVariantMap.value( QStringLiteral( "rescale" ), false ).toBool();
241 return item;
242}
243
245{
246 QVariantMap vm;
247 vm[ QStringLiteral( "inputFile" )] = item.inputFilename;
248 vm[ QStringLiteral( "outputFile" ) ] = item.outputFilename;
249 vm[ QStringLiteral( "resampleMethod" ) ] = static_cast<int>( item.resampleMethod );
250 vm[ QStringLiteral( "rescale" ) ] = item.rescaleValues;
251 return vm;
252}
GdalResampleAlgorithm
Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)
Definition qgis.h:5383
@ Raster
Raster layer.
@ Optional
Parameter is optional.
Base class for all map layer types.
Definition qgsmaplayer.h:76
QString source() const
Returns the source for the layer.
Qgis::LayerType type
Definition qgsmaplayer.h:86
Contains information about the context in which a processing algorithm is executed.
A parameter for Processing algorithms that need a list of input raster layers to align - this paramet...
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for 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 QString typeName()
Returns the type name for the parameter class.
static QgsAlignRasterData::RasterItem variantMapAsItem(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
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...
QString type() const override
Unique parameter type name.
QgsProcessingParameterAlignRasterLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingParameterAlignRasterLayers.
static QVariantMap itemAsVariantMap(const QgsAlignRasterData::RasterItem &item)
Converts a single input layer to QVariant representation (a QVariantMap)
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QList< QgsAlignRasterData::RasterItem > parameterAsItems(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
Base class for the definition of processing parameters.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
@ AllowMapLayerValues
Enable map layer value handling.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
QVariant valueAsJsonObjectPrivate(const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags) const
Internal method for evaluating values as JSON objects.
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 raster layer.
Definition of one raster layer for alignment.
Qgis::GdalResampleAlgorithm resampleMethod
resampling method to be used
bool rescaleValues
rescaling of values according to the change of pixel size
QString inputFilename
filename of the source raster
QString outputFilename
filename of the newly created aligned raster (will be overwritten if exists already)