QGIS API Documentation  3.6.0-Noosa (5873452)
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 "qgsvectorlayer.h"
29 #include "qgsprocessingfeedback.h"
30 #include "qgsmeshlayer.h"
32 
33 
35 {
36  qDeleteAll( mParameters );
37  qDeleteAll( mOutputs );
38 }
39 
40 QgsProcessingAlgorithm *QgsProcessingAlgorithm::create( const QVariantMap &configuration ) const
41 {
42  std::unique_ptr< QgsProcessingAlgorithm > creation( createInstance() );
43  if ( ! creation )
44  throw QgsProcessingException( QObject::tr( "Error creating algorithm from createInstance()" ) );
45  creation->setProvider( provider() );
46  creation->initAlgorithm( configuration );
47  return creation.release();
48 }
49 
51 {
52  if ( mProvider )
53  return QStringLiteral( "%1:%2" ).arg( mProvider->id(), name() );
54  else
55  return name();
56 }
57 
59 {
60  return QString();
61 }
62 
64 {
65  return QString();
66 }
67 
69 {
70  return QString();
71 }
72 
74 {
75  return QString();
76 }
77 
79 {
80  return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
81 }
82 
84 {
85  return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
86 }
87 
88 QgsProcessingAlgorithm::Flags QgsProcessingAlgorithm::flags() const
89 {
91 }
92 
93 bool QgsProcessingAlgorithm::canExecute( QString * ) const
94 {
95  return true;
96 }
97 
98 bool QgsProcessingAlgorithm::checkParameterValues( const QVariantMap &parameters, QgsProcessingContext &context, QString *message ) const
99 {
100  for ( const QgsProcessingParameterDefinition *def : mParameters )
101  {
102  if ( !def->checkValueIsAcceptable( parameters.value( def->name() ), &context ) )
103  {
104  if ( message )
105  {
106  // TODO QGIS 4 - move the message handling to the parameter subclasses (but this
107  // requires a change in signature for the virtual checkValueIsAcceptable method)
108  if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
109  *message = invalidSourceError( parameters, def->name() );
110  else if ( def->type() == QgsProcessingParameterFeatureSink::typeName() )
111  *message = invalidSinkError( parameters, def->name() );
112  else if ( def->type() == QgsProcessingParameterRasterLayer::typeName() )
113  *message = invalidRasterError( parameters, def->name() );
114  else
115  *message = QObject::tr( "Incorrect parameter value for %1" ).arg( def->name() );
116  }
117  return false;
118  }
119  }
120  return true;
121 }
122 
123 QVariantMap QgsProcessingAlgorithm::preprocessParameters( const QVariantMap &parameters )
124 {
125  return parameters;
126 }
127 
129 {
130  return mProvider;
131 }
132 
134 {
135  mProvider = provider;
136 
137  if ( mProvider && !mProvider->supportsNonFileBasedOutput() )
138  {
139  // need to update all destination parameters to turn off non file based outputs
140  for ( const QgsProcessingParameterDefinition *definition : qgis::as_const( mParameters ) )
141  {
142  if ( definition->isDestination() )
143  {
144  const QgsProcessingDestinationParameter *destParam = static_cast< const QgsProcessingDestinationParameter *>( definition );
145  const_cast< QgsProcessingDestinationParameter *>( destParam )->setSupportsNonFileBasedOutput( false );
146  }
147  }
148  }
149 }
150 
152 {
153  return nullptr;
154 }
155 
157  QgsProcessingContext &context, QgsProcessingFeatureSource *source ) const
158 {
159  // start with context's expression context
161 
162  // If there's a source capable of generating a context scope, use it
163  if ( source )
164  {
166  if ( scope )
167  c << scope;
168  }
169  else if ( c.scopeCount() == 0 )
170  {
171  //empty scope, populate with initial scopes
174  }
175 
176  c << QgsExpressionContextUtils::processingAlgorithmScope( this, parameters, context );
177  return c;
178 }
179 
180 bool QgsProcessingAlgorithm::validateInputCrs( const QVariantMap &parameters, QgsProcessingContext &context ) const
181 {
182  if ( !( flags() & FlagRequiresMatchingCrs ) )
183  {
184  // I'm a well behaved algorithm - I take work AWAY from users!
185  return true;
186  }
187 
188  bool foundCrs = false;
190  for ( const QgsProcessingParameterDefinition *def : mParameters )
191  {
193  {
194  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( def, parameters, context );
195  if ( layer )
196  {
197  if ( foundCrs && layer->crs().isValid() && crs != layer->crs() )
198  {
199  return false;
200  }
201  else if ( !foundCrs && layer->crs().isValid() )
202  {
203  foundCrs = true;
204  crs = layer->crs();
205  }
206  }
207  }
208  else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
209  {
210  std::unique_ptr< QgsFeatureSource > source( QgsProcessingParameters::parameterAsSource( def, parameters, context ) );
211  if ( source )
212  {
213  if ( foundCrs && source->sourceCrs().isValid() && crs != source->sourceCrs() )
214  {
215  return false;
216  }
217  else if ( !foundCrs && source->sourceCrs().isValid() )
218  {
219  foundCrs = true;
220  crs = source->sourceCrs();
221  }
222  }
223  }
224  else if ( def->type() == QgsProcessingParameterMultipleLayers::typeName() )
225  {
226  QList< QgsMapLayer *> layers = QgsProcessingParameters::parameterAsLayerList( def, parameters, context );
227  Q_FOREACH ( QgsMapLayer *layer, layers )
228  {
229  if ( !layer )
230  continue;
231 
232  if ( foundCrs && layer->crs().isValid() && crs != layer->crs() )
233  {
234  return false;
235  }
236  else if ( !foundCrs && layer->crs().isValid() )
237  {
238  foundCrs = true;
239  crs = layer->crs();
240  }
241  }
242  }
243  else if ( def->type() == QgsProcessingParameterExtent::typeName() )
244  {
245  QgsCoordinateReferenceSystem extentCrs = QgsProcessingParameters::parameterAsExtentCrs( def, parameters, context );
246  if ( foundCrs && extentCrs.isValid() && crs != extentCrs )
247  {
248  return false;
249  }
250  else if ( !foundCrs && extentCrs.isValid() )
251  {
252  foundCrs = true;
253  crs = extentCrs;
254  }
255  }
256  else if ( def->type() == QgsProcessingParameterPoint::typeName() )
257  {
258  QgsCoordinateReferenceSystem pointCrs = QgsProcessingParameters::parameterAsPointCrs( def, parameters, context );
259  if ( foundCrs && pointCrs.isValid() && crs != pointCrs )
260  {
261  return false;
262  }
263  else if ( !foundCrs && pointCrs.isValid() )
264  {
265  foundCrs = true;
266  crs = pointCrs;
267  }
268  }
269  }
270  return true;
271 }
272 
273 QString QgsProcessingAlgorithm::asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const
274 {
275  QString s = QStringLiteral( "processing.run(\"%1\"," ).arg( id() );
276 
277  QStringList parts;
278  for ( const QgsProcessingParameterDefinition *def : mParameters )
279  {
281  continue;
282 
283  if ( !parameters.contains( def->name() ) )
284  continue;
285 
286  parts << QStringLiteral( "'%1':%2" ).arg( def->name(), def->valueAsPythonString( parameters.value( def->name() ), context ) );
287  }
288 
289  s += QStringLiteral( " {%1})" ).arg( parts.join( ',' ) );
290  return s;
291 }
292 
294 {
295  if ( !definition )
296  return false;
297 
298  // check for duplicate named parameters
300  if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
301  {
302  QgsMessageLog::logMessage( QObject::tr( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
303  delete definition;
304  return false;
305  }
306 
307  if ( definition->isDestination() && mProvider )
308  {
309  QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition );
310  if ( !mProvider->supportsNonFileBasedOutput() )
311  destParam->setSupportsNonFileBasedOutput( false );
312  }
313 
314  mParameters << definition;
315  definition->mAlgorithm = this;
316 
317  if ( createOutput )
318  return createAutoOutputForParameter( definition );
319  else
320  return true;
321 }
322 
324 {
326  if ( def )
327  {
328  delete def;
329  mParameters.removeAll( def );
330  }
331 }
332 
334 {
335  if ( !definition )
336  return false;
337 
338  // check for duplicate named outputs
339  if ( QgsProcessingAlgorithm::outputDefinition( definition->name() ) )
340  {
341  QgsMessageLog::logMessage( QObject::tr( "Duplicate output %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
342  delete definition;
343  return false;
344  }
345 
346  mOutputs << definition;
347  return true;
348 }
349 
351 {
352  return true;
353 }
354 
356 {
357  return QVariantMap();
358 }
359 
361 {
362  // first pass - case sensitive match
363  for ( const QgsProcessingParameterDefinition *def : mParameters )
364  {
365  if ( def->name() == name )
366  return def;
367  }
368 
369  // second pass - case insensitive
370  for ( const QgsProcessingParameterDefinition *def : mParameters )
371  {
372  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
373  return def;
374  }
375  return nullptr;
376 }
377 
379 {
380  int count = 0;
381  for ( const QgsProcessingParameterDefinition *def : mParameters )
382  {
383  if ( !( def->flags() & QgsProcessingParameterDefinition::FlagHidden ) )
384  count++;
385  }
386  return count;
387 }
388 
390 {
392  for ( const QgsProcessingParameterDefinition *def : mParameters )
393  {
394  if ( def->isDestination() )
395  result << def;
396  }
397  return result;
398 }
399 
401 {
402  for ( const QgsProcessingOutputDefinition *def : mOutputs )
403  {
404  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
405  return def;
406  }
407  return nullptr;
408 }
409 
411 {
412  for ( const QgsProcessingOutputDefinition *def : mOutputs )
413  {
414  if ( def->type() == QStringLiteral( "outputHtml" ) )
415  return true;
416  }
417  return false;
418 }
419 
420 QVariantMap QgsProcessingAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok, const QVariantMap &configuration ) const
421 {
422  std::unique_ptr< QgsProcessingAlgorithm > alg( create( configuration ) );
423  if ( ok )
424  *ok = false;
425 
426  bool res = alg->prepare( parameters, context, feedback );
427  if ( !res )
428  return QVariantMap();
429 
430  QVariantMap runRes;
431  try
432  {
433  runRes = alg->runPrepared( parameters, context, feedback );
434  }
435  catch ( QgsProcessingException &e )
436  {
437  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
438  feedback->reportError( e.what() );
439  return QVariantMap();
440  }
441 
442  if ( ok )
443  *ok = true;
444 
445  QVariantMap ppRes = alg->postProcess( context, feedback );
446  if ( !ppRes.isEmpty() )
447  return ppRes;
448  else
449  return runRes;
450 }
451 
452 bool QgsProcessingAlgorithm::prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
453 {
454  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::prepare", "prepare() must be called from the same thread as context was created in" );
455  Q_ASSERT_X( !mHasPrepared, "QgsProcessingAlgorithm::prepare", "prepare() has already been called for the algorithm instance" );
456  try
457  {
458  mHasPrepared = prepareAlgorithm( parameters, context, feedback );
459  return mHasPrepared;
460  }
461  catch ( QgsProcessingException &e )
462  {
463  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
464  feedback->reportError( e.what() );
465  return false;
466  }
467 }
468 
469 QVariantMap QgsProcessingAlgorithm::runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
470 {
471  Q_ASSERT_X( mHasPrepared, "QgsProcessingAlgorithm::runPrepared", QStringLiteral( "prepare() was not called for the algorithm instance %1" ).arg( name() ).toLatin1() );
472  Q_ASSERT_X( !mHasExecuted, "QgsProcessingAlgorithm::runPrepared", "runPrepared() was already called for this algorithm instance" );
473 
474  // Hey kids, let's all be thread safe! It's the fun thing to do!
475  //
476  // First, let's see if we're going to run into issues.
477  QgsProcessingContext *runContext = nullptr;
478  if ( context.thread() == QThread::currentThread() )
479  {
480  // OH. No issues. Seems you're running everything in the same thread, so go about your business. Sorry about
481  // the intrusion, we're just making sure everything's nice and safe here. We like to keep a clean and tidy neighbourhood,
482  // you know, for the kids and dogs and all.
483  runContext = &context;
484  }
485  else
486  {
487  // HA! I knew things looked a bit suspicious - seems you're running this algorithm in a different thread
488  // from that which the passed context has an affinity for. That's fine and all, but we need to make sure
489  // we proceed safely...
490 
491  // So first we create a temporary local context with affinity for the current thread
492  mLocalContext.reset( new QgsProcessingContext() );
493  // copy across everything we can safely do from the passed context
494  mLocalContext->copyThreadSafeSettings( context );
495  // and we'll run the actual algorithm processing using the local thread safe context
496  runContext = mLocalContext.get();
497  }
498 
499  try
500  {
501  QVariantMap runResults = processAlgorithm( parameters, *runContext, feedback );
502 
503  mHasExecuted = true;
504  if ( mLocalContext )
505  {
506  // ok, time to clean things up. We need to push the temporary context back into
507  // the thread that the passed context is associated with (we can only push from the
508  // current thread, so we HAVE to do this here)
509  mLocalContext->pushToThread( context.thread() );
510  }
511  return runResults;
512  }
513  catch ( QgsProcessingException & )
514  {
515  if ( mLocalContext )
516  {
517  // see above!
518  mLocalContext->pushToThread( context.thread() );
519  }
520  //rethrow
521  throw;
522  }
523 }
524 
526 {
527  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::postProcess", "postProcess() must be called from the same thread the context was created in" );
528  Q_ASSERT_X( mHasExecuted, "QgsProcessingAlgorithm::postProcess", QStringLiteral( "algorithm instance %1 was not executed" ).arg( name() ).toLatin1() );
529  Q_ASSERT_X( !mHasPostProcessed, "QgsProcessingAlgorithm::postProcess", "postProcess() was already called for this algorithm instance" );
530 
531  if ( mLocalContext )
532  {
533  // algorithm was processed using a temporary thread safe context. So now we need
534  // to take the results from that temporary context, and smash them into the passed
535  // context
536  context.takeResultsFrom( *mLocalContext );
537  // now get lost, we don't need you anymore
538  mLocalContext.reset();
539  }
540 
541  mHasPostProcessed = true;
542  try
543  {
544  return postProcessAlgorithm( context, feedback );
545  }
546  catch ( QgsProcessingException &e )
547  {
548  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
549  feedback->reportError( e.what() );
550  return QVariantMap();
551  }
552 }
553 
554 QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
555 {
556  return QgsProcessingParameters::parameterAsString( parameterDefinition( name ), parameters, context );
557 }
558 
559 QString QgsProcessingAlgorithm::parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
560 {
561  return QgsProcessingParameters::parameterAsExpression( parameterDefinition( name ), parameters, context );
562 }
563 
564 double QgsProcessingAlgorithm::parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
565 {
566  return QgsProcessingParameters::parameterAsDouble( parameterDefinition( name ), parameters, context );
567 }
568 
569 int QgsProcessingAlgorithm::parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
570 {
571  return QgsProcessingParameters::parameterAsInt( parameterDefinition( name ), parameters, context );
572 }
573 
574 QList<int> QgsProcessingAlgorithm::parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
575 {
576  return QgsProcessingParameters::parameterAsInts( parameterDefinition( name ), parameters, context );
577 }
578 
579 int QgsProcessingAlgorithm::parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
580 {
581  return QgsProcessingParameters::parameterAsEnum( parameterDefinition( name ), parameters, context );
582 }
583 
584 QList<int> QgsProcessingAlgorithm::parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
585 {
586  return QgsProcessingParameters::parameterAsEnums( parameterDefinition( name ), parameters, context );
587 }
588 
589 bool QgsProcessingAlgorithm::parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
590 {
591  return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
592 }
593 
594 QgsFeatureSink *QgsProcessingAlgorithm::parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsFeatureSink::SinkFlags sinkFlags ) const
595 {
596  return QgsProcessingParameters::parameterAsSink( parameterDefinition( name ), parameters, fields, geometryType, crs, context, destinationIdentifier, sinkFlags );
597 }
598 
599 QgsProcessingFeatureSource *QgsProcessingAlgorithm::parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
600 {
601  return QgsProcessingParameters::parameterAsSource( parameterDefinition( name ), parameters, context );
602 }
603 
604 QString QgsProcessingAlgorithm::parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
605 {
606  return QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( parameterDefinition( name ), parameters, context, compatibleFormats, preferredFormat, feedback );
607 }
608 
609 QgsMapLayer *QgsProcessingAlgorithm::parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
610 {
611  return QgsProcessingParameters::parameterAsLayer( parameterDefinition( name ), parameters, context );
612 }
613 
614 QgsRasterLayer *QgsProcessingAlgorithm::parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
615 {
616  return QgsProcessingParameters::parameterAsRasterLayer( parameterDefinition( name ), parameters, context );
617 }
618 
619 QgsMeshLayer *QgsProcessingAlgorithm::parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
620 {
621  return QgsProcessingParameters::parameterAsMeshLayer( parameterDefinition( name ), parameters, context );
622 }
623 
624 QString QgsProcessingAlgorithm::parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
625 {
626  return QgsProcessingParameters::parameterAsOutputLayer( parameterDefinition( name ), parameters, context );
627 }
628 
629 QString QgsProcessingAlgorithm::parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
630 {
631  return QgsProcessingParameters::parameterAsFileOutput( parameterDefinition( name ), parameters, context );
632 }
633 
634 QgsVectorLayer *QgsProcessingAlgorithm::parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
635 {
636  return QgsProcessingParameters::parameterAsVectorLayer( parameterDefinition( name ), parameters, context );
637 }
638 
639 QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
640 {
641  return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
642 }
643 
645 {
646  return QgsProcessingParameters::parameterAsExtentCrs( parameterDefinition( name ), parameters, context );
647 }
648 
649 QgsRectangle QgsProcessingAlgorithm::parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
650 {
651  return QgsProcessingParameters::parameterAsExtent( parameterDefinition( name ), parameters, context, crs );
652 }
653 
655 {
656  return QgsProcessingParameters::parameterAsExtentGeometry( parameterDefinition( name ), parameters, context, crs );
657 }
658 
659 QgsPointXY QgsProcessingAlgorithm::parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
660 {
661  return QgsProcessingParameters::parameterAsPoint( parameterDefinition( name ), parameters, context, crs );
662 }
663 
665 {
666  return QgsProcessingParameters::parameterAsPointCrs( parameterDefinition( name ), parameters, context );
667 }
668 
669 QString QgsProcessingAlgorithm::parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
670 {
671  return QgsProcessingParameters::parameterAsFile( parameterDefinition( name ), parameters, context );
672 }
673 
674 QVariantList QgsProcessingAlgorithm::parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
675 {
676  return QgsProcessingParameters::parameterAsMatrix( parameterDefinition( name ), parameters, context );
677 }
678 
679 QList<QgsMapLayer *> QgsProcessingAlgorithm::parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
680 {
681  return QgsProcessingParameters::parameterAsLayerList( parameterDefinition( name ), parameters, context );
682 }
683 
684 QList<double> QgsProcessingAlgorithm::parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
685 {
686  return QgsProcessingParameters::parameterAsRange( parameterDefinition( name ), parameters, context );
687 }
688 
689 QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
690 {
691  return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, context );
692 }
693 
694 QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameters, const QString &name )
695 {
696  if ( !parameters.contains( name ) )
697  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
698  else
699  {
700  QVariant var = parameters.value( name );
701  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
702  {
704  var = fromVar.source;
705  }
706  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
707  {
709  var = fromVar.sink;
710  }
711  if ( var.canConvert<QgsProperty>() )
712  {
713  QgsProperty p = var.value< QgsProperty >();
715  {
716  var = p.staticValue();
717  }
718  }
719  if ( !var.toString().isEmpty() )
720  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
721  else
722  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
723  }
724 }
725 
726 QString QgsProcessingAlgorithm::invalidRasterError( const QVariantMap &parameters, const QString &name )
727 {
728  if ( !parameters.contains( name ) )
729  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
730  else
731  {
732  QVariant var = parameters.value( name );
733  if ( var.canConvert<QgsProperty>() )
734  {
735  QgsProperty p = var.value< QgsProperty >();
737  {
738  var = p.staticValue();
739  }
740  }
741  if ( !var.toString().isEmpty() )
742  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
743  else
744  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
745  }
746 }
747 
748 QString QgsProcessingAlgorithm::invalidSinkError( const QVariantMap &parameters, const QString &name )
749 {
750  if ( !parameters.contains( name ) )
751  return QObject::tr( "Could not create destination layer for %1: no value specified for parameter" ).arg( name );
752  else
753  {
754  QVariant var = parameters.value( name );
755  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
756  {
758  var = fromVar.sink;
759  }
760  if ( var.canConvert<QgsProperty>() )
761  {
762  QgsProperty p = var.value< QgsProperty >();
764  {
765  var = p.staticValue();
766  }
767  }
768  if ( !var.toString().isEmpty() )
769  return QObject::tr( "Could not create destination layer for %1: %2" ).arg( name, var.toString() );
770  else
771  return QObject::tr( "Could not create destination layer for %1: invalid value" ).arg( name );
772  }
773 }
774 
776 {
777  Q_UNUSED( layer );
778  return false;
779 }
780 
781 
782 bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
783 {
784  if ( !parameter->isDestination() )
785  return true; // nothing created, but nothing went wrong - so return true
786 
787  QgsProcessingDestinationParameter *dest = static_cast< QgsProcessingDestinationParameter * >( parameter );
789  if ( !output )
790  return true; // nothing created - but nothing went wrong - so return true
791 
792  if ( !addOutput( output ) )
793  {
794  // couldn't add output - probably a duplicate name
795  return false;
796  }
797  else
798  {
799  return true;
800  }
801 }
802 
803 
804 //
805 // QgsProcessingFeatureBasedAlgorithm
806 //
807 
808 QgsProcessingAlgorithm::Flags QgsProcessingFeatureBasedAlgorithm::flags() const
809 {
810  Flags f = QgsProcessingAlgorithm::flags();
812  return f;
813 }
814 
815 void QgsProcessingFeatureBasedAlgorithm::initAlgorithm( const QVariantMap &config )
816 {
817  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), inputLayerTypes() ) );
818  initParameters( config );
819  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), outputName(), outputLayerType() ) );
820 }
821 
823 {
824  return QList<int>();
825 }
826 
828 {
830 }
831 
833 {
834  return static_cast<QgsProcessingFeatureSource::Flag>( 0 );
835 }
836 
837 QgsFeatureSink::SinkFlags QgsProcessingFeatureBasedAlgorithm::sinkFlags() const
838 {
839  return nullptr;
840 }
841 
843 {
844  return inputWkbType;
845 }
846 
848 {
849  return inputFields;
850 }
851 
853 {
854  return inputCrs;
855 }
856 
858 {
859 }
860 
862 {
863  if ( mSource )
864  return mSource->sourceCrs();
865  else
867 }
868 
869 QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
870 {
871  prepareSource( parameters, context );
872  QString dest;
873  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
874  outputFields( mSource->fields() ),
875  outputWkbType( mSource->wkbType() ),
876  outputCrs( mSource->sourceCrs() ),
877  sinkFlags() ) );
878  if ( !sink )
879  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
880 
881  // prepare expression context for feature iteration
882  QgsExpressionContext prevContext = context.expressionContext();
883  QgsExpressionContext algContext = prevContext;
884 
885  algContext.appendScopes( createExpressionContext( parameters, context, mSource.get() ).takeScopes() );
886  context.setExpressionContext( algContext );
887 
888  long count = mSource->featureCount();
889 
890  QgsFeature f;
891  QgsFeatureIterator it = mSource->getFeatures( request(), sourceFlags() );
892 
893  double step = count > 0 ? 100.0 / count : 1;
894  int current = 0;
895  while ( it.nextFeature( f ) )
896  {
897  if ( feedback->isCanceled() )
898  {
899  break;
900  }
901 
902  context.expressionContext().setFeature( f );
903  const QgsFeatureList transformed = processFeature( f, context, feedback );
904  for ( QgsFeature transformedFeature : transformed )
905  sink->addFeature( transformedFeature, QgsFeatureSink::FastInsert );
906 
907  feedback->setProgress( current * step );
908  current++;
909  }
910 
911  mSource.reset();
912 
913  // probably not necessary - context's aren't usually recycled, but can't hurt
914  context.setExpressionContext( prevContext );
915 
916  QVariantMap outputs;
917  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
918  return outputs;
919 }
920 
922 {
923  return QgsFeatureRequest();
924 }
925 
927 {
928  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
929  if ( !layer )
930  return false;
931 
932  QgsWkbTypes::GeometryType inPlaceGeometryType = layer->geometryType();
933  if ( !inputLayerTypes().empty() &&
934  !inputLayerTypes().contains( QgsProcessing::TypeVector ) &&
935  !inputLayerTypes().contains( QgsProcessing::TypeVectorAnyGeometry ) &&
936  ( ( inPlaceGeometryType == QgsWkbTypes::PolygonGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorPolygon ) ) ||
937  ( inPlaceGeometryType == QgsWkbTypes::LineGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorLine ) ) ||
938  ( inPlaceGeometryType == QgsWkbTypes::PointGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorPoint ) ) ) )
939  return false;
940 
942  if ( inPlaceGeometryType == QgsWkbTypes::PointGeometry )
943  type = QgsWkbTypes::Point;
944  else if ( inPlaceGeometryType == QgsWkbTypes::LineGeometry )
946  else if ( inPlaceGeometryType == QgsWkbTypes::PolygonGeometry )
947  type = QgsWkbTypes::Polygon;
948  if ( QgsWkbTypes::geometryType( outputWkbType( type ) ) != inPlaceGeometryType )
949  return false;
950 
951  return true;
952 }
953 
954 void QgsProcessingFeatureBasedAlgorithm::prepareSource( const QVariantMap &parameters, QgsProcessingContext &context )
955 {
956  if ( ! mSource )
957  {
958  mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
959  if ( !mSource )
960  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
961  }
962 }
963 
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:41
Base class for all map layer types.
Definition: qgsmaplayer.h:64
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 Q_DECL_DEPRECATED 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.
virtual QgsExpressionContext createExpressionContext(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeatureSource *source=nullptr) const
Creates an expression context relating to the algorithm.
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:571
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.
QgsMeshLayer * parameterAsMeshLayer(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a mesh layer.
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...
QgsWkbTypes::GeometryType geometryType() const
Returns point, line or polygon.
QgsFeatureSink * parameterAsSink(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier, const QgsFields &fields, QgsWkbTypes::Type geometryType=QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags=nullptr) const
Evaluates the parameter with matching name to a feature sink.
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:106
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:55
const QgsCoordinateReferenceSystem & crs
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.
virtual bool supportInPlaceEdit(const QgsMapLayer *layer) const
Checks whether this algorithm supports in-place editing on the given layer Default implementation ret...
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.
static QList< int > parameterAsInts(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of integer values.
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:68
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...
static QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier, QgsFeatureSink::SinkFlags sinkFlags=nullptr)
Evaluates the parameter with matching definition to a feature sink.
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...
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:665
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
Vector polygon layers.
Definition: qgsprocessing.h:50
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.
void prepareSource(const QVariantMap &parameters, QgsProcessingContext &context)
Read the source from parameters and context and set it.
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.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:138
virtual QgsFeatureSink::SinkFlags sinkFlags() const
Returns the feature sink flags to be used for the output.
virtual QVariantMap processAlgorithm(const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback) SIP_THROW(QgsProcessingException)=0
Runs the algorithm using the specified parameters.
static QgsMeshLayer * parameterAsMeshLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition and value to a mesh layer.
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.
Vector point layers.
Definition: qgsprocessing.h:48
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.
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.
Vector line layers.
Definition: qgsprocessing.h:49
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.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
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.
const QgsCoordinateReferenceSystem & outputCrs
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.
QList< int > parameterAsInts(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a list of integer values.
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
virtual void initParameters(const QVariantMap &configuration=QVariantMap())
Initializes any extra parameters added by the algorithm subclass.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:89
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.
QgsProcessingAlgorithm::Flags flags() const override
Returns the flags indicating how and when the algorithm operates and should be exposed to users...
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
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:71
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.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const SIP_THROW(QgsProcessingException)
Creates a copy of the algorithm, ready for execution.
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.