QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  const auto constLayers = layers;
228  for ( QgsMapLayer *layer : constLayers )
229  {
230  if ( !layer )
231  continue;
232 
233  if ( foundCrs && layer->crs().isValid() && crs != layer->crs() )
234  {
235  return false;
236  }
237  else if ( !foundCrs && layer->crs().isValid() )
238  {
239  foundCrs = true;
240  crs = layer->crs();
241  }
242  }
243  }
244  else if ( def->type() == QgsProcessingParameterExtent::typeName() )
245  {
246  QgsCoordinateReferenceSystem extentCrs = QgsProcessingParameters::parameterAsExtentCrs( def, parameters, context );
247  if ( foundCrs && extentCrs.isValid() && crs != extentCrs )
248  {
249  return false;
250  }
251  else if ( !foundCrs && extentCrs.isValid() )
252  {
253  foundCrs = true;
254  crs = extentCrs;
255  }
256  }
257  else if ( def->type() == QgsProcessingParameterPoint::typeName() )
258  {
259  QgsCoordinateReferenceSystem pointCrs = QgsProcessingParameters::parameterAsPointCrs( def, parameters, context );
260  if ( foundCrs && pointCrs.isValid() && crs != pointCrs )
261  {
262  return false;
263  }
264  else if ( !foundCrs && pointCrs.isValid() )
265  {
266  foundCrs = true;
267  crs = pointCrs;
268  }
269  }
270  }
271  return true;
272 }
273 
274 QString QgsProcessingAlgorithm::asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const
275 {
276  QString s = QStringLiteral( "processing.run(\"%1\"," ).arg( id() );
277 
278  QStringList parts;
279  for ( const QgsProcessingParameterDefinition *def : mParameters )
280  {
282  continue;
283 
284  if ( !parameters.contains( def->name() ) )
285  continue;
286 
287  parts << QStringLiteral( "'%1':%2" ).arg( def->name(), def->valueAsPythonString( parameters.value( def->name() ), context ) );
288  }
289 
290  s += QStringLiteral( " {%1})" ).arg( parts.join( ',' ) );
291  return s;
292 }
293 
295 {
296  if ( !definition )
297  return false;
298 
299  // check for duplicate named parameters
301  if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
302  {
303  QgsMessageLog::logMessage( QObject::tr( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
304  delete definition;
305  return false;
306  }
307 
308  if ( definition->isDestination() && mProvider )
309  {
310  QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition );
311  if ( !mProvider->supportsNonFileBasedOutput() )
312  destParam->setSupportsNonFileBasedOutput( false );
313  }
314 
315  mParameters << definition;
316  definition->mAlgorithm = this;
317 
318  if ( createOutput )
319  return createAutoOutputForParameter( definition );
320  else
321  return true;
322 }
323 
325 {
327  if ( def )
328  {
329  delete def;
330  mParameters.removeAll( def );
331  }
332 }
333 
335 {
336  if ( !definition )
337  return false;
338 
339  // check for duplicate named outputs
340  if ( QgsProcessingAlgorithm::outputDefinition( definition->name() ) )
341  {
342  QgsMessageLog::logMessage( QObject::tr( "Duplicate output %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) );
343  delete definition;
344  return false;
345  }
346 
347  mOutputs << definition;
348  return true;
349 }
350 
352 {
353  return true;
354 }
355 
357 {
358  return QVariantMap();
359 }
360 
362 {
363  // first pass - case sensitive match
364  for ( const QgsProcessingParameterDefinition *def : mParameters )
365  {
366  if ( def->name() == name )
367  return def;
368  }
369 
370  // second pass - case insensitive
371  for ( const QgsProcessingParameterDefinition *def : mParameters )
372  {
373  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
374  return def;
375  }
376  return nullptr;
377 }
378 
380 {
381  int count = 0;
382  for ( const QgsProcessingParameterDefinition *def : mParameters )
383  {
384  if ( !( def->flags() & QgsProcessingParameterDefinition::FlagHidden ) )
385  count++;
386  }
387  return count;
388 }
389 
391 {
393  for ( const QgsProcessingParameterDefinition *def : mParameters )
394  {
395  if ( def->isDestination() )
396  result << def;
397  }
398  return result;
399 }
400 
402 {
403  for ( const QgsProcessingOutputDefinition *def : mOutputs )
404  {
405  if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
406  return def;
407  }
408  return nullptr;
409 }
410 
412 {
413  for ( const QgsProcessingOutputDefinition *def : mOutputs )
414  {
415  if ( def->type() == QStringLiteral( "outputHtml" ) )
416  return true;
417  }
418  return false;
419 }
420 
421 QVariantMap QgsProcessingAlgorithm::run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok, const QVariantMap &configuration ) const
422 {
423  std::unique_ptr< QgsProcessingAlgorithm > alg( create( configuration ) );
424  if ( ok )
425  *ok = false;
426 
427  bool res = alg->prepare( parameters, context, feedback );
428  if ( !res )
429  return QVariantMap();
430 
431  QVariantMap runRes;
432  try
433  {
434  runRes = alg->runPrepared( parameters, context, feedback );
435  }
436  catch ( QgsProcessingException &e )
437  {
438  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
439  feedback->reportError( e.what() );
440  return QVariantMap();
441  }
442 
443  if ( ok )
444  *ok = true;
445 
446  QVariantMap ppRes = alg->postProcess( context, feedback );
447  if ( !ppRes.isEmpty() )
448  return ppRes;
449  else
450  return runRes;
451 }
452 
453 bool QgsProcessingAlgorithm::prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
454 {
455  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::prepare", "prepare() must be called from the same thread as context was created in" );
456  Q_ASSERT_X( !mHasPrepared, "QgsProcessingAlgorithm::prepare", "prepare() has already been called for the algorithm instance" );
457  try
458  {
459  mHasPrepared = prepareAlgorithm( parameters, context, feedback );
460  return mHasPrepared;
461  }
462  catch ( QgsProcessingException &e )
463  {
464  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
465  feedback->reportError( e.what() );
466  return false;
467  }
468 }
469 
470 QVariantMap QgsProcessingAlgorithm::runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
471 {
472  Q_ASSERT_X( mHasPrepared, "QgsProcessingAlgorithm::runPrepared", QStringLiteral( "prepare() was not called for the algorithm instance %1" ).arg( name() ).toLatin1() );
473  Q_ASSERT_X( !mHasExecuted, "QgsProcessingAlgorithm::runPrepared", "runPrepared() was already called for this algorithm instance" );
474 
475  // Hey kids, let's all be thread safe! It's the fun thing to do!
476  //
477  // First, let's see if we're going to run into issues.
478  QgsProcessingContext *runContext = nullptr;
479  if ( context.thread() == QThread::currentThread() )
480  {
481  // OH. No issues. Seems you're running everything in the same thread, so go about your business. Sorry about
482  // the intrusion, we're just making sure everything's nice and safe here. We like to keep a clean and tidy neighbourhood,
483  // you know, for the kids and dogs and all.
484  runContext = &context;
485  }
486  else
487  {
488  // HA! I knew things looked a bit suspicious - seems you're running this algorithm in a different thread
489  // from that which the passed context has an affinity for. That's fine and all, but we need to make sure
490  // we proceed safely...
491 
492  // So first we create a temporary local context with affinity for the current thread
493  mLocalContext.reset( new QgsProcessingContext() );
494  // copy across everything we can safely do from the passed context
495  mLocalContext->copyThreadSafeSettings( context );
496  // and we'll run the actual algorithm processing using the local thread safe context
497  runContext = mLocalContext.get();
498  }
499 
500  try
501  {
502  QVariantMap runResults = processAlgorithm( parameters, *runContext, feedback );
503 
504  mHasExecuted = true;
505  if ( mLocalContext )
506  {
507  // ok, time to clean things up. We need to push the temporary context back into
508  // the thread that the passed context is associated with (we can only push from the
509  // current thread, so we HAVE to do this here)
510  mLocalContext->pushToThread( context.thread() );
511  }
512  return runResults;
513  }
514  catch ( QgsProcessingException & )
515  {
516  if ( mLocalContext )
517  {
518  // see above!
519  mLocalContext->pushToThread( context.thread() );
520  }
521  //rethrow
522  throw;
523  }
524 }
525 
527 {
528  Q_ASSERT_X( QThread::currentThread() == context.temporaryLayerStore()->thread(), "QgsProcessingAlgorithm::postProcess", "postProcess() must be called from the same thread the context was created in" );
529  Q_ASSERT_X( mHasExecuted, "QgsProcessingAlgorithm::postProcess", QStringLiteral( "algorithm instance %1 was not executed" ).arg( name() ).toLatin1() );
530  Q_ASSERT_X( !mHasPostProcessed, "QgsProcessingAlgorithm::postProcess", "postProcess() was already called for this algorithm instance" );
531 
532  if ( mLocalContext )
533  {
534  // algorithm was processed using a temporary thread safe context. So now we need
535  // to take the results from that temporary context, and smash them into the passed
536  // context
537  context.takeResultsFrom( *mLocalContext );
538  // now get lost, we don't need you anymore
539  mLocalContext.reset();
540  }
541 
542  mHasPostProcessed = true;
543  try
544  {
545  return postProcessAlgorithm( context, feedback );
546  }
547  catch ( QgsProcessingException &e )
548  {
549  QgsMessageLog::logMessage( e.what(), QObject::tr( "Processing" ), Qgis::Critical );
550  feedback->reportError( e.what() );
551  return QVariantMap();
552  }
553 }
554 
555 QString QgsProcessingAlgorithm::parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
556 {
557  return QgsProcessingParameters::parameterAsString( parameterDefinition( name ), parameters, context );
558 }
559 
560 QString QgsProcessingAlgorithm::parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
561 {
562  return QgsProcessingParameters::parameterAsExpression( parameterDefinition( name ), parameters, context );
563 }
564 
565 double QgsProcessingAlgorithm::parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
566 {
567  return QgsProcessingParameters::parameterAsDouble( parameterDefinition( name ), parameters, context );
568 }
569 
570 int QgsProcessingAlgorithm::parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
571 {
572  return QgsProcessingParameters::parameterAsInt( parameterDefinition( name ), parameters, context );
573 }
574 
575 QList<int> QgsProcessingAlgorithm::parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
576 {
577  return QgsProcessingParameters::parameterAsInts( parameterDefinition( name ), parameters, context );
578 }
579 
580 int QgsProcessingAlgorithm::parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
581 {
582  return QgsProcessingParameters::parameterAsEnum( parameterDefinition( name ), parameters, context );
583 }
584 
585 QList<int> QgsProcessingAlgorithm::parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
586 {
587  return QgsProcessingParameters::parameterAsEnums( parameterDefinition( name ), parameters, context );
588 }
589 
590 bool QgsProcessingAlgorithm::parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
591 {
592  return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
593 }
594 
595 bool QgsProcessingAlgorithm::parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
596 {
597  return QgsProcessingParameters::parameterAsBool( parameterDefinition( name ), parameters, context );
598 }
599 
600 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
601 {
602  return QgsProcessingParameters::parameterAsSink( parameterDefinition( name ), parameters, fields, geometryType, crs, context, destinationIdentifier, sinkFlags );
603 }
604 
605 QgsProcessingFeatureSource *QgsProcessingAlgorithm::parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
606 {
607  return QgsProcessingParameters::parameterAsSource( parameterDefinition( name ), parameters, context );
608 }
609 
610 QString QgsProcessingAlgorithm::parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
611 {
612  return QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( parameterDefinition( name ), parameters, context, compatibleFormats, preferredFormat, feedback );
613 }
614 
615 QString QgsProcessingAlgorithm::parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback, QString *layerName )
616 {
617  return QgsProcessingParameters::parameterAsCompatibleSourceLayerPathAndLayerName( parameterDefinition( name ), parameters, context, compatibleFormats, preferredFormat, feedback, layerName );
618 }
619 
620 QgsMapLayer *QgsProcessingAlgorithm::parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
621 {
622  return QgsProcessingParameters::parameterAsLayer( parameterDefinition( name ), parameters, context );
623 }
624 
625 QgsRasterLayer *QgsProcessingAlgorithm::parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
626 {
627  return QgsProcessingParameters::parameterAsRasterLayer( parameterDefinition( name ), parameters, context );
628 }
629 
630 QgsMeshLayer *QgsProcessingAlgorithm::parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
631 {
632  return QgsProcessingParameters::parameterAsMeshLayer( parameterDefinition( name ), parameters, context );
633 }
634 
635 QString QgsProcessingAlgorithm::parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
636 {
637  return QgsProcessingParameters::parameterAsOutputLayer( parameterDefinition( name ), parameters, context );
638 }
639 
640 QString QgsProcessingAlgorithm::parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
641 {
642  return QgsProcessingParameters::parameterAsFileOutput( parameterDefinition( name ), parameters, context );
643 }
644 
645 QgsVectorLayer *QgsProcessingAlgorithm::parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
646 {
647  return QgsProcessingParameters::parameterAsVectorLayer( parameterDefinition( name ), parameters, context );
648 }
649 
650 QgsCoordinateReferenceSystem QgsProcessingAlgorithm::parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
651 {
652  return QgsProcessingParameters::parameterAsCrs( parameterDefinition( name ), parameters, context );
653 }
654 
656 {
657  return QgsProcessingParameters::parameterAsExtentCrs( parameterDefinition( name ), parameters, context );
658 }
659 
660 QgsRectangle QgsProcessingAlgorithm::parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
661 {
662  return QgsProcessingParameters::parameterAsExtent( parameterDefinition( name ), parameters, context, crs );
663 }
664 
666 {
667  return QgsProcessingParameters::parameterAsExtentGeometry( parameterDefinition( name ), parameters, context, crs );
668 }
669 
670 QgsPointXY QgsProcessingAlgorithm::parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs ) const
671 {
672  return QgsProcessingParameters::parameterAsPoint( parameterDefinition( name ), parameters, context, crs );
673 }
674 
676 {
677  return QgsProcessingParameters::parameterAsPointCrs( parameterDefinition( name ), parameters, context );
678 }
679 
680 QString QgsProcessingAlgorithm::parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
681 {
682  return QgsProcessingParameters::parameterAsFile( parameterDefinition( name ), parameters, context );
683 }
684 
685 QVariantList QgsProcessingAlgorithm::parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
686 {
687  return QgsProcessingParameters::parameterAsMatrix( parameterDefinition( name ), parameters, context );
688 }
689 
690 QList<QgsMapLayer *> QgsProcessingAlgorithm::parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
691 {
692  return QgsProcessingParameters::parameterAsLayerList( parameterDefinition( name ), parameters, context );
693 }
694 
695 QStringList QgsProcessingAlgorithm::parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
696 {
697  return QgsProcessingParameters::parameterAsFileList( parameterDefinition( name ), parameters, context );
698 }
699 
700 QList<double> QgsProcessingAlgorithm::parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
701 {
702  return QgsProcessingParameters::parameterAsRange( parameterDefinition( name ), parameters, context );
703 }
704 
705 QStringList QgsProcessingAlgorithm::parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const
706 {
707  return QgsProcessingParameters::parameterAsFields( parameterDefinition( name ), parameters, context );
708 }
709 
710 QgsPrintLayout *QgsProcessingAlgorithm::parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context )
711 {
712  return QgsProcessingParameters::parameterAsLayout( parameterDefinition( name ), parameters, context );
713 }
714 
715 QgsLayoutItem *QgsProcessingAlgorithm::parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout )
716 {
717  return QgsProcessingParameters::parameterAsLayoutItem( parameterDefinition( name ), parameters, context, layout );
718 }
719 
720 QColor QgsProcessingAlgorithm::parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context )
721 {
722  return QgsProcessingParameters::parameterAsColor( parameterDefinition( name ), parameters, context );
723 }
724 
725 QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameters, const QString &name )
726 {
727  if ( !parameters.contains( name ) )
728  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
729  else
730  {
731  QVariant var = parameters.value( name );
732  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
733  {
735  var = fromVar.source;
736  }
737  else if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
738  {
740  var = fromVar.sink;
741  }
742  if ( var.canConvert<QgsProperty>() )
743  {
744  QgsProperty p = var.value< QgsProperty >();
746  {
747  var = p.staticValue();
748  }
749  }
750  if ( !var.toString().isEmpty() )
751  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
752  else
753  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
754  }
755 }
756 
757 QString QgsProcessingAlgorithm::invalidRasterError( const QVariantMap &parameters, const QString &name )
758 {
759  if ( !parameters.contains( name ) )
760  return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
761  else
762  {
763  QVariant var = parameters.value( name );
764  if ( var.canConvert<QgsProperty>() )
765  {
766  QgsProperty p = var.value< QgsProperty >();
768  {
769  var = p.staticValue();
770  }
771  }
772  if ( !var.toString().isEmpty() )
773  return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
774  else
775  return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
776  }
777 }
778 
779 QString QgsProcessingAlgorithm::invalidSinkError( const QVariantMap &parameters, const QString &name )
780 {
781  if ( !parameters.contains( name ) )
782  return QObject::tr( "Could not create destination layer for %1: no value specified for parameter" ).arg( name );
783  else
784  {
785  QVariant var = parameters.value( name );
786  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
787  {
789  var = fromVar.sink;
790  }
791  if ( var.canConvert<QgsProperty>() )
792  {
793  QgsProperty p = var.value< QgsProperty >();
795  {
796  var = p.staticValue();
797  }
798  }
799  if ( !var.toString().isEmpty() )
800  return QObject::tr( "Could not create destination layer for %1: %2" ).arg( name, var.toString() );
801  else
802  return QObject::tr( "Could not create destination layer for %1: invalid value" ).arg( name );
803  }
804 }
805 
807 {
808  Q_UNUSED( layer )
809  return false;
810 }
811 
812 
813 bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
814 {
815  if ( !parameter->isDestination() )
816  return true; // nothing created, but nothing went wrong - so return true
817 
818  QgsProcessingDestinationParameter *dest = static_cast< QgsProcessingDestinationParameter * >( parameter );
820  if ( !output )
821  return true; // nothing created - but nothing went wrong - so return true
822 
823  if ( !addOutput( output ) )
824  {
825  // couldn't add output - probably a duplicate name
826  return false;
827  }
828  else
829  {
830  return true;
831  }
832 }
833 
834 
835 //
836 // QgsProcessingFeatureBasedAlgorithm
837 //
838 
839 QgsProcessingAlgorithm::Flags QgsProcessingFeatureBasedAlgorithm::flags() const
840 {
841  Flags f = QgsProcessingAlgorithm::flags();
843  return f;
844 }
845 
846 void QgsProcessingFeatureBasedAlgorithm::initAlgorithm( const QVariantMap &config )
847 {
848  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), inputLayerTypes() ) );
849  initParameters( config );
850  addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), outputName(), outputLayerType() ) );
851 }
852 
854 {
855  return QList<int>();
856 }
857 
859 {
861 }
862 
864 {
865  return static_cast<QgsProcessingFeatureSource::Flag>( 0 );
866 }
867 
868 QgsFeatureSink::SinkFlags QgsProcessingFeatureBasedAlgorithm::sinkFlags() const
869 {
870  return nullptr;
871 }
872 
874 {
875  return inputWkbType;
876 }
877 
879 {
880  return inputFields;
881 }
882 
884 {
885  return inputCrs;
886 }
887 
889 {
890 }
891 
893 {
894  if ( mSource )
895  return mSource->sourceCrs();
896  else
898 }
899 
900 QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
901 {
902  prepareSource( parameters, context );
903  QString dest;
904  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
905  outputFields( mSource->fields() ),
906  outputWkbType( mSource->wkbType() ),
907  outputCrs( mSource->sourceCrs() ),
908  sinkFlags() ) );
909  if ( !sink )
910  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
911 
912  // prepare expression context for feature iteration
913  QgsExpressionContext prevContext = context.expressionContext();
914  QgsExpressionContext algContext = prevContext;
915 
916  algContext.appendScopes( createExpressionContext( parameters, context, mSource.get() ).takeScopes() );
917  context.setExpressionContext( algContext );
918 
919  long count = mSource->featureCount();
920 
921  QgsFeature f;
922  QgsFeatureIterator it = mSource->getFeatures( request(), sourceFlags() );
923 
924  double step = count > 0 ? 100.0 / count : 1;
925  int current = 0;
926  while ( it.nextFeature( f ) )
927  {
928  if ( feedback->isCanceled() )
929  {
930  break;
931  }
932 
933  context.expressionContext().setFeature( f );
934  const QgsFeatureList transformed = processFeature( f, context, feedback );
935  for ( QgsFeature transformedFeature : transformed )
936  sink->addFeature( transformedFeature, QgsFeatureSink::FastInsert );
937 
938  feedback->setProgress( current * step );
939  current++;
940  }
941 
942  mSource.reset();
943 
944  // probably not necessary - context's aren't usually recycled, but can't hurt
945  context.setExpressionContext( prevContext );
946 
947  QVariantMap outputs;
948  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
949  return outputs;
950 }
951 
953 {
954  return QgsFeatureRequest();
955 }
956 
958 {
959  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
960  if ( !layer )
961  return false;
962 
963  QgsWkbTypes::GeometryType inPlaceGeometryType = layer->geometryType();
964  if ( !inputLayerTypes().empty() &&
965  !inputLayerTypes().contains( QgsProcessing::TypeVector ) &&
966  !inputLayerTypes().contains( QgsProcessing::TypeVectorAnyGeometry ) &&
967  ( ( inPlaceGeometryType == QgsWkbTypes::PolygonGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorPolygon ) ) ||
968  ( inPlaceGeometryType == QgsWkbTypes::LineGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorLine ) ) ||
969  ( inPlaceGeometryType == QgsWkbTypes::PointGeometry && !inputLayerTypes().contains( QgsProcessing::TypeVectorPoint ) ) ) )
970  return false;
971 
973  if ( inPlaceGeometryType == QgsWkbTypes::PointGeometry )
974  type = QgsWkbTypes::Point;
975  else if ( inPlaceGeometryType == QgsWkbTypes::LineGeometry )
977  else if ( inPlaceGeometryType == QgsWkbTypes::PolygonGeometry )
978  type = QgsWkbTypes::Polygon;
979  if ( QgsWkbTypes::geometryType( outputWkbType( type ) ) != inPlaceGeometryType )
980  return false;
981 
982  return true;
983 }
984 
985 void QgsProcessingFeatureBasedAlgorithm::prepareSource( const QVariantMap &parameters, QgsProcessingContext &context )
986 {
987  if ( ! mSource )
988  {
989  mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
990  if ( !mSource )
991  throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
992  }
993 }
994 
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:79
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.
Base class for graphical items within a QgsLayout.
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
QColor parameterAsColor(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context)
Evaluates the parameter with matching name to a color, or returns an invalid color if the parameter w...
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:122
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.
QStringList parameterAsFileList(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context) const
Evaluates the parameter with matching name to a list of files (for QgsProcessingParameterMultipleLaye...
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:812
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
static QString parameterAsCompatibleSourceLayerPathAndLayerName(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr, QString *layerName=nullptr)
Evaluates the parameter with matching definition to a source vector layer file path and layer name of...
static QStringList parameterAsFileList(const QgsProcessingParameterDefinition *definition, const QVariant &value, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of files (for QgsProcessingParameterMultip...
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.
bool parameterAsBoolean(const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context) const
Evaluates the parameter with matching name to a static boolean value.
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...
static QgsPrintLayout * parameterAsLayout(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a print layout.
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.
QgsLayoutItem * parameterAsLayoutItem(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout)
Evaluates the parameter with matching name to a print layout item, taken from the specified layout...
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:139
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.
static QgsLayoutItem * parameterAsLayoutItem(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, QgsPrintLayout *layout)
Evaluates the parameter with matching definition to a print layout item, taken from the specified lay...
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
QgsPrintLayout * parameterAsLayout(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context)
Evaluates the parameter with matching name to a print layout.
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:90
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
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.
QString parameterAsCompatibleSourceLayerPathAndLayerName(const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat=QString("shp"), QgsProcessingFeedback *feedback=nullptr, QString *layerName=nullptr)
Evaluates the parameter with matching name to a source vector layer file path and layer name of compa...
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.
Static property (QgsStaticProperty)
Definition: qgsproperty.h:237
Contains information about the context in which a processing algorithm is executed.
static QColor parameterAsColor(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the color associated with an point parameter value, or an invalid color if the parameter was ...
QgsProcessingAlgorithm::Flags flags() const override
Returns the flags indicating how and when the algorithm operates and should be exposed to users...
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:86
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.