QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmrastercalculator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrastercalculator.cpp
3 ---------------------
4 begin : July 2023
5 copyright : (C) 2023 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsrastercalculator.h"
21#include "qgsrasterfilewriter.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29Qgis::ProcessingAlgorithmFlags QgsRasterCalculatorAlgorithm::flags() const
30{
32}
33
34QString QgsRasterCalculatorAlgorithm::name() const
35{
36 return u"rastercalc"_s;
37}
38
39QString QgsRasterCalculatorAlgorithm::displayName() const
40{
41 return QObject::tr( "Raster calculator" );
42}
43
44QStringList QgsRasterCalculatorAlgorithm::tags() const
45{
46 return QObject::tr( "raster,calculator" ).split( ',' );
47}
48
49QString QgsRasterCalculatorAlgorithm::group() const
50{
51 return QObject::tr( "Raster analysis" );
52}
53
54QString QgsRasterCalculatorAlgorithm::groupId() const
55{
56 return u"rasteranalysis"_s;
57}
58
59QString QgsRasterCalculatorAlgorithm::shortHelpString() const
60{
61 return QObject::tr( "This algorithm performs algebraic operations using raster layers." );
62}
63
64QString QgsRasterCalculatorAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Performs algebraic operations using raster layers." );
67}
68
69QgsRasterCalculatorAlgorithm *QgsRasterCalculatorAlgorithm::createInstance() const
70{
71 return new QgsRasterCalculatorAlgorithm();
72}
73
74void QgsRasterCalculatorAlgorithm::initAlgorithm( const QVariantMap & )
75{
76 addParameter( new QgsProcessingParameterMultipleLayers( u"LAYERS"_s, QObject::tr( "Input layers" ), Qgis::ProcessingSourceType::Raster ) );
77 addParameter( new QgsProcessingParameterExpression( u"EXPRESSION"_s, QObject::tr( "Expression" ), QVariant(), u"LAYERS"_s, false, Qgis::ExpressionType::RasterCalculator ) );
78 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u"EXTENT"_s, QObject::tr( "Output extent" ), QVariant(), true );
79 extentParam->setHelp( QObject::tr( "Extent of the output layer. If not specified, the extent will be the overall extent of all input layers" ) );
80 addParameter( extentParam.release() );
81 auto cellSizeParam = std::make_unique<
82 QgsProcessingParameterNumber>( u"CELL_SIZE"_s, QObject::tr( "Output cell size (leave empty to set automatically)" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 0.0 );
83 cellSizeParam->setHelp( QObject::tr( "Cell size of the output layer. If not specified, the smallest cell size from the input layers will be used" ) );
84 addParameter( cellSizeParam.release() );
85 auto crsParam = std::make_unique<QgsProcessingParameterCrs>( u"CRS"_s, QObject::tr( "Output CRS" ), QVariant(), true );
86 crsParam->setHelp( QObject::tr( "CRS of the output layer. If not specified, the CRS of the first input layer will be used" ) );
87 addParameter( crsParam.release() );
88
89 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
90 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
91 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
92 addParameter( creationOptsParam.release() );
93
94 addParameter( new QgsProcessingParameterRasterDestination( u"OUTPUT"_s, QObject::tr( "Calculated" ) ) );
95}
96
97bool QgsRasterCalculatorAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
98{
99 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u"LAYERS"_s, context );
100
101 for ( const QgsMapLayer *layer : std::as_const( layers ) )
102 {
103 QgsMapLayer *clonedLayer { layer->clone() };
104 clonedLayer->moveToThread( nullptr );
105 mLayers << clonedLayer;
106 }
107
108 if ( mLayers.isEmpty() )
109 {
110 feedback->reportError( QObject::tr( "No layers selected" ), false );
111 return false;
112 }
113
114 return true;
115}
116
117QVariantMap QgsRasterCalculatorAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
118{
119 for ( QgsMapLayer *layer : std::as_const( mLayers ) )
120 {
121 layer->moveToThread( QThread::currentThread() );
122 }
123
125 if ( parameters.value( u"CRS"_s ).isValid() )
126 {
127 crs = parameterAsCrs( parameters, u"CRS"_s, context );
128 }
129 else
130 {
131 crs = mLayers.at( 0 )->crs();
132 }
133
134 QgsRectangle bbox;
135 if ( parameters.value( u"EXTENT"_s ).isValid() )
136 {
137 bbox = parameterAsExtent( parameters, u"EXTENT"_s, context, crs );
138 }
139 else
140 {
141 bbox = QgsProcessingUtils::combineLayerExtents( mLayers, crs, context );
142 }
143
144 double minCellSize = 1e9;
145
146 QVector<QgsRasterCalculatorEntry> entries;
147 for ( QgsMapLayer *layer : mLayers )
148 {
149 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
150 if ( !rLayer )
151 {
152 continue;
153 }
154
155 const int nBands = rLayer->dataProvider()->bandCount();
156 for ( int i = 0; i < nBands; ++i )
157 {
159 entry.ref = u"%1@%2"_s.arg( rLayer->name() ).arg( i + 1 );
160 entry.raster = rLayer;
161 entry.bandNumber = i + 1;
162 entries << entry;
163 }
164
165 QgsRectangle ext = rLayer->extent();
166 if ( rLayer->crs() != crs )
167 {
168 QgsCoordinateTransform ct( rLayer->crs(), crs, context.transformContext() );
169 ext = ct.transformBoundingBox( ext );
170 }
171
172 double cellSize = ( ext.xMaximum() - ext.xMinimum() ) / rLayer->width();
173 if ( cellSize < minCellSize )
174 {
175 minCellSize = cellSize;
176 }
177 }
178
179 double cellSize = parameterAsDouble( parameters, u"CELL_SIZE"_s, context );
180 if ( cellSize == 0 )
181 {
182 cellSize = minCellSize;
183 }
184
185 const QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
186 const QString expression = parameterAsExpression( parameters, u"EXPRESSION"_s, context );
187 const QString outputFile = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
188 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT"_s, context );
189
190 double width = std::round( ( bbox.xMaximum() - bbox.xMinimum() ) / cellSize );
191 double height = std::round( ( bbox.yMaximum() - bbox.yMinimum() ) / cellSize );
192
193 QgsRasterCalculator calc( expression, outputFile, outputFormat, bbox, crs, width, height, entries, context.transformContext() );
194 calc.setCreationOptions( creationOptions.split( '|', Qt::SplitBehaviorFlags::SkipEmptyParts ) );
195 QgsRasterCalculator::Result result = calc.processCalculation( feedback );
196 qDeleteAll( mLayers );
197 mLayers.clear();
198 switch ( result )
199 {
201 throw QgsProcessingException( QObject::tr( "Error creating output file." ) );
203 throw QgsProcessingException( QObject::tr( "Error reading input layer." ) );
205 throw QgsProcessingException( QObject::tr( "Error parsing formula." ) );
207 throw QgsProcessingException( QObject::tr( "Error allocating memory for result." ) );
209 throw QgsProcessingException( QObject::tr( "Invalid band number for input." ) );
211 throw QgsProcessingException( QObject::tr( "Error occurred while performing calculation." ) );
212 default:
213 break;
214 }
215
216 QVariantMap outputs;
217 outputs.insert( u"OUTPUT"_s, outputFile );
218 return outputs;
219}
220
221Qgis::ProcessingAlgorithmFlags QgsRasterCalculatorModelerAlgorithm::flags() const
222{
224}
225
226QString QgsRasterCalculatorModelerAlgorithm::name() const
227{
228 return u"modelerrastercalc"_s;
229}
230
231QString QgsRasterCalculatorModelerAlgorithm::displayName() const
232{
233 return QObject::tr( "Raster calculator" );
234}
235
236QStringList QgsRasterCalculatorModelerAlgorithm::tags() const
237{
238 return QObject::tr( "raster,calculator" ).split( ',' );
239}
240
241QString QgsRasterCalculatorModelerAlgorithm::group() const
242{
243 return QObject::tr( "Raster analysis" );
244}
245
246QString QgsRasterCalculatorModelerAlgorithm::groupId() const
247{
248 return u"rasteranalysis"_s;
249}
250
251QgsRasterCalculatorModelerAlgorithm *QgsRasterCalculatorModelerAlgorithm::createInstance() const
252{
253 return new QgsRasterCalculatorModelerAlgorithm();
254}
255
256QVariantMap QgsRasterCalculatorModelerAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
257{
258 for ( QgsMapLayer *layer : std::as_const( mLayers ) )
259 {
260 layer->moveToThread( QThread::currentThread() );
261 }
262
264 if ( parameters.value( u"CRS"_s ).isValid() )
265 {
266 crs = parameterAsCrs( parameters, u"CRS"_s, context );
267 }
268 else
269 {
270 crs = mLayers.at( 0 )->crs();
271 }
272
273 QgsRectangle bbox;
274 if ( parameters.value( u"EXTENT"_s ).isValid() )
275 {
276 bbox = parameterAsExtent( parameters, u"EXTENT"_s, context, crs );
277 }
278 else
279 {
280 bbox = QgsProcessingUtils::combineLayerExtents( mLayers, crs, context );
281 }
282
283 double minCellSize = 1e9;
284
285 QVector<QgsRasterCalculatorEntry> entries;
286 int n = 0;
287 for ( QgsMapLayer *layer : mLayers )
288 {
289 QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );
290 if ( !rLayer )
291 {
292 continue;
293 }
294
295 n++;
296 const int nBands = rLayer->dataProvider()->bandCount();
297 for ( int i = 0; i < nBands; ++i )
298 {
300 entry.ref = u"%1@%2"_s.arg( indexToName( n ) ).arg( i + 1 );
301 entry.raster = rLayer;
302 entry.bandNumber = i + 1;
303 entries << entry;
304 }
305
306 QgsRectangle ext = rLayer->extent();
307 if ( rLayer->crs() != crs )
308 {
309 QgsCoordinateTransform ct( rLayer->crs(), crs, context.transformContext() );
310 ext = ct.transformBoundingBox( ext );
311 }
312
313 double cellSize = ( ext.xMaximum() - ext.xMinimum() ) / rLayer->width();
314 if ( cellSize < minCellSize )
315 {
316 minCellSize = cellSize;
317 }
318 }
319
320 double cellSize = parameterAsDouble( parameters, u"CELL_SIZE"_s, context );
321 if ( cellSize == 0 )
322 {
323 cellSize = minCellSize;
324 }
325
326 const QString expression = parameterAsExpression( parameters, u"EXPRESSION"_s, context );
327 const QString outputFile = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
328 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT"_s, context );
329
330 double width = std::round( ( bbox.xMaximum() - bbox.xMinimum() ) / cellSize );
331 double height = std::round( ( bbox.yMaximum() - bbox.yMinimum() ) / cellSize );
332
333 QgsRasterCalculator calc( expression, outputFile, outputFormat, bbox, crs, width, height, entries, context.transformContext() );
334 QgsRasterCalculator::Result result = calc.processCalculation( feedback );
335 qDeleteAll( mLayers );
336 mLayers.clear();
337 switch ( result )
338 {
340 throw QgsProcessingException( QObject::tr( "Error creating output file." ) );
342 throw QgsProcessingException( QObject::tr( "Error reading input layer." ) );
344 throw QgsProcessingException( QObject::tr( "Error parsing formula." ) );
346 throw QgsProcessingException( QObject::tr( "Error allocating memory for result." ) );
348 throw QgsProcessingException( QObject::tr( "Invalid band number for input." ) );
350 throw QgsProcessingException( QObject::tr( "Error occurred while performing calculation." ) );
351 default:
352 break;
353 }
354
355 QVariantMap outputs;
356 outputs.insert( u"OUTPUT"_s, outputFile );
357 return outputs;
358}
359
360QString QgsRasterCalculatorModelerAlgorithm::indexToName( int index ) const
361{
362 QString name;
363 int div = index;
364 int mod = 0;
365
366 while ( div > 0 )
367 {
368 mod = ( div - 1 ) % 26;
369 name = static_cast<char>( 65 + mod ) + name;
370 div = ( int ) ( ( div - mod ) / 26 );
371 }
372 return name;
373}
374
@ Raster
Raster layers.
Definition qgis.h:3651
@ RasterCalculator
Raster calculator expression.
Definition qgis.h:5901
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3724
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
Definition qgis.h:3698
@ HideFromModeler
Algorithm should be hidden from the modeler.
Definition qgis.h:3699
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
Base class for all map layer types.
Definition qgsmaplayer.h:83
QString name
Definition qgsmaplayer.h:87
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
void setHelp(const QString &help)
Sets the help for the parameter.
An expression parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
static QgsRectangle combineLayerExtents(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context)
Combines the extent of several map layers.
Represents an individual raster layer/band number entry within a raster calculation.
QgsRasterLayer * raster
Raster layer associated with entry.
int bandNumber
Band number for entry.
QString ref
Name of entry.
Performs raster layer calculations.
Result
Result of the calculation.
@ InputLayerError
Error reading input layer.
@ BandError
Invalid band number for input.
@ CreateOutputError
Error creating output data file.
@ ParserError
Error parsing formula.
@ CalculationError
Error occurred while performing calculation.
@ MemoryError
Error allocating memory for result.
virtual int bandCount() const =0
Gets number of bands.
Represents a raster layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
int width() const
Returns the width of the (unclipped) raster.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum