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