QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsprocessingalgorithm.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingalgorithm.cpp
3  --------------------------
4  begin : December 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson 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 
18 #include "qgsprocessingalgorithm.h"
19 #include "qgsapplication.h"
20 #include "qgsprocessingprovider.h"
22 #include "qgsprocessingoutputs.h"
23 #include "qgsrectangle.h"
24 #include "qgsprocessingcontext.h"
25 #include "qgsprocessingutils.h"
26 #include "qgsexception.h"
27 #include "qgsmessagelog.h"
28 #include "qgsprocessingfeedback.h"
29 
31 {
32  qDeleteAll( mParameters );
33  qDeleteAll( mOutputs );
34 }
35 
36 QgsProcessingAlgorithm *QgsProcessingAlgorithm::create( const QVariantMap &configuration ) const
37 {
38  std::unique_ptr< QgsProcessingAlgorithm > creation( createInstance() );
39  creation->setProvider( provider() );
40  creation->initAlgorithm( configuration );
41  return creation.release();
42 }
43 
45 {
46  if ( mProvider )
47  return QStringLiteral( "%1:%2" ).arg( mProvider->id(), name() );
48  else
49  return name();
50 }
51 
53 {
54  return QString();
55 }
56 
58 {
59  return QString();
60 }
61 
63 {
64  return QString();
65 }
66 
68 {
69  return QString();
70 }
71 
73 {
74  return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
75 }
76 
78 {
79  return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
80 }
81 
82 QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
83 {
85 }
86 
87 bool QgsProcessingAlgorithm::canExecute( QString * ) const
88 {
89  return true;
90 }
91 
92 bool QgsProcessingAlgorithm::checkParameterValues( const QVariantMap &parameters, QgsProcessingContext &context, QString *message ) const
93 {
94  for ( const QgsProcessingParameterDefinition *def : mParameters )
95  {
96  if ( !def->checkValueIsAcceptable( parameters.value( def->name() ), &context ) )
97  {
98  if ( message )
99  {
100  // TODO QGIS 4 - move the message handling to the parameter subclasses (but this
101  // requires a change in signature for the virtual checkValueIsAcceptable method)
102  if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
103  *message = invalidSourceError( parameters, def->name() );
104  else if ( def->type() == QgsProcessingParameterFeatureSink::typeName() )
105  *message = invalidSinkError( parameters, def->name() );
106  else if ( def->type() == QgsProcessingParameterRasterLayer::typeName() )
107  *message = invalidRasterError( parameters, def->name() );
108  else
109  *message = QObject::tr( "Incorrect parameter value for %1" ).arg( def->name() );
110  }
111  return false;
112  }
113  }
114  return true;
115 }
116 
117 QVariantMap QgsProcessingAlgorithm::preprocessParameters( const QVariantMap &parameters )
118 {
119  return parameters;
120 }
121 
123 {
124  return mProvider;
125 }
126 
128 {
129  mProvider = provider;
130 
131  if ( mProvider && !mProvider->supportsNonFileBasedOutput() )
132  {
133  // need to update all destination parameters to turn off non file based outputs
134  for ( const QgsProcessingParameterDefinition *definition : qgis::as_const( mParameters ) )
135  {
136  if ( definition->isDestination() )
137  {
138  const QgsProcessingDestinationParameter *destParam = static_cast< const QgsProcessingDestinationParameter *>( definition );
139  const_cast< QgsProcessingDestinationParameter *>( destParam )->setSupportsNonFileBasedOutput( false );
140  }
141  }
142  }
143 }
144 
146 {
147  return nullptr;
148 }
149 
151  QgsProcessingContext &context, QgsProcessingFeatureSource *source ) const
152 {
153  // start with context's expression context
155 
156  // If there's a source capable of generating a context scope, use it
157  if ( source )
158  {
160  if ( scope )
161  c << scope;
162  }
163  else if ( c.scopeCount() == 0 )
164  {
165  //empty scope, populate with initial scopes
168  }
169 
170  c << QgsExpressionContextUtils::processingAlgorithmScope( this, parameters, context );
171  return c;
172 }
173 
174 bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, QgsProcessingContext &context ) const
175 {
176  if ( !( flags() & FlagRequiresMatchingCrs ) )
177  {
178  // I'm a well behaved algorithm - I take work AWAY from users!
179  return true;
180  }
181 
182  bool foundCrs = false;
184  for ( const QgsProcessingParameterDefinition *def : mParameters )
185  {
187  {
188  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( def, parameters, context );
189  if ( layer )
190  {
191  if ( foundCrs && layer->crs().isValid() && crs != layer->crs() )
192  {
193  return false;
194  }
195  else if ( !foundCrs && layer->crs().isValid() )
196  {
197  foundCrs = true;
198  crs = layer->crs();
199  }
200  }
201  }
202  else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
203  {
204  std::unique_ptr< QgsFeatureSource > source( QgsProcessingParameters::parameterAsSource( def, parameters, context ) );
205  if ( source )
206  {
207  if ( foundCrs && source->sourceCrs().isValid() && crs != source->sourceCrs() )
208  {
209  return false;
210  }
211  else if ( !foundCrs && source->sourceCrs().isValid() )
212  {
213  foundCrs = true;
214  crs = source->sourceCrs();
215  }
216  }
217  }
218  else if ( def->type() == QgsProcessingParameterMultipleLayers::typeName() )
219  {
220  QList< QgsMapLayer *> layers = QgsProcessingParameters::parameterAsLayerList( def, parameters, context );
221  Q_FOREACH ( QgsMapLayer *layer, layers )
222  {
223  if ( !layer )
224  continue;
225 
226  if ( foundCrs && layer->crs().isValid() && crs != layer->crs() )
227  {
228  return false;
229  }
230  else if ( !foundCrs && layer->crs().isValid() )
231  {
232  foundCrs = true;
233  crs = layer->crs();
234  }
235  }
236  }
237  else if ( def->type() == QgsProcessingParameterExtent::typeName() )
238  {
239  QgsCoordinateReferenceSystem extentCrs = QgsProcessingParameters::parameterAsExtentCrs( def, parameters, context );
240  if ( foundCrs && extentCrs.isValid() && crs != extentCrs )
241  {
242  return false;
243  }
244  else if ( !foundCrs && extentCrs.isValid() )
245  {
246  foundCrs = true;
247  crs = extentCrs;
248  }
249  }
250  else if ( def->type() == QgsProcessingParameterPoint::typeName() )
251  {
252  QgsCoordinateReferenceSystem pointCrs = QgsProcessingParameters::parameterAsPointCrs( def, parameters, context );
253  if ( foundCrs && pointCrs.isValid() && crs != pointCrs )
254  {
255  return false;
256  }
257  else if ( !foundCrs && pointCrs.isValid() )
258  {
259  foundCrs = true;
260  crs = pointCrs;
261  }
262  }
263  }
264  return true;
265 }
266 
267 QString QgsProcessingAlgorithm::asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const
268 {
269  QString s = QStringLiteral( "processing.run(\"%1\"," ).arg( id() );
270 
271  QStringList parts;
272  for ( const QgsProcessingParameterDefinition *def : mParameters )
273  {
275  continue;
276 
277  if ( !parameters.contains( def->name() ) )
278  continue;
279 
280  parts << QStringLiteral( "'%1':%2" ).arg( def->name(), def->valueAsPythonString( parameters.value( def->name() ), context ) );
281  }
282 
283  s += QStringLiteral( " {%1})" ).arg( parts.join( ',' ) );
284  return s;
285 }
286 
288 {
289  if ( !definition )
290  return false;
291 
292  // check for duplicate named parameters
294  if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
295  {
296  QgsMessageLog::logMessage( QObject::tr( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
297  delete definition;
298  return false;
299  }
300 
301  if ( definition->isDestination() && mProvider )
302  {
303  QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition );
304  if ( !mProvider->supportsNonFileBasedOutput() )
305  destParam->setSupportsNonFileBasedOutput( false );
306  }
307 
308  mParameters << definition;
309  definition->mAlgorithm = this;
310 
311  if ( createOutput )
312  return createAutoOutputForParameter( definition );
313  else
314  return true;
315 }
316 
318 {
320  if ( def )
321  {
322  delete def;
323  mParameters.removeAll( def );
324  }
325 }
326 
328 {
329  if ( !definition )
330  return false;
331 
332  // check for duplicate named outputs
333  if ( QgsProcessingAlgorithm::outputDefinition( definition->name() ) )
334  {
335  QgsMessageLog::logMessage( QObject::tr( "Duplicate output %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
336  delete definition;
337  return false;
338  }
339 
340  mOutputs << definition;
341  return true;
342 }
343 
345 {
346  return true;
347 }
348 
350 {
351  return QVariantMap();
352 }
353 
355 {
356  // first pass - case sensitive match
357  for ( const QgsProcessingParameterDefinition *def : mParameters )
358  {
359  if ( def->name() == name )
360  return def;
361  }
362 
363  // second pass - case insensitive
364  for ( const QgsProcessingParameterDefinition *def : mParameters )
365  {
366  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
367  return def;
368  }
369  return nullptr;
370 }
371 
373 {
374  int count = 0;
375  for ( const QgsProcessingParameterDefinition *def : mParameters )
376  {
377  if ( !( def->flags() & QgsProcessingParameterDefinition::FlagHidden ) )
378  count++;
379  }
380  return count;
381 }
382 
384 {
386  for ( const QgsProcessingParameterDefinition *def : mParameters )
387  {
388  if ( def->isDestination() )
389  result << def;
390  }
391  return result;
392 }
393 
395 {
396  for ( const QgsProcessingOutputDefinition *def : mOutputs )
397  {
398  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
399  return def;
400  }
401  return nullptr;
402 }
403 
405 {
406  for ( const QgsProcessingOutputDefinition *def : mOutputs )
407  {
408  if ( def->type() == QStringLiteral( "outputHtml" ) )
409  return true;
410  }
411  return false;
412 }
413 
414 QVariantMap QgsProcessingAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok, const QVariantMap &configuration ) const
415 {
416  std::unique_ptr< QgsProcessingAlgorithm > alg( create( configuration ) );
417  if ( ok )
418  *ok = false;
419 
420  bool res = alg->prepare( parameters, context, feedback );
421  if ( !res )
422  return QVariantMap();
423 
424  QVariantMap runRes;
425  try
426  {
427  runRes = alg->runPrepared( parameters, context, feedback );
428  }
429  catch ( QgsProcessingException &e )
430  {
431  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
432  feedback->reportError( e.what() );
433  return QVariantMap();
434  }
435 
436  if ( ok )
437  *ok = true;
438 
439  QVariantMap ppRes = alg->postProcess( context, feedback );
440  if ( !ppRes.isEmpty() )
441  return ppRes;
442  else
443  return runRes;
444 }
445 
446 bool QgsProcessingAlgorithm::prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
447 {
448  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::prepare", "prepare() must be called from the same thread as context was created in" );
449  Q_ASSERT_X( !mHasPrepared, "QgsProcessingAlgorithm::prepare", "prepare() has already been called for the algorithm instance" );
450  try
451  {
452  mHasPrepared = prepareAlgorithm( parameters, context, feedback );
453  return mHasPrepared;
454  }
455  catch ( QgsProcessingException &e )
456  {
457  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
458  feedback->reportError( e.what() );
459  return false;
460  }
461 }
462 
463 QVariantMap QgsProcessingAlgorithm::runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
464 {
465  Q_ASSERT_X( mHasPrepared, "QgsProcessingAlgorithm::runPrepared", QStringLiteral( "prepare() was not called for the algorithm instance %1" ).arg( name() ).toLatin1() );
466  Q_ASSERT_X( !mHasExecuted, "QgsProcessingAlgorithm::runPrepared", "runPrepared() was already called for this algorithm instance" );
467 
468  // Hey kids, let's all be thread safe! It's the fun thing to do!
469  //
470  // First, let's see if we're going to run into issues.
471  QgsProcessingContext *runContext = nullptr;
472  if ( context.thread() == QThread::currentThread() )
473  {
474  // OH. No issues. Seems you're running everything in the same thread, so go about your business. Sorry about
475  // the intrusion, we're just making sure everything's nice and safe here. We like to keep a clean and tidy neighbourhood,
476  // you know, for the kids and dogs and all.
477  runContext = &context;
478  }
479  else
480  {
481  // HA! I knew things looked a bit suspicious - seems you're running this algorithm in a different thread
482  // from that which the passed context has an affinity for. That's fine and all, but we need to make sure
483  // we proceed safely...
484 
485  // So first we create a temporary local context with affinity for the current thread
486  mLocalContext.reset( new QgsProcessingContext() );
487  // copy across everything we can safely do from the passed context
488  mLocalContext->copyThreadSafeSettings( context );
489  // and we'll run the actual algorithm processing using the local thread safe context
490  runContext = mLocalContext.get();
491  }
492 
493  try
494  {
495  QVariantMap runResults = processAlgorithm( parameters, *runContext, feedback );
496 
497  mHasExecuted = true;
498  if ( mLocalContext )
499  {
500  // ok, time to clean things up. We need to push the temporary context back into
501  // the thread that the passed context is associated with (we can only push from the
502  // current thread, so we HAVE to do this here)
503  mLocalContext->pushToThread( context.thread() );
504  }
505  return runResults;
506  }
507  catch ( QgsProcessingException & )
508  {
509  if ( mLocalContext )
510  {
511  // see above!
512  mLocalContext->pushToThread( context.thread() );
513  }
514  //rethrow
515  throw;
516  }
517 }
518 
520 {
521  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::postProcess", "postProcess() must be called from the same thread the context was created in" );
522  Q_ASSERT_X( mHasExecuted, "QgsProcessingAlgorithm::postProcess", QStringLiteral( "algorithm instance %1 was not executed" ).arg( name() ).toLatin1() );
523  Q_ASSERT_X( !mHasPostProcessed, "QgsProcessingAlgorithm::postProcess", "postProcess() was already called for this algorithm instance" );
524 
525  if ( mLocalContext )
526  {
527  // algorithm was processed using a temporary thread safe context. So now we need
528  // to take the results from that temporary context, and smash them into the passed
529  // context
530  context.takeResultsFrom( *mLocalContext );
531  // now get lost, we don't need you anymore
532  mLocalContext.reset();
533  }
534 
535  mHasPostProcessed = true;
536  try
537  {
538  return postProcessAlgorithm( context, feedback );
539  }
540  catch ( QgsProcessingException &e )
541  {
542  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
543  feedback->reportError( e.what() );
544  return QVariantMap();
545  }
546 }
547 
548 QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
549 {
550  return QgsProcessingParameters::parameterAsString( parameterDefinition( name ), parameters, context );
551 }
552 
553 QString QgsProcessingAlgorithm::parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
554 {
555  return QgsProcessingParameters::parameterAsExpression( parameterDefinition( name ), parameters, context );
556 }
557 
558 double QgsProcessingAlgorithm::parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
559 {
560  return QgsProcessingParameters::parameterAsDouble( parameterDefinition( name ), parameters, context );
561 }
562 
563 int QgsProcessingAlgorithm::parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
564 {
565  return QgsProcessingParameters::parameterAsInt( parameterDefinition( name ), parameters, context );
566 }
567 
568 int QgsProcessingAlgorithm::parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
569 {
570  return QgsProcessingParameters::parameterAsEnum( parameterDefinition( name ), parameters, context );
571 }
572 
573 QList<int> QgsProcessingAlgorithm::parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
574 {
575  return QgsProcessingParameters::parameterAsEnums( parameterDefinition( name ), parameters, context );
576 }
577 
578 bool QgsProcessingAlgorithm::parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
579 {
580  return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
581 }
582 
583 QgsFeatureSink *QgsProcessingAlgorithm::parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs ) const
584 {
585  return QgsProcessingParameters::parameterAsSink( parameterDefinition( name ), parameters, fields, geometryType, crs, context, destinationIdentifier );
586 }
587 
588 QgsProcessingFeatureSource *QgsProcessingAlgorithm::parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
589 {
590  return QgsProcessingParameters::parameterAsSource( parameterDefinition( name ), parameters, context );
591 }
592 
593 QString QgsProcessingAlgorithm::parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
594 {
595  return QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( parameterDefinition( name ), parameters, context, compatibleFormats, preferredFormat, feedback );
596 }
597 
598 QgsMapLayer *QgsProcessingAlgorithm::parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
599 {
600  return QgsProcessingParameters::parameterAsLayer( parameterDefinition( name ), parameters, context );
601 }
602 
603 QgsRasterLayer *QgsProcessingAlgorithm::parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
604 {
605  return QgsProcessingParameters::parameterAsRasterLayer( parameterDefinition( name ), parameters, context );
606 }
607 
608 QString QgsProcessingAlgorithm::parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
609 {
610  return QgsProcessingParameters::parameterAsOutputLayer( parameterDefinition( name ), parameters, context );
611 }
612 
613 QString QgsProcessingAlgorithm::parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
614 {
615  return QgsProcessingParameters::parameterAsFileOutput( parameterDefinition( name ), parameters, context );
616 }
617 
618 QgsVectorLayer *QgsProcessingAlgorithm::parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
619 {
620  return QgsProcessingParameters::parameterAsVectorLayer( parameterDefinition( name ), parameters, context );
621 }
622 
623 QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
624 {
625  return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
626 }
627 
629 {
630  return QgsProcessingParameters::parameterAsExtentCrs( parameterDefinition( name ), parameters, context );
631 }
632 
633 QgsRectangle QgsProcessingAlgorithm::parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
634 {
635  return QgsProcessingParameters::parameterAsExtent( parameterDefinition( name ), parameters, context, crs );
636 }
637 
639 {
640  return QgsProcessingParameters::parameterAsExtentGeometry( parameterDefinition( name ), parameters, context, crs );
641 }
642 
643 QgsPointXY QgsProcessingAlgorithm::parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
644 {
645  return QgsProcessingParameters::parameterAsPoint( parameterDefinition( name ), parameters, context, crs );
646 }
647 
649 {
650  return QgsProcessingParameters::parameterAsPointCrs( parameterDefinition( name ), parameters, context );
651 }
652 
653 QString QgsProcessingAlgorithm::parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
654 {
655  return QgsProcessingParameters::parameterAsFile( parameterDefinition( name ), parameters, context );
656 }
657 
658 QVariantList QgsProcessingAlgorithm::parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
659 {
660  return QgsProcessingParameters::parameterAsMatrix( parameterDefinition( name ), parameters, context );
661 }
662 
663 QList<QgsMapLayer *> QgsProcessingAlgorithm::parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
664 {
665  return QgsProcessingParameters::parameterAsLayerList( parameterDefinition( name ), parameters, context );
666 }
667 
668 QList<double> QgsProcessingAlgorithm::parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
669 {
670  return QgsProcessingParameters::parameterAsRange( parameterDefinition( name ), parameters, context );
671 }
672 
673 QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
674 {
675  return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, context );
676 }
677 
678 QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameters, const QString &name )
679 {
680  if ( !parameters.contains( name ) )
681  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
682  else
683  {
684  QVariant var = parameters.value( name );
685  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
686  {
688  var = fromVar.source;
689  }
690  if ( var.canConvert<QgsProperty>() )
691  {
692  QgsProperty p = var.value< QgsProperty >();
694  {
695  var = p.staticValue();
696  }
697  }
698  if ( !var.toString().isEmpty() )
699  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
700  else
701  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
702  }
703 }
704 
705 QString QgsProcessingAlgorithm::invalidRasterError( const QVariantMap &parameters, const QString &name )
706 {
707  if ( !parameters.contains( name ) )
708  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
709  else
710  {
711  QVariant var = parameters.value( name );
712  if ( var.canConvert<QgsProperty>() )
713  {
714  QgsProperty p = var.value< QgsProperty >();
716  {
717  var = p.staticValue();
718  }
719  }
720  if ( !var.toString().isEmpty() )
721  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
722  else
723  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
724  }
725 }
726 
727 QString QgsProcessingAlgorithm::invalidSinkError( const QVariantMap &parameters, const QString &name )
728 {
729  if ( !parameters.contains( name ) )
730  return QObject::tr( "Could not create destination layer for %1: no value specified for parameter" ).arg( name );
731  else
732  {
733  QVariant var = parameters.value( name );
734  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
735  {
737  var = fromVar.sink;
738  }
739  if ( var.canConvert<QgsProperty>() )
740  {
741  QgsProperty p = var.value< QgsProperty >();
743  {
744  var = p.staticValue();
745  }
746  }
747  if ( !var.toString().isEmpty() )
748  return QObject::tr( "Could not create destination layer for %1: %2" ).arg( name, var.toString() );
749  else
750  return QObject::tr( "Could not create destination layer for %1: invalid value" ).arg( name );
751  }
752 }
753 
754 bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
755 {
756  if ( !parameter->isDestination() )
757  return true; // nothing created, but nothing went wrong - so return true
758 
759  QgsProcessingDestinationParameter *dest = static_cast< QgsProcessingDestinationParameter * >( parameter );
761  if ( !output )
762  return true; // nothing created - but nothing went wrong - so return true
763 
764  if ( !addOutput( output ) )
765  {
766  // couldn't add output - probably a duplicate name
767  return false;
768  }
769  else
770  {
771  return true;
772  }
773 }
774 
775 
776 //
777 // QgsProcessingFeatureBasedAlgorithm
778 //
779 
780 void QgsProcessingFeatureBasedAlgorithm::initAlgorithm( const QVariantMap &config )
781 {
782  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), inputLayerTypes() ) );
783  initParameters( config );
784  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), outputName(), outputLayerType() ) );
785 }
786 
788 {
789  return QList<int>();
790 }
791 
793 {
795 }
796 
798 {
799  return static_cast<QgsProcessingFeatureSource::Flag>( 0 );
800 }
801 
803 {
804  return inputWkbType;
805 }
806 
808 {
809  return inputFields;
810 }
811 
813 {
814  return inputCrs;
815 }
816 
818 {
819 }
820 
822 {
823  if ( mSource )
824  return mSource->sourceCrs();
825  else
827 }
828 
829 QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
830 {
831  mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
832  if ( !mSource )
833  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
834 
835  QString dest;
836  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
837  outputFields( mSource->fields() ),
838  outputWkbType( mSource->wkbType() ),
839  outputCrs( mSource->sourceCrs() ) ) );
840  if ( !sink )
841  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
842 
843  // prepare expression context for feature iteration
844  QgsExpressionContext prevContext = context.expressionContext();
845  QgsExpressionContext algContext = prevContext;
846 
847  algContext.appendScopes( createExpressionContext( parameters, context, mSource.get() ).takeScopes() );
848  context.setExpressionContext( algContext );
849 
850  long count = mSource->featureCount();
851 
852  QgsFeature f;
853  QgsFeatureIterator it = mSource->getFeatures( request(), sourceFlags() );
854 
855  double step = count > 0 ? 100.0 / count : 1;
856  int current = 0;
857  while ( it.nextFeature( f ) )
858  {
859  if ( feedback->isCanceled() )
860  {
861  break;
862  }
863 
864  context.expressionContext().setFeature( f );
865  const QgsFeatureList transformed = processFeature( f, context, feedback );
866  for ( QgsFeature transformedFeature : transformed )
867  sink->addFeature( transformedFeature, QgsFeatureSink::FastInsert );
868 
869  feedback->setProgress( current * step );
870  current++;
871  }
872 
873  mSource.reset();
874 
875  // probably not necessary - context's aren't usually recycled, but can't hurt
876  context.setExpressionContext( prevContext );
877 
878  QVariantMap outputs;
879  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
880  return outputs;
881 }
882 
884 {
885  return QgsFeatureRequest();
886 }
887 
QgsProperty sink
Sink/layer definition.
static QgsCoordinateReferenceSystem parameterAsCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a coordinate reference system.
virtual QgsFields outputFields(const QgsFields &inputFields) const
Maps the input source fields (inputFields) to corresponding output fields generated by the algorithm...
QList< const QgsProcessingParameterDefinition *> QgsProcessingParameterDefinitions
List of processing parameters.
void setProvider(QgsProcessingProvider *provider)
Associates this algorithm with its provider.
virtual bool prepareAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)
Prepares the algorithm to run using the specified parameters.
QString parameterAsFile(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a file/folder name.
QgsProcessingFeatureSource * parameterAsSource(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a feature source.
virtual QString helpUrl() const
Returns a url pointing to the algorithm&#39;s help page.
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap &parameters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm...
Wrapper for iterator of features from vector data provider or vector layer.
double parameterAsDouble(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static double value.
QVariantMap processAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) override SIP_THROW(QgsProcessingException)
Runs the algorithm using the specified parameters.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
QList< QgsMapLayer * > parameterAsLayerList(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a list of map layers.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:61
QgsCoordinateReferenceSystem parameterAsPointCrs(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value. ...
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
QgsRasterLayer * parameterAsRasterLayer(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a raster layer.
QString id() const
Returns the unique ID for the algorithm, which is a combination of the algorithm provider&#39;s ID and th...
virtual QIcon icon() const
Returns an icon for the algorithm.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
virtual QString helpString() const
Returns a localised help string for the algorithm.
Base class for providing feedback from a processing algorithm.
static QVariantList parameterAsMatrix(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a matrix/table of values.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm...
int countVisibleParameters() const
Returns the number of visible (non-hidden) parameters defined by this algorithm.
static QString invalidRasterError(const QVariantMap &parameters, const QString &name)
Returns a user-friendly string to use as an error when a raster layer input could not be loaded...
QVariantMap postProcess(QgsProcessingContext &context, QgsProcessingFeedback *feedback)
Should be called in the main thread following the completion of runPrepared().
QgsPointXY parameterAsPoint(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Evaluates the parameter with matching name to a point.
QgsExpressionContext createExpressionContext(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source=nullptr) const
Creates an expression context relating to the algorithm.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
static QString typeName()
Returns the type name for the parameter class.
bool hasHtmlOutputs() const
Returns true if this algorithm generates HTML outputs.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
static QList< int > parameterAsEnums(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to list of enum values.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
QVariantList parameterAsMatrix(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a matrix/table of values.
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
QString name() const
Returns the name of the output.
A class to represent a 2D point.
Definition: qgspointxy.h:43
QgsCoordinateReferenceSystem sourceCrs() const
Returns the source&#39;s coordinate reference system.
QList< int > parameterAsEnums(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to list of enum values.
static QString typeName()
Returns the type name for the parameter class.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
An interface for objects which accept features via addFeature(s) methods.
QgsProcessingAlgorithm * mAlgorithm
Pointer to algorithm which owns this parameter.
QgsRectangle parameterAsExtent(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Evaluates the parameter with matching name to a rectangular extent.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users...
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource, modifying results according to the settings in a QgsProcessingContext.
Container of fields for a vector layer.
Definition: qgsfields.h:42
static QgsRasterLayer * parameterAsRasterLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a raster layer.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
static QgsMapLayer * parameterAsLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a map layer.
QgsMapLayer * parameterAsLayer(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a map layer.
Abstract base class for processing providers.
Algorithm requires that all input layers have matching coordinate reference systems.
QThread * thread()
Returns the thread in which the context lives.
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
virtual QString asPythonCommand(const QVariantMap &parameters, QgsProcessingContext &context) const
Returns a Python command string which can be executed to run the algorithm using the specified parame...
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
QStringList parameterAsFields(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a list of fields.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
virtual QList< int > inputLayerTypes() const
Returns the valid input layer types for the source layer for this algorithm.
virtual QString shortHelpString() const
Returns a localised short helper string for the algorithm.
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
Base class for all parameter definitions which represent file or layer destinations, e.g.
Abstract base class for processing algorithms.
QgsCoordinateReferenceSystem parameterAsCrs(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a coordinate reference system.
static QgsPointXY parameterAsPoint(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a point.
static QString parameterAsFile(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file/folder name.
A feature sink output for processing algorithms.
static QList< QgsMapLayer * > parameterAsLayerList(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of map layers.
static QString invalidSourceError(const QVariantMap &parameters, const QString &name)
Returns a user-friendly string to use as an error when a source parameter could not be loaded...
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QVariantMap runPrepared(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)
Runs the algorithm, which has been prepared by an earlier call to prepare().
QString what() const
Definition: qgsexception.h:48
static QString typeName()
Returns the type name for the parameter class.
static QgsGeometry parameterAsExtentGeometry(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent, and returns a geometry cove...
static QgsCoordinateReferenceSystem parameterAsExtentCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an extent parameter value.
QgsProcessingParameterDefinitions destinationParameterDefinitions() const
Returns a list of destination parameters definitions utilized by the algorithm.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
static QgsRectangle parameterAsExtent(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a rectangular extent.
virtual QVariantMap preprocessParameters(const QVariantMap &parameters)
Pre-processes a set of parameters, allowing the algorithm to clean their values.
void removeParameter(const QString &name)
Removes the parameter with matching name from the algorithm, and deletes any existing definition...
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value. ...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
static QString invalidSinkError(const QVariantMap &parameters, const QString &name)
Returns a user-friendly string to use as an error when a sink parameter could not be created...
static QString typeName()
Returns the type name for the parameter class.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution...
QgsProperty source
Source definition.
Type propertyType() const
Returns the property type.
bool prepare(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback)
Prepares the algorithm for execution.
QgsCoordinateReferenceSystem parameterAsExtentCrs(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context)
Returns the coordinate reference system associated with an extent parameter value.
virtual QWidget * createCustomParametersWidget(QWidget *parent=nullptr) const
If an algorithm subclass implements a custom parameters widget, a copy of this widget should be const...
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
int scopeCount() const
Returns the number of scopes contained in the context.
static QString typeName()
Returns the type name for the parameter class.
bool parameterAsBool(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static boolean value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual QVariantMap postProcessAlgorithm(QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)
Allows the algorithm to perform any required cleanup tasks.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void takeResultsFrom(QgsProcessingContext &context)
Takes the results from another context and merges them with the results currently stored in this cont...
virtual QgsWkbTypes::Type outputWkbType(QgsWkbTypes::Type inputWkbType) const
Maps the input WKB geometry type (inputWkbType) to the corresponding output WKB type generated by the...
virtual QgsFeatureRequest request() const
Returns the feature request used for fetching features to process from the source layer...
static QList< double > parameterAsRange(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a range of values.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
virtual bool validateInputCrs(const QVariantMap &parameters, QgsProcessingContext &context) const
Checks whether the coordinate reference systems for the specified set of parameters are valid for the...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
QgsVectorLayer * parameterAsVectorLayer(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a vector layer.
QString parameterAsString(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static string value.
Single scope for storing variables and functions for use within a QgsExpressionContext.
int parameterAsInt(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static integer value.
A store for object properties.
Definition: qgsproperty.h:229
virtual bool checkParameterValues(const QVariantMap &parameters, QgsProcessingContext &context, QString *message=nullptr) const
Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm i...
virtual QgsProcessingAlgorithm * createInstance() const =0
Creates a new instance of the algorithm class.
QString parameterAsExpression(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to an expression.
QgsExpressionContext & expressionContext()
Returns the expression context.
const QgsProcessingParameterDefinition * parameterDefinition(const QString &name) const
Returns a matching parameter by name.
QgsGeometry parameterAsExtentGeometry(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching name to a rectangular extent, and returns a geometry covering t...
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
QString name() const
Returns the name of the parameter.
QgsExpressionContextScope * createExpressionContextScope() const
Returns an expression context scope suitable for this source.
Encapsulates settings relating to a feature source input to a processing algorithm.
const QgsProcessingOutputDefinition * outputDefinition(const QString &name) const
Returns a matching output by name.
virtual bool isDestination() const
Returns true if this parameter represents a file or layer destination, e.g.
Base class for the definition of processing outputs.
virtual QVariantMap processAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)=0
Runs the algorithm using the specified parameters.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
bool addOutput(QgsProcessingOutputDefinition *outputDefinition)
Adds an output definition to the algorithm.
QgsFeatureSink * parameterAsSink(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Evaluates the parameter with matching name to a feature sink.
virtual bool canExecute(QString *errorMessage=nullptr) const
Returns true if the algorithm can execute.
QString parameterAsOutputLayer(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a output layer destination.
An input feature source (such as vector layers) parameter for processing algorithms.
static QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier)
Evaluates the parameter with matching definition to a feature sink.
This class represents a coordinate reference system (CRS).
virtual QString shortDescription() const
Returns an optional translated short description of the algorithm.
Base class for the definition of processing parameters.
virtual QgsProcessingFeatureSource::Flag sourceFlags() const
Returns the processing feature source flags to be used in the algorithm.
QVariant staticValue() const
Returns the current static value for the property.
static double parameterAsDouble(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static double value.
void initAlgorithm(const QVariantMap &configuration=QVariantMap()) override
Initializes the algorithm using the specified configuration.
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
static int parameterAsInt(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static integer value.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingFeatureSource * parameterAsSource(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a feature source.
virtual void initParameters(const QVariantMap &configuration=QVariantMap())
Initializes any extra parameters added by the algorithm subclass.
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
void setSupportsNonFileBasedOutput(bool supportsNonFileBasedOutput)
Sets whether the destination parameter supports non filed-based outputs, such as memory layers or dir...
static QString typeName()
Returns the type name for the parameter class.
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
bool nextFeature(QgsFeature &f)
QString parameterAsCompatibleSourceLayerPath(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr)
Evaluates the parameter with matching name to a source vector layer file path of compatible format...
static QString parameterAsCompatibleSourceLayerPath(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path of compatible for...
Parameter is hidden and should not be shown to users.
bool addParameter(QgsProcessingParameterDefinition *parameterDefinition, bool createOutput=true)
Adds a parameter definition to the algorithm.
QList< double > parameterAsRange(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a range of values.
Represents a vector layer which manages a vector based data sets.
static QString parameterAsExpression(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to an expression.
Invalid (not set) property.
Definition: qgsproperty.h:237
Contains information about the context in which a processing algorithm is executed.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
virtual QgsProcessingOutputDefinition * toOutputDefinition() const =0
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
Any vector layer with geometry.
Definition: qgsprocessing.h:47
virtual QgsCoordinateReferenceSystem outputCrs(const QgsCoordinateReferenceSystem &inputCrs) const
Maps the input source coordinate reference system (inputCrs) to a corresponding output CRS generated ...
virtual QgsProcessing::SourceType outputLayerType() const
Returns the layer type for layers generated by this algorithm, if this is possible to determine in ad...
QVariantMap run(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok=nullptr, const QVariantMap &configuration=QVariantMap()) const
Executes the algorithm using the specified parameters.
virtual QString svgIconPath() const
Returns a path to an SVG version of the algorithm&#39;s icon.
QString parameterAsFileOutput(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a file based output destination.
int parameterAsEnum(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a enum value.
static QString parameterAsFileOutput(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file based output destination.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
static QStringList parameterAsFields(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of fields.