QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsprocessingparameters.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingparameters.cpp
3  ---------------------------
4  begin : April 2017
5  copyright : (C) 2017 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 
19 #include "qgsprocessingprovider.h"
20 #include "qgsprocessingcontext.h"
21 #include "qgsprocessingutils.h"
22 #include "qgsprocessingalgorithm.h"
24 #include "qgsprocessingoutputs.h"
25 #include "qgssettings.h"
26 #include "qgsvectorfilewriter.h"
27 #include "qgsreferencedgeometry.h"
28 #include "qgsprocessingregistry.h"
30 #include "qgsrasterfilewriter.h"
31 #include <functional>
32 
33 
35 {
36  QVariantMap map;
37  map.insert( QStringLiteral( "sink" ), sink.toVariant() );
38  map.insert( QStringLiteral( "create_options" ), createOptions );
39  return map;
40 }
41 
43 {
44  sink.loadVariant( map.value( QStringLiteral( "sink" ) ) );
45  createOptions = map.value( QStringLiteral( "create_options" ) ).toMap();
46  return true;
47 }
48 
49 bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
50 {
51  QVariant val = parameters.value( name );
52  if ( val.canConvert<QgsProperty>() )
53  return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
54  else
55  return false;
56 }
57 
58 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
59 {
60  if ( !definition )
61  return QString();
62 
63  QVariant val = parameters.value( definition->name() );
64  if ( val.canConvert<QgsProperty>() )
65  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
66 
67  if ( !val.isValid() )
68  {
69  // fall back to default
70  val = definition->defaultValue();
71  }
72 
73  return val.toString();
74 }
75 
76 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
77 {
78  if ( !definition )
79  return QString();
80 
81  QVariant val = parameters.value( definition->name() );
82  if ( val.canConvert<QgsProperty>() )
83  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
84 
85  if ( val.isValid() && !val.toString().isEmpty() )
86  {
87  QgsExpression e( val.toString() );
88  if ( e.isValid() )
89  return val.toString();
90  }
91 
92  // fall back to default
93  return definition->defaultValue().toString();
94 }
95 
96 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
97 {
98  if ( !definition )
99  return 0;
100 
101  QVariant val = parameters.value( definition->name() );
102  if ( val.canConvert<QgsProperty>() )
103  return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
104 
105  bool ok = false;
106  double res = val.toDouble( &ok );
107  if ( ok )
108  return res;
109 
110  // fall back to default
111  val = definition->defaultValue();
112  return val.toDouble();
113 }
114 
115 int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
116 {
117  if ( !definition )
118  return 0;
119 
120  QVariant val = parameters.value( definition->name() );
121  if ( val.canConvert<QgsProperty>() )
122  return val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
123 
124  bool ok = false;
125  double dbl = val.toDouble( &ok );
126  if ( !ok )
127  {
128  // fall back to default
129  val = definition->defaultValue();
130  dbl = val.toDouble( &ok );
131  }
132 
133  //String representations of doubles in QVariant will not convert to int
134  //work around this by first converting to double, and then checking whether the double is convertible to int
135  if ( ok )
136  {
137  double round = std::round( dbl );
138  if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
139  {
140  //double too large to fit in int
141  return 0;
142  }
143  return std::round( dbl );
144  }
145 
146  return val.toInt();
147 }
148 
149 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
150 {
151  if ( !definition )
152  return 0;
153 
154  int val = parameterAsInt( definition, parameters, context );
155  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
156  if ( enumDef && val >= enumDef->options().size() )
157  {
158  return enumDef->defaultValue().toInt();
159  }
160  return val;
161 }
162 
163 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
164 {
165  if ( !definition )
166  return QList<int>();
167 
168  QVariantList resultList;
169  QVariant val = parameters.value( definition->name() );
170  if ( val.canConvert<QgsProperty>() )
171  resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
172  else if ( val.type() == QVariant::List )
173  {
174  Q_FOREACH ( const QVariant &var, val.toList() )
175  resultList << var;
176  }
177  else if ( val.type() == QVariant::String )
178  {
179  Q_FOREACH ( const QString &var, val.toString().split( ',' ) )
180  resultList << var;
181  }
182  else
183  resultList << val;
184 
185  if ( resultList.isEmpty() )
186  return QList< int >();
187 
188  if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
189  {
190  resultList.clear();
191  // check default
192  if ( definition->defaultValue().type() == QVariant::List )
193  {
194  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
195  resultList << var;
196  }
197  else if ( definition->defaultValue().type() == QVariant::String )
198  {
199  Q_FOREACH ( const QString &var, definition->defaultValue().toString().split( ',' ) )
200  resultList << var;
201  }
202  else
203  resultList << definition->defaultValue();
204  }
205 
206  QList< int > result;
207  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
208  Q_FOREACH ( const QVariant &var, resultList )
209  {
210  int resInt = var.toInt();
211  if ( !enumDef || resInt < enumDef->options().size() )
212  {
213  result << resInt;
214  }
215  }
216  return result;
217 }
218 
219 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
220 {
221  if ( !definition )
222  return false;
223 
224  QVariant def = definition->defaultValue();
225 
226  QVariant val = parameters.value( definition->name() );
227  if ( val.canConvert<QgsProperty>() )
228  return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
229  else if ( val.isValid() )
230  return val.toBool();
231  else
232  return def.toBool();
233 }
234 
235 QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields,
236  QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
237  QgsProcessingContext &context, QString &destinationIdentifier )
238 {
239  QVariant val;
240  if ( definition )
241  {
242  val = parameters.value( definition->name() );
243  }
244 
245  QgsProject *destinationProject = nullptr;
246  QString destName;
247  QVariantMap createOptions;
248  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
249  {
250  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
252  destinationProject = fromVar.destinationProject;
253  createOptions = fromVar.createOptions;
254  val = fromVar.sink;
255  destName = fromVar.destinationName;
256  }
257 
258  QString dest;
259  if ( val.canConvert<QgsProperty>() )
260  {
261  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
262  }
263  else if ( !val.isValid() || val.toString().isEmpty() )
264  {
265  if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
266  {
267  // unset, optional sink, no default => no sink
268  return nullptr;
269  }
270  // fall back to default
271  dest = definition->defaultValue().toString();
272  }
273  else
274  {
275  dest = val.toString();
276  }
277 
278  if ( dest.isEmpty() )
279  return nullptr;
280 
281  std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, createOptions ) );
282  destinationIdentifier = dest;
283 
284  if ( destinationProject )
285  {
286  if ( destName.isEmpty() && definition )
287  {
288  destName = definition->description();
289  }
290  QString outputName;
291  if ( definition )
292  outputName = definition->name();
293  context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName ) );
294  }
295 
296  return sink.release();
297 }
298 
300 {
301  if ( !definition )
302  return nullptr;
303 
304  QVariant val = parameters.value( definition->name() );
305 
306  return QgsProcessingUtils::variantToSource( val, context, definition->defaultValue() );
307 }
308 
309 QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
310 {
311  if ( !definition )
312  return QString();
313 
314  QVariant val = parameters.value( definition->name() );
315 
316  bool selectedFeaturesOnly = false;
317  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
318  {
319  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
321  selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
322  val = fromVar.source;
323  }
324 
325  if ( val.canConvert<QgsProperty>() )
326  {
327  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
328  }
329 
330  QgsVectorLayer *vl = nullptr;
331  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
332 
333  if ( !vl )
334  {
335  QString layerRef;
336  if ( val.canConvert<QgsProperty>() )
337  {
338  layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
339  }
340  else if ( !val.isValid() || val.toString().isEmpty() )
341  {
342  // fall back to default
343  val = definition->defaultValue();
344 
345  // default value may be a vector layer
346  vl = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) );
347  if ( !vl )
348  layerRef = definition->defaultValue().toString();
349  }
350  else
351  {
352  layerRef = val.toString();
353  }
354 
355  if ( !vl )
356  {
357  if ( layerRef.isEmpty() )
358  return QString();
359 
360  vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
361  }
362  }
363 
364  if ( !vl )
365  return QString();
366 
367  return QgsProcessingUtils::convertToCompatibleFormat( vl, selectedFeaturesOnly, definition->name(),
368  compatibleFormats, preferredFormat, context, feedback );
369 }
370 
371 
373 {
374  if ( !definition )
375  return nullptr;
376 
377  QVariant val = parameters.value( definition->name() );
378  if ( val.canConvert<QgsProperty>() )
379  {
380  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
381  }
382 
383  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
384  {
385  return layer;
386  }
387 
388  if ( !val.isValid() || val.toString().isEmpty() )
389  {
390  // fall back to default
391  val = definition->defaultValue();
392  }
393 
394  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
395  {
396  return layer;
397  }
398 
399  QString layerRef = val.toString();
400  if ( layerRef.isEmpty() )
401  layerRef = definition->defaultValue().toString();
402 
403  if ( layerRef.isEmpty() )
404  return nullptr;
405 
406  return QgsProcessingUtils::mapLayerFromString( layerRef, context );
407 }
408 
410 {
411  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context ) );
412 }
413 
414 QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
415 {
416  QVariant val;
417  if ( definition )
418  {
419  val = parameters.value( definition->name() );
420  }
421 
422  QgsProject *destinationProject = nullptr;
423  QVariantMap createOptions;
424  QString destName;
425  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
426  {
427  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
429  destinationProject = fromVar.destinationProject;
430  createOptions = fromVar.createOptions;
431  val = fromVar.sink;
432  destName = fromVar.destinationName;
433  }
434 
435  QString dest;
436  if ( val.canConvert<QgsProperty>() )
437  {
438  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
439  }
440  else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
441  {
442  // fall back to default
443  dest = definition->defaultValue().toString();
444  }
445  else
446  {
447  dest = val.toString();
448  }
449 
450  if ( destinationProject )
451  {
452  QString outputName;
453  if ( destName.isEmpty() && definition )
454  {
455  destName = definition->description();
456  }
457  if ( definition )
458  outputName = definition->name();
459  context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName ) );
460  }
461 
462  return dest;
463 }
464 
465 QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
466 {
467  QVariant val;
468  if ( definition )
469  {
470  val = parameters.value( definition->name() );
471  }
472 
473  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
474  {
475  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
477  val = fromVar.sink;
478  }
479 
480  QString dest;
481  if ( val.canConvert<QgsProperty>() )
482  {
483  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
484  }
485  else if ( !val.isValid() || val.toString().isEmpty() )
486  {
487  // fall back to default
488  dest = definition->defaultValue().toString();
489  }
490  else
491  {
492  dest = val.toString();
493  }
494 
495  return dest;
496 }
497 
499 {
500  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context ) );
501 }
502 
504 {
505  if ( !definition )
507 
508  QVariant val = parameters.value( definition->name() );
509 
510  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
511  {
512  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
514  val = fromVar.source;
515  }
516 
517  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
518  {
519  val = val.value< QgsProperty >().staticValue();
520  }
521 
522  // maybe a map layer
523  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
524  return layer->crs();
525 
526  if ( val.canConvert<QgsProperty>() )
527  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
528 
529  if ( !val.isValid() )
530  {
531  // fall back to default
532  val = definition->defaultValue();
533  }
534 
535  QString crsText = val.toString();
536  if ( crsText.isEmpty() )
537  crsText = definition->defaultValue().toString();
538 
539  if ( crsText.isEmpty() )
541 
542  // maybe special string
543  if ( context.project() && crsText.compare( QStringLiteral( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
544  return context.project()->crs();
545 
546  // maybe a map layer reference
547  if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
548  return layer->crs();
549 
550  // else CRS from string
552  crs.createFromString( crsText );
553  return crs;
554 }
555 
557  const QgsCoordinateReferenceSystem &crs )
558 {
559  if ( !definition )
560  return QgsRectangle();
561 
562  QVariant val = parameters.value( definition->name() );
563 
564  if ( val.canConvert< QgsRectangle >() )
565  {
566  return val.value<QgsRectangle>();
567  }
568  if ( val.canConvert< QgsReferencedRectangle >() )
569  {
571  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
572  {
573  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
574  try
575  {
576  return ct.transformBoundingBox( rr );
577  }
578  catch ( QgsCsException & )
579  {
580  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
581  }
582  }
583  return rr;
584  }
585 
586  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
587  {
588  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
590  val = fromVar.source;
591  }
592 
593  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
594  {
595  val = val.value< QgsProperty >().staticValue();
596  }
597 
598  // maybe parameter is a direct layer value?
599  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
600 
601  QString rectText;
602  if ( val.canConvert<QgsProperty>() )
603  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
604  else
605  rectText = val.toString();
606 
607  if ( rectText.isEmpty() && !layer )
608  return QgsRectangle();
609 
610  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
611  QRegularExpressionMatch match = rx.match( rectText );
612  if ( match.hasMatch() )
613  {
614  bool xMinOk = false;
615  double xMin = match.captured( 1 ).toDouble( &xMinOk );
616  bool xMaxOk = false;
617  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
618  bool yMinOk = false;
619  double yMin = match.captured( 3 ).toDouble( &yMinOk );
620  bool yMaxOk = false;
621  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
622  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
623  {
624  QgsRectangle rect( xMin, yMin, xMax, yMax );
625  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
626  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
627  {
628  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
629  try
630  {
631  return ct.transformBoundingBox( rect );
632  }
633  catch ( QgsCsException & )
634  {
635  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
636  }
637  }
638  return rect;
639  }
640  }
641 
642  // try as layer extent
643  if ( !layer )
644  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
645 
646  if ( layer )
647  {
648  QgsRectangle rect = layer->extent();
649  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
650  {
651  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
652  try
653  {
654  return ct.transformBoundingBox( rect );
655  }
656  catch ( QgsCsException & )
657  {
658  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
659  }
660  }
661  return rect;
662  }
663  return QgsRectangle();
664 }
665 
667 {
668  if ( !definition )
669  return QgsGeometry();
670 
671  QVariant val = parameters.value( definition->name() );
672 
673  if ( val.canConvert< QgsReferencedRectangle >() )
674  {
677  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
678  {
679  g = g.densifyByCount( 20 );
680  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
681  try
682  {
683  g.transform( ct );
684  }
685  catch ( QgsCsException & )
686  {
687  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
688  }
689  return g;
690  }
691  }
692 
693  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
694  {
695  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
697  val = fromVar.source;
698  }
699 
700  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
701  {
702  val = val.value< QgsProperty >().staticValue();
703  }
704 
705  QString rectText;
706  if ( val.canConvert<QgsProperty>() )
707  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
708  else
709  rectText = val.toString();
710 
711  if ( !rectText.isEmpty() )
712  {
713  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
714  QRegularExpressionMatch match = rx.match( rectText );
715  if ( match.hasMatch() )
716  {
717  bool xMinOk = false;
718  double xMin = match.captured( 1 ).toDouble( &xMinOk );
719  bool xMaxOk = false;
720  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
721  bool yMinOk = false;
722  double yMin = match.captured( 3 ).toDouble( &yMinOk );
723  bool yMaxOk = false;
724  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
725  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
726  {
727  QgsRectangle rect( xMin, yMin, xMax, yMax );
728  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
730  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
731  {
732  g = g.densifyByCount( 20 );
733  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
734  try
735  {
736  g.transform( ct );
737  }
738  catch ( QgsCsException & )
739  {
740  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
741  }
742  return g;
743  }
744  }
745  }
746  }
747 
748  // try as layer extent
749 
750  // maybe parameter is a direct layer value?
751  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
752  if ( !layer )
753  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
754 
755  if ( layer )
756  {
757  QgsRectangle rect = layer->extent();
759  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
760  {
761  g = g.densifyByCount( 20 );
762  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
763  try
764  {
765  g.transform( ct );
766  }
767  catch ( QgsCsException & )
768  {
769  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
770  }
771  }
772  return g;
773  }
774 
775  return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
776 }
777 
779 {
780  QVariant val = parameters.value( definition->name() );
781 
782  if ( val.canConvert< QgsReferencedRectangle >() )
783  {
785  if ( rr.crs().isValid() )
786  {
787  return rr.crs();
788  }
789  }
790 
791  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
792  {
793  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
795  val = fromVar.source;
796  }
797 
798  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
799  {
800  val = val.value< QgsProperty >().staticValue();
801  }
802 
803  QString valueAsString;
804  if ( val.canConvert<QgsProperty>() )
805  valueAsString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
806  else
807  valueAsString = val.toString();
808 
809  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
810 
811  QRegularExpressionMatch match = rx.match( valueAsString );
812  if ( match.hasMatch() )
813  {
814  QgsCoordinateReferenceSystem crs( match.captured( 5 ) );
815  if ( crs.isValid() )
816  return crs;
817  }
818 
819  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
820  {
821  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
823  val = fromVar.source;
824  }
825 
826  if ( val.canConvert<QgsProperty>() && val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty )
827  {
828  val = val.value< QgsProperty >().staticValue();
829  }
830 
831  // try as layer crs
832  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
833  return layer->crs();
834  else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
835  return layer->crs();
836 
837  if ( context.project() )
838  return context.project()->crs();
839  else
841 }
842 
844 {
845  if ( !definition )
846  return QgsPointXY();
847 
848  QVariant val = parameters.value( definition->name() );
849  if ( val.canConvert< QgsPointXY >() )
850  {
851  return val.value<QgsPointXY>();
852  }
853  if ( val.canConvert< QgsReferencedPointXY >() )
854  {
855  QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
856  if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
857  {
858  QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
859  try
860  {
861  return ct.transform( rp );
862  }
863  catch ( QgsCsException & )
864  {
865  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
866  }
867  }
868  return rp;
869  }
870 
871  QString pointText = parameterAsString( definition, parameters, context );
872  if ( pointText.isEmpty() )
873  pointText = definition->defaultValue().toString();
874 
875  if ( pointText.isEmpty() )
876  return QgsPointXY();
877 
878  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
879 
880  QString valueAsString = parameterAsString( definition, parameters, context );
881  QRegularExpressionMatch match = rx.match( valueAsString );
882  if ( match.hasMatch() )
883  {
884  bool xOk = false;
885  double x = match.captured( 1 ).toDouble( &xOk );
886  bool yOk = false;
887  double y = match.captured( 2 ).toDouble( &yOk );
888 
889  if ( xOk && yOk )
890  {
891  QgsPointXY pt( x, y );
892 
893  QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
894  if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
895  {
896  QgsCoordinateTransform ct( pointCrs, crs, context.project() );
897  try
898  {
899  return ct.transform( pt );
900  }
901  catch ( QgsCsException & )
902  {
903  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
904  }
905  }
906  return pt;
907  }
908  }
909 
910  return QgsPointXY();
911 }
912 
914 {
915  QVariant val = parameters.value( definition->name() );
916 
917  if ( val.canConvert< QgsReferencedPointXY >() )
918  {
919  QgsReferencedPointXY rr = val.value<QgsReferencedPointXY>();
920  if ( rr.crs().isValid() )
921  {
922  return rr.crs();
923  }
924  }
925 
926  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
927 
928  QString valueAsString = parameterAsString( definition, parameters, context );
929  QRegularExpressionMatch match = rx.match( valueAsString );
930  if ( match.hasMatch() )
931  {
932  QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
933  if ( crs.isValid() )
934  return crs;
935  }
936 
937  if ( context.project() )
938  return context.project()->crs();
939  else
941 }
942 
943 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
944 {
945  if ( !definition )
946  return QString();
947 
948  QString fileText = parameterAsString( definition, parameters, context );
949  if ( fileText.isEmpty() )
950  fileText = definition->defaultValue().toString();
951  return fileText;
952 }
953 
954 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
955 {
956  if ( !definition )
957  return QVariantList();
958 
959  QString resultString;
960  QVariant val = parameters.value( definition->name() );
961  if ( val.canConvert<QgsProperty>() )
962  resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
963  else if ( val.type() == QVariant::List )
964  return val.toList();
965  else
966  resultString = val.toString();
967 
968  if ( resultString.isEmpty() )
969  {
970  // check default
971  if ( definition->defaultValue().type() == QVariant::List )
972  return definition->defaultValue().toList();
973  else
974  resultString = definition->defaultValue().toString();
975  }
976 
977  QVariantList result;
978  Q_FOREACH ( const QString &s, resultString.split( ',' ) )
979  result << s;
980 
981  return result;
982 }
983 
984 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
985 {
986  if ( !definition )
987  return QList<QgsMapLayer *>();
988 
989  QVariant val = parameters.value( definition->name() );
990  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
991  {
992  return QList<QgsMapLayer *>() << layer;
993  }
994 
995  QList<QgsMapLayer *> layers;
996 
997  QStringList resultStringList;
998 
999  std::function< void( const QVariant &var ) > processVariant;
1000  processVariant = [ &resultStringList, &layers, &context, &definition, &processVariant ]( const QVariant & var )
1001  {
1002  if ( var.type() == QVariant::List )
1003  {
1004  Q_FOREACH ( const QVariant &listVar, var.toList() )
1005  {
1006  processVariant( listVar );
1007  }
1008  }
1009  else if ( var.type() == QVariant::StringList )
1010  {
1011  Q_FOREACH ( const QString &s, var.toStringList() )
1012  {
1013  resultStringList << s;
1014  }
1015  }
1016  else if ( var.canConvert<QgsProperty>() )
1017  resultStringList << var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1018  else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1019  {
1020  layers << layer;
1021  }
1022  else
1023  {
1024  resultStringList << var.toString();
1025  }
1026  };
1027 
1028  processVariant( val );
1029 
1030  if ( layers.isEmpty() && ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
1031  {
1032  resultStringList.clear();
1033  // check default
1034  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
1035  {
1036  layers << layer;
1037  }
1038  else if ( definition->defaultValue().type() == QVariant::List )
1039  {
1040  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1041  {
1042  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
1043  {
1044  layers << layer;
1045  }
1046  else
1047  {
1048  resultStringList << var.toString();
1049  }
1050  }
1051  }
1052  else
1053  resultStringList << definition->defaultValue().toString();
1054  }
1055 
1056  Q_FOREACH ( const QString &s, resultStringList )
1057  {
1058  QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( s, context );
1059  if ( layer )
1060  layers << layer;
1061  }
1062 
1063  return layers;
1064 }
1065 
1066 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1067 {
1068  if ( !definition )
1069  return QList<double>();
1070 
1071  QStringList resultStringList;
1072  QVariant val = parameters.value( definition->name() );
1073  if ( val.canConvert<QgsProperty>() )
1074  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1075  else if ( val.type() == QVariant::List )
1076  {
1077  Q_FOREACH ( const QVariant &var, val.toList() )
1078  resultStringList << var.toString();
1079  }
1080  else
1081  resultStringList << val.toString();
1082 
1083  if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
1084  {
1085  resultStringList.clear();
1086  // check default
1087  if ( definition->defaultValue().type() == QVariant::List )
1088  {
1089  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1090  resultStringList << var.toString();
1091  }
1092  else
1093  resultStringList << definition->defaultValue().toString();
1094  }
1095 
1096  if ( resultStringList.size() == 1 )
1097  {
1098  resultStringList = resultStringList.at( 0 ).split( ',' );
1099  }
1100 
1101  if ( resultStringList.size() < 2 )
1102  return QList< double >() << 0.0 << 0.0;
1103 
1104  return QList< double >() << resultStringList.at( 0 ).toDouble() << resultStringList.at( 1 ).toDouble();
1105 }
1106 
1107 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
1108 {
1109  if ( !definition )
1110  return QStringList();
1111 
1112  QStringList resultStringList;
1113  QVariant val = parameters.value( definition->name() );
1114  if ( val.isValid() )
1115  {
1116  if ( val.canConvert<QgsProperty>() )
1117  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1118  else if ( val.type() == QVariant::List )
1119  {
1120  Q_FOREACH ( const QVariant &var, val.toList() )
1121  resultStringList << var.toString();
1122  }
1123  else
1124  resultStringList.append( val.toString().split( ';' ) );
1125  }
1126 
1127  if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
1128  {
1129  resultStringList.clear();
1130  // check default
1131  if ( definition->defaultValue().isValid() )
1132  {
1133  if ( definition->defaultValue().type() == QVariant::List )
1134  {
1135  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1136  resultStringList << var.toString();
1137  }
1138  else
1139  resultStringList.append( definition->defaultValue().toString().split( ';' ) );
1140  }
1141  }
1142 
1143  return resultStringList;
1144 }
1145 
1147 {
1148  QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
1149  QString name = map.value( QStringLiteral( "name" ) ).toString();
1150  std::unique_ptr< QgsProcessingParameterDefinition > def;
1152  def.reset( new QgsProcessingParameterBoolean( name ) );
1153  else if ( type == QgsProcessingParameterCrs::typeName() )
1154  def.reset( new QgsProcessingParameterCrs( name ) );
1155  else if ( type == QgsProcessingParameterMapLayer::typeName() )
1156  def.reset( new QgsProcessingParameterMapLayer( name ) );
1157  else if ( type == QgsProcessingParameterExtent::typeName() )
1158  def.reset( new QgsProcessingParameterExtent( name ) );
1159  else if ( type == QgsProcessingParameterPoint::typeName() )
1160  def.reset( new QgsProcessingParameterPoint( name ) );
1161  else if ( type == QgsProcessingParameterFile::typeName() )
1162  def.reset( new QgsProcessingParameterFile( name ) );
1163  else if ( type == QgsProcessingParameterMatrix::typeName() )
1164  def.reset( new QgsProcessingParameterMatrix( name ) );
1166  def.reset( new QgsProcessingParameterMultipleLayers( name ) );
1167  else if ( type == QgsProcessingParameterNumber::typeName() )
1168  def.reset( new QgsProcessingParameterNumber( name ) );
1169  else if ( type == QgsProcessingParameterRange::typeName() )
1170  def.reset( new QgsProcessingParameterRange( name ) );
1171  else if ( type == QgsProcessingParameterRasterLayer::typeName() )
1172  def.reset( new QgsProcessingParameterRasterLayer( name ) );
1173  else if ( type == QgsProcessingParameterEnum::typeName() )
1174  def.reset( new QgsProcessingParameterEnum( name ) );
1175  else if ( type == QgsProcessingParameterString::typeName() )
1176  def.reset( new QgsProcessingParameterString( name ) );
1177  else if ( type == QgsProcessingParameterExpression::typeName() )
1178  def.reset( new QgsProcessingParameterExpression( name ) );
1179  else if ( type == QgsProcessingParameterVectorLayer::typeName() )
1180  def.reset( new QgsProcessingParameterVectorLayer( name ) );
1181  else if ( type == QgsProcessingParameterField::typeName() )
1182  def.reset( new QgsProcessingParameterField( name ) );
1183  else if ( type == QgsProcessingParameterFeatureSource::typeName() )
1184  def.reset( new QgsProcessingParameterFeatureSource( name ) );
1185  else if ( type == QgsProcessingParameterFeatureSink::typeName() )
1186  def.reset( new QgsProcessingParameterFeatureSink( name ) );
1188  def.reset( new QgsProcessingParameterVectorDestination( name ) );
1190  def.reset( new QgsProcessingParameterRasterDestination( name ) );
1192  def.reset( new QgsProcessingParameterFileDestination( name ) );
1194  def.reset( new QgsProcessingParameterFolderDestination( name ) );
1195  else if ( type == QgsProcessingParameterBand::typeName() )
1196  def.reset( new QgsProcessingParameterBand( name ) );
1197  else
1198  {
1200  if ( paramType )
1201  def.reset( paramType->create( name ) );
1202  }
1203 
1204  if ( !def )
1205  return nullptr;
1206 
1207  def->fromVariantMap( map );
1208  return def.release();
1209 }
1210 
1211 QString QgsProcessingParameters::descriptionFromName( const QString &name )
1212 {
1213  QString desc = name;
1214  desc.replace( '_', ' ' );
1215  return desc;
1216 }
1217 
1219 {
1220  bool isOptional = false;
1221  QString name;
1222  QString definition;
1223  QString type;
1224  if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
1225  return nullptr;
1226 
1227  QString description = descriptionFromName( name );
1228 
1229  if ( type == QStringLiteral( "boolean" ) )
1230  return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
1231  else if ( type == QStringLiteral( "crs" ) )
1232  return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
1233  else if ( type == QStringLiteral( "layer" ) )
1234  return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
1235  else if ( type == QStringLiteral( "extent" ) )
1236  return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
1237  else if ( type == QStringLiteral( "point" ) )
1238  return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
1239  else if ( type == QStringLiteral( "file" ) )
1240  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
1241  else if ( type == QStringLiteral( "folder" ) )
1242  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
1243  else if ( type == QStringLiteral( "matrix" ) )
1244  return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
1245  else if ( type == QStringLiteral( "multiple" ) )
1246  return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
1247  else if ( type == QStringLiteral( "number" ) )
1248  return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
1249  else if ( type == QStringLiteral( "range" ) )
1250  return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
1251  else if ( type == QStringLiteral( "raster" ) )
1252  return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
1253  else if ( type == QStringLiteral( "enum" ) )
1254  return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
1255  else if ( type == QStringLiteral( "string" ) )
1256  return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
1257  else if ( type == QStringLiteral( "expression" ) )
1258  return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
1259  else if ( type == QStringLiteral( "field" ) )
1260  return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
1261  else if ( type == QStringLiteral( "vector" ) )
1262  return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
1263  else if ( type == QStringLiteral( "source" ) )
1264  return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
1265  else if ( type == QStringLiteral( "sink" ) )
1266  return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
1267  else if ( type == QStringLiteral( "vectordestination" ) )
1268  return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
1269  else if ( type == QStringLiteral( "rasterdestination" ) )
1270  return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
1271  else if ( type == QStringLiteral( "filedestination" ) )
1272  return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
1273  else if ( type == QStringLiteral( "folderdestination" ) )
1274  return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
1275  else if ( type == QStringLiteral( "band" ) )
1276  return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
1277 
1278  return nullptr;
1279 }
1280 
1281 bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
1282 {
1283  QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
1284  QRegularExpressionMatch m = re.match( code );
1285  if ( !m.hasMatch() )
1286  return false;
1287 
1288  name = m.captured( 1 );
1289  QString tokens = m.captured( 2 );
1290  if ( tokens.startsWith( QStringLiteral( "optional" ), Qt::CaseInsensitive ) )
1291  {
1292  isOptional = true;
1293  tokens.remove( 0, 8 ); // length "optional" = 8
1294  }
1295  else
1296  {
1297  isOptional = false;
1298  }
1299 
1300  tokens = tokens.trimmed();
1301 
1302  QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
1303  m = re2.match( tokens );
1304  if ( !m.hasMatch() )
1305  {
1306  type = tokens.toLower().trimmed();
1307  definition.clear();
1308  }
1309  else
1310  {
1311  type = m.captured( 1 ).toLower().trimmed();
1312  definition = m.captured( 2 );
1313  }
1314  return true;
1315 }
1316 
1317 //
1318 // QgsProcessingParameterDefinition
1319 //
1320 
1321 QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1322  : mName( name )
1323  , mDescription( description )
1324  , mDefault( defaultValue )
1325  , mFlags( optional ? FlagOptional : 0 )
1326 {}
1327 
1329 {
1330  if ( !input.isValid() && !mDefault.isValid() )
1331  return mFlags & FlagOptional;
1332 
1333  if ( ( input.type() == QVariant::String && input.toString().isEmpty() )
1334  || ( !input.isValid() && mDefault.type() == QVariant::String && mDefault.toString().isEmpty() ) )
1335  return mFlags & FlagOptional;
1336 
1337  return true;
1338 }
1339 
1341 {
1342  if ( !value.isValid() )
1343  return QStringLiteral( "None" );
1344 
1345  if ( value.canConvert<QgsProperty>() )
1346  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1347 
1348  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
1349 }
1350 
1352 {
1353  QString code = QStringLiteral( "##%1=" ).arg( mName );
1354  if ( mFlags & FlagOptional )
1355  code += QStringLiteral( "optional " );
1356  code += type() + ' ';
1357  code += mDefault.toString();
1358  return code.trimmed();
1359 }
1360 
1362 {
1363  QVariantMap map;
1364  map.insert( QStringLiteral( "parameter_type" ), type() );
1365  map.insert( QStringLiteral( "name" ), mName );
1366  map.insert( QStringLiteral( "description" ), mDescription );
1367  map.insert( QStringLiteral( "default" ), mDefault );
1368  map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
1369  map.insert( QStringLiteral( "metadata" ), mMetadata );
1370  return map;
1371 }
1372 
1374 {
1375  mName = map.value( QStringLiteral( "name" ) ).toString();
1376  mDescription = map.value( QStringLiteral( "description" ) ).toString();
1377  mDefault = map.value( QStringLiteral( "default" ) );
1378  mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
1379  mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
1380  return true;
1381 }
1382 
1384 {
1385  return mAlgorithm;
1386 }
1387 
1389 {
1390  return mAlgorithm ? mAlgorithm->provider() : nullptr;
1391 }
1392 
1394 {
1395  return QStringLiteral( "<p><b>%1</b></p><p>%2</p>" ).arg(
1396  description(),
1397  QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
1398 }
1399 
1400 QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1401  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1402 {}
1403 
1405 {
1406  return new QgsProcessingParameterBoolean( *this );
1407 }
1408 
1410 {
1411  if ( !val.isValid() )
1412  return QStringLiteral( "None" );
1413 
1414  if ( val.canConvert<QgsProperty>() )
1415  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1416  return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
1417 }
1418 
1420 {
1421  QString code = QStringLiteral( "##%1=" ).arg( mName );
1422  if ( mFlags & FlagOptional )
1423  code += QStringLiteral( "optional " );
1424  code += type() + ' ';
1425  code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
1426  return code.trimmed();
1427 }
1428 
1429 QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1430 {
1431  return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
1432 }
1433 
1434 QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1435  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1436 {
1437 
1438 }
1439 
1441 {
1442  return new QgsProcessingParameterCrs( *this );
1443 }
1444 
1446 {
1447  if ( !input.isValid() )
1448  return mFlags & FlagOptional;
1449 
1450  if ( input.canConvert<QgsProcessingFeatureSourceDefinition>() )
1451  {
1452  return true;
1453  }
1454 
1455  if ( input.canConvert<QgsProperty>() )
1456  {
1457  return true;
1458  }
1459 
1460  // direct map layer value
1461  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1462  return true;
1463 
1464  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1465  return mFlags & FlagOptional;
1466 
1467  return true;
1468 }
1469 
1470 QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1471 {
1472  if ( !value.isValid() )
1473  return QStringLiteral( "None" );
1474 
1475  if ( value.canConvert<QgsProperty>() )
1476  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1477 
1478  QVariantMap p;
1479  p.insert( name(), value );
1480  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1481  if ( layer )
1483 
1485 }
1486 
1487 QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1488 {
1489  return new QgsProcessingParameterCrs( name, description, definition.toLower() == QStringLiteral( "none" ) ? QVariant() : definition, isOptional );
1490 }
1491 
1492 QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1493  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1494 {
1495 
1496 }
1497 
1499 {
1500  return new QgsProcessingParameterMapLayer( *this );
1501 }
1502 
1504 {
1505  if ( !input.isValid() )
1506  return mFlags & FlagOptional;
1507 
1508  if ( input.canConvert<QgsProperty>() )
1509  {
1510  return true;
1511  }
1512 
1513  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1514  {
1515  return true;
1516  }
1517 
1518  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1519  return mFlags & FlagOptional;
1520 
1521  if ( !context )
1522  {
1523  // that's as far as we can get without a context
1524  return true;
1525  }
1526 
1527  // try to load as layer
1528  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
1529  return true;
1530 
1531  return false;
1532 }
1533 
1535 {
1536  if ( !val.isValid() )
1537  return QStringLiteral( "None" );
1538 
1539  if ( val.canConvert<QgsProperty>() )
1540  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1541 
1542  QVariantMap p;
1543  p.insert( name(), val );
1544  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1546  : QString();
1547 }
1548 
1549 QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1550 {
1551  return new QgsProcessingParameterMapLayer( name, description, definition, isOptional );
1552 }
1553 
1554 QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1555  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1556 {
1557 
1558 }
1559 
1561 {
1562  return new QgsProcessingParameterExtent( *this );
1563 }
1564 
1566 {
1567  if ( !input.isValid() )
1568  return mFlags & FlagOptional;
1569 
1570  if ( input.canConvert<QgsProperty>() )
1571  {
1572  return true;
1573  }
1574 
1575  if ( input.canConvert< QgsRectangle >() )
1576  {
1577  QgsRectangle r = input.value<QgsRectangle>();
1578  return !r.isNull();
1579  }
1580  if ( input.canConvert< QgsReferencedRectangle >() )
1581  {
1583  return !r.isNull();
1584  }
1585 
1586  // direct map layer value
1587  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1588  return true;
1589 
1590  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1591  return mFlags & FlagOptional;
1592 
1593  if ( !context )
1594  {
1595  // that's as far as we can get without a context
1596  return true;
1597  }
1598 
1599  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1600  QRegularExpressionMatch match = rx.match( input.toString() );
1601  if ( match.hasMatch() )
1602  {
1603  bool xMinOk = false;
1604  ( void )match.captured( 1 ).toDouble( &xMinOk );
1605  bool xMaxOk = false;
1606  ( void )match.captured( 2 ).toDouble( &xMaxOk );
1607  bool yMinOk = false;
1608  ( void )match.captured( 3 ).toDouble( &yMinOk );
1609  bool yMaxOk = false;
1610  ( void )match.captured( 4 ).toDouble( &yMaxOk );
1611  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1612  return true;
1613  }
1614 
1615  // try as layer extent
1616  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
1617 }
1618 
1619 QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1620 {
1621  if ( !value.isValid() )
1622  return QStringLiteral( "None" );
1623 
1624  if ( value.canConvert<QgsProperty>() )
1625  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1626 
1627  if ( value.canConvert< QgsRectangle >() )
1628  {
1629  QgsRectangle r = value.value<QgsRectangle>();
1630  return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
1631  qgsDoubleToString( r.yMinimum() ),
1632  qgsDoubleToString( r.xMaximum() ),
1633  qgsDoubleToString( r.yMaximum() ) );
1634  }
1635  if ( value.canConvert< QgsReferencedRectangle >() )
1636  {
1638  return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
1639  qgsDoubleToString( r.yMinimum() ),
1640  qgsDoubleToString( r.xMaximum() ),
1641  qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
1642  }
1643 
1644  QVariantMap p;
1645  p.insert( name(), value );
1646  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1647  if ( layer )
1649 
1651 }
1652 
1653 QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1654 {
1655  return new QgsProcessingParameterExtent( name, description, definition, isOptional );
1656 }
1657 
1658 QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1659  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1660 {
1661 
1662 }
1663 
1665 {
1666  return new QgsProcessingParameterPoint( *this );
1667 }
1668 
1670 {
1671  if ( !input.isValid() )
1672  return mFlags & FlagOptional;
1673 
1674  if ( input.canConvert<QgsProperty>() )
1675  {
1676  return true;
1677  }
1678 
1679  if ( input.canConvert< QgsPointXY >() )
1680  {
1681  return true;
1682  }
1683  if ( input.canConvert< QgsReferencedPointXY >() )
1684  {
1685  return true;
1686  }
1687 
1688  if ( input.type() == QVariant::String )
1689  {
1690  if ( input.toString().isEmpty() )
1691  return mFlags & FlagOptional;
1692  }
1693 
1694  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1695 
1696  QRegularExpressionMatch match = rx.match( input.toString() );
1697  if ( match.hasMatch() )
1698  {
1699  bool xOk = false;
1700  ( void )match.captured( 1 ).toDouble( &xOk );
1701  bool yOk = false;
1702  ( void )match.captured( 2 ).toDouble( &yOk );
1703  return xOk && yOk;
1704  }
1705  else
1706  return false;
1707 }
1708 
1709 QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1710 {
1711  if ( !value.isValid() )
1712  return QStringLiteral( "None" );
1713 
1714  if ( value.canConvert<QgsProperty>() )
1715  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1716 
1717  if ( value.canConvert< QgsPointXY >() )
1718  {
1719  QgsPointXY r = value.value<QgsPointXY>();
1720  return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
1721  qgsDoubleToString( r.y() ) );
1722  }
1723  if ( value.canConvert< QgsReferencedPointXY >() )
1724  {
1725  QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
1726  return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
1727  qgsDoubleToString( r.y() ),
1728  r.crs().authid() );
1729  }
1730 
1732 }
1733 
1734 QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1735 {
1736  return new QgsProcessingParameterPoint( name, description, definition, isOptional );
1737 }
1738 
1739 QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional )
1740  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1741  , mBehavior( behavior )
1742  , mExtension( extension )
1743 {
1744 
1745 }
1746 
1748 {
1749  return new QgsProcessingParameterFile( *this );
1750 }
1751 
1753 {
1754  if ( !input.isValid() )
1755  return mFlags & FlagOptional;
1756 
1757  if ( input.canConvert<QgsProperty>() )
1758  {
1759  return true;
1760  }
1761 
1762  QString string = input.toString().trimmed();
1763 
1764  if ( input.type() != QVariant::String || string.isEmpty() )
1765  return mFlags & FlagOptional;
1766 
1767  switch ( mBehavior )
1768  {
1769  case File:
1770  {
1771  if ( !mExtension.isEmpty() )
1772  return string.endsWith( mExtension, Qt::CaseInsensitive );
1773  return true;
1774  }
1775 
1776  case Folder:
1777  return true;
1778  }
1779  return true;
1780 }
1781 
1783 {
1784  QString code = QStringLiteral( "##%1=" ).arg( mName );
1785  if ( mFlags & FlagOptional )
1786  code += QStringLiteral( "optional " );
1787  code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
1788  code += mDefault.toString();
1789  return code.trimmed();
1790 }
1791 
1793 {
1795  map.insert( QStringLiteral( "behavior" ), mBehavior );
1796  map.insert( QStringLiteral( "extension" ), mExtension );
1797  return map;
1798 }
1799 
1800 bool QgsProcessingParameterFile::fromVariantMap( const QVariantMap &map )
1801 {
1803  mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
1804  mExtension = map.value( QStringLiteral( "extension" ) ).toString();
1805  return true;
1806 }
1807 
1809 {
1810  return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
1811 }
1812 
1813 QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
1814  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1815  , mHeaders( headers )
1816  , mNumberRows( numberRows )
1817  , mFixedNumberRows( fixedNumberRows )
1818 {
1819 
1820 }
1821 
1823 {
1824  return new QgsProcessingParameterMatrix( *this );
1825 }
1826 
1828 {
1829  if ( !input.isValid() )
1830  return mFlags & FlagOptional;
1831 
1832  if ( input.type() == QVariant::String )
1833  {
1834  if ( input.toString().isEmpty() )
1835  return mFlags & FlagOptional;
1836  return true;
1837  }
1838  else if ( input.type() == QVariant::List )
1839  {
1840  if ( input.toList().isEmpty() )
1841  return mFlags & FlagOptional;
1842  return true;
1843  }
1844  else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
1845  {
1846  return true;
1847  }
1848 
1849  return false;
1850 }
1851 
1852 QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1853 {
1854  if ( !value.isValid() )
1855  return QStringLiteral( "None" );
1856 
1857  if ( value.canConvert<QgsProperty>() )
1858  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1859 
1860  QVariantMap p;
1861  p.insert( name(), value );
1862  QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
1863 
1864  QStringList parts;
1865  Q_FOREACH ( const QVariant &v, list )
1866  {
1867  if ( v.type() == QVariant::List )
1868  {
1869  QStringList parts2;
1870  Q_FOREACH ( const QVariant &v2, v.toList() )
1871  {
1872  if ( v2.isNull() || !v2.isValid() )
1873  parts2 << QStringLiteral( "None" );
1874  else if ( v2.toString().isEmpty() )
1875  parts2 << QStringLiteral( "''" );
1876  else
1877  parts2 << v2.toString();
1878  }
1879  parts << parts2.join( ',' ).prepend( '[' ).append( ']' );
1880  }
1881  else
1882  {
1883  if ( v.isNull() || !v.isValid() )
1884  parts << QStringLiteral( "None" );
1885  else if ( v.toString().isEmpty() )
1886  parts << QStringLiteral( "''" );
1887  else
1888  parts << v.toString();
1889  }
1890  }
1891 
1892  return parts.join( ',' ).prepend( '[' ).append( ']' );
1893 }
1894 
1896 {
1897  return mHeaders;
1898 }
1899 
1901 {
1902  mHeaders = headers;
1903 }
1904 
1906 {
1907  return mNumberRows;
1908 }
1909 
1911 {
1912  mNumberRows = numberRows;
1913 }
1914 
1916 {
1917  return mFixedNumberRows;
1918 }
1919 
1921 {
1922  mFixedNumberRows = fixedNumberRows;
1923 }
1924 
1926 {
1928  map.insert( QStringLiteral( "headers" ), mHeaders );
1929  map.insert( QStringLiteral( "rows" ), mNumberRows );
1930  map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
1931  return map;
1932 }
1933 
1934 bool QgsProcessingParameterMatrix::fromVariantMap( const QVariantMap &map )
1935 {
1937  mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
1938  mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
1939  mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
1940  return true;
1941 }
1942 
1943 QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1944 {
1945  return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
1946 }
1947 
1949  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1950  , mLayerType( layerType )
1951 {
1952 
1953 }
1954 
1956 {
1957  return new QgsProcessingParameterMultipleLayers( *this );
1958 }
1959 
1961 {
1962  if ( !input.isValid() )
1963  return mFlags & FlagOptional;
1964 
1965  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1966  {
1967  return true;
1968  }
1969 
1970  if ( input.type() == QVariant::String )
1971  {
1972  if ( input.toString().isEmpty() )
1973  return mFlags & FlagOptional;
1974 
1975  if ( mMinimumNumberInputs > 1 )
1976  return false;
1977 
1978  if ( !context )
1979  return true;
1980 
1981  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
1982  }
1983  else if ( input.type() == QVariant::List )
1984  {
1985  if ( input.toList().count() < mMinimumNumberInputs )
1986  return mFlags & FlagOptional;
1987 
1988  if ( mMinimumNumberInputs > input.toList().count() )
1989  return false;
1990 
1991  if ( !context )
1992  return true;
1993 
1994  Q_FOREACH ( const QVariant &v, input.toList() )
1995  {
1996  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
1997  continue;
1998 
1999  if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
2000  return false;
2001  }
2002  return true;
2003  }
2004  else if ( input.type() == QVariant::StringList )
2005  {
2006  if ( input.toStringList().count() < mMinimumNumberInputs )
2007  return mFlags & FlagOptional;
2008 
2009  if ( mMinimumNumberInputs > input.toStringList().count() )
2010  return false;
2011 
2012  if ( !context )
2013  return true;
2014 
2015  Q_FOREACH ( const QString &v, input.toStringList() )
2016  {
2017  if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
2018  return false;
2019  }
2020  return true;
2021  }
2022  return false;
2023 }
2024 
2026 {
2027  if ( !value.isValid() )
2028  return QStringLiteral( "None" );
2029 
2030  if ( value.canConvert<QgsProperty>() )
2031  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2032 
2033  QVariantMap p;
2034  p.insert( name(), value );
2035  QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
2036  if ( !list.isEmpty() )
2037  {
2038  QStringList parts;
2039  Q_FOREACH ( const QgsMapLayer *layer, list )
2040  {
2042  }
2043  return parts.join( ',' ).prepend( '[' ).append( ']' );
2044  }
2045 
2047 }
2048 
2050 {
2051  QString code = QStringLiteral( "##%1=" ).arg( mName );
2052  if ( mFlags & FlagOptional )
2053  code += QStringLiteral( "optional " );
2054  switch ( mLayerType )
2055  {
2057  code += QStringLiteral( "multiple raster" );
2058  break;
2059 
2061  code += QStringLiteral( "multiple file" );
2062  break;
2063 
2064  default:
2065  code += QStringLiteral( "multiple vector" );
2066  break;
2067  }
2068  code += ' ';
2069  if ( mDefault.type() == QVariant::List )
2070  {
2071  QStringList parts;
2072  Q_FOREACH ( const QVariant &var, mDefault.toList() )
2073  {
2074  parts << var.toString();
2075  }
2076  code += parts.join( ',' );
2077  }
2078  else if ( mDefault.type() == QVariant::StringList )
2079  {
2080  code += mDefault.toStringList().join( ',' );
2081  }
2082  else
2083  {
2084  code += mDefault.toString();
2085  }
2086  return code.trimmed();
2087 }
2088 
2090 {
2091  return mLayerType;
2092 }
2093 
2095 {
2096  mLayerType = type;
2097 }
2098 
2100 {
2101  return mMinimumNumberInputs;
2102 }
2103 
2105 {
2106  if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
2107  mMinimumNumberInputs = minimumNumberInputs;
2108 }
2109 
2111 {
2113  map.insert( QStringLiteral( "layer_type" ), mLayerType );
2114  map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
2115  return map;
2116 }
2117 
2119 {
2121  mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
2122  mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
2123  return true;
2124 }
2125 
2126 QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2127 {
2128  QString type = definition;
2129  QString defaultVal;
2130  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
2131  QRegularExpressionMatch m = re.match( definition );
2132  if ( m.hasMatch() )
2133  {
2134  type = m.captured( 1 ).toLower().trimmed();
2135  defaultVal = m.captured( 2 );
2136  }
2138  if ( type == QStringLiteral( "vector" ) )
2140  else if ( type == QStringLiteral( "raster" ) )
2141  layerType = QgsProcessing::TypeRaster;
2142  else if ( type == QStringLiteral( "file" ) )
2143  layerType = QgsProcessing::TypeFile;
2144  return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
2145 }
2146 
2147 QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
2148  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2149  , mMin( minValue )
2150  , mMax( maxValue )
2151  , mDataType( type )
2152 {
2153  if ( mMin >= mMax )
2154  {
2155  QgsMessageLog::logMessage( QObject::tr( "Invalid number parameter \"%1\": min value %2 is >= max value %3!" ).arg( name ).arg( mMin ).arg( mMax ), QObject::tr( "Processing" ) );
2156  }
2157 }
2158 
2160 {
2161  return new QgsProcessingParameterNumber( *this );
2162 }
2163 
2165 {
2166  QVariant input = value;
2167  if ( !input.isValid() )
2168  {
2169  if ( !defaultValue().isValid() )
2170  return mFlags & FlagOptional;
2171 
2172  input = defaultValue();
2173  }
2174 
2175  if ( input.canConvert<QgsProperty>() )
2176  {
2177  return true;
2178  }
2179 
2180  bool ok = false;
2181  double res = input.toDouble( &ok );
2182  if ( !ok )
2183  return mFlags & FlagOptional;
2184 
2185  return !( res < mMin || res > mMax );
2186 }
2187 
2189 {
2190  if ( !value.isValid() )
2191  return QStringLiteral( "None" );
2192 
2193  if ( value.canConvert<QgsProperty>() )
2194  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2195 
2196  return value.toString();
2197 }
2198 
2200 {
2202  QStringList parts;
2203  if ( mMin > std::numeric_limits<double>::lowest() + 1 )
2204  parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
2205  if ( mMax < std::numeric_limits<double>::max() )
2206  parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
2207  if ( mDefault.isValid() )
2208  parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
2209  QString extra = parts.join( QStringLiteral( "<br />" ) );
2210  if ( !extra.isEmpty() )
2211  text += QStringLiteral( "<p>%1</p>" ).arg( extra );
2212  return text;
2213 }
2214 
2216 {
2217  return mMin;
2218 }
2219 
2221 {
2222  mMin = min;
2223 }
2224 
2226 {
2227  return mMax;
2228 }
2229 
2231 {
2232  mMax = max;
2233 }
2234 
2236 {
2237  return mDataType;
2238 }
2239 
2241 {
2242  mDataType = dataType;
2243 }
2244 
2246 {
2248  map.insert( QStringLiteral( "min" ), mMin );
2249  map.insert( QStringLiteral( "max" ), mMax );
2250  map.insert( QStringLiteral( "data_type" ), mDataType );
2251  return map;
2252 }
2253 
2254 bool QgsProcessingParameterNumber::fromVariantMap( const QVariantMap &map )
2255 {
2257  mMin = map.value( QStringLiteral( "min" ) ).toDouble();
2258  mMax = map.value( QStringLiteral( "max" ) ).toDouble();
2259  mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2260  return true;
2261 }
2262 
2263 QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2264 {
2265  return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
2266  : ( definition.toLower().trimmed() == QStringLiteral( "none" ) ? QVariant() : definition ), isOptional );
2267 }
2268 
2270  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2271  , mDataType( type )
2272 {
2273 
2274 }
2275 
2277 {
2278  return new QgsProcessingParameterRange( *this );
2279 }
2280 
2282 {
2283  if ( !input.isValid() )
2284  return mFlags & FlagOptional;
2285 
2286  if ( input.canConvert<QgsProperty>() )
2287  {
2288  return true;
2289  }
2290 
2291  if ( input.type() == QVariant::String )
2292  {
2293  QStringList list = input.toString().split( ',' );
2294  if ( list.count() != 2 )
2295  return mFlags & FlagOptional;
2296  bool ok = false;
2297  list.at( 0 ).toDouble( &ok );
2298  bool ok2 = false;
2299  list.at( 1 ).toDouble( &ok2 );
2300  if ( !ok || !ok2 )
2301  return mFlags & FlagOptional;
2302  return true;
2303  }
2304  else if ( input.type() == QVariant::List )
2305  {
2306  if ( input.toList().count() != 2 )
2307  return mFlags & FlagOptional;
2308 
2309  bool ok = false;
2310  input.toList().at( 0 ).toDouble( &ok );
2311  bool ok2 = false;
2312  input.toList().at( 1 ).toDouble( &ok2 );
2313  if ( !ok || !ok2 )
2314  return mFlags & FlagOptional;
2315  return true;
2316  }
2317 
2318  return false;
2319 }
2320 
2321 QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2322 {
2323  if ( !value.isValid() )
2324  return QStringLiteral( "None" );
2325 
2326  if ( value.canConvert<QgsProperty>() )
2327  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2328 
2329  QVariantMap p;
2330  p.insert( name(), value );
2331  QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
2332 
2333  QStringList stringParts;
2334  Q_FOREACH ( double v, parts )
2335  {
2336  stringParts << QString::number( v );
2337  }
2338  return stringParts.join( ',' ).prepend( '[' ).append( ']' );
2339 }
2340 
2342 {
2343  return mDataType;
2344 }
2345 
2347 {
2348  mDataType = dataType;
2349 }
2350 
2352 {
2354  map.insert( QStringLiteral( "data_type" ), mDataType );
2355  return map;
2356 }
2357 
2358 bool QgsProcessingParameterRange::fromVariantMap( const QVariantMap &map )
2359 {
2361  mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2362  return true;
2363 }
2364 
2365 QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2366 {
2367  return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant() : definition, isOptional );
2368 }
2369 
2370 QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2371  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2372 {
2373 
2374 }
2375 
2377 {
2378  return new QgsProcessingParameterRasterLayer( *this );
2379 }
2380 
2382 {
2383  if ( !input.isValid() )
2384  return mFlags & FlagOptional;
2385 
2386  if ( input.canConvert<QgsProperty>() )
2387  {
2388  return true;
2389  }
2390 
2391  if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
2392  return true;
2393 
2394  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2395  return mFlags & FlagOptional;
2396 
2397  if ( !context )
2398  {
2399  // that's as far as we can get without a context
2400  return true;
2401  }
2402 
2403  // try to load as layer
2404  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
2405  return true;
2406 
2407  return false;
2408 }
2409 
2411 {
2412  if ( !val.isValid() )
2413  return QStringLiteral( "None" );
2414 
2415  if ( val.canConvert<QgsProperty>() )
2416  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2417 
2418  QVariantMap p;
2419  p.insert( name(), val );
2422 }
2423 
2424 QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2425 {
2426  return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
2427 }
2428 
2429 QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional )
2430  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2431  , mOptions( options )
2432  , mAllowMultiple( allowMultiple )
2433 {
2434 
2435 }
2436 
2438 {
2439  return new QgsProcessingParameterEnum( *this );
2440 }
2441 
2443 {
2444  QVariant input = value;
2445  if ( !input.isValid() )
2446  {
2447  if ( !defaultValue().isValid() )
2448  return mFlags & FlagOptional;
2449 
2450  input = defaultValue();
2451  }
2452 
2453  if ( input.canConvert<QgsProperty>() )
2454  {
2455  return true;
2456  }
2457 
2458  if ( input.type() == QVariant::List )
2459  {
2460  if ( !mAllowMultiple )
2461  return false;
2462 
2463  const QVariantList values = input.toList();
2464  if ( values.empty() && !( mFlags & FlagOptional ) )
2465  return false;
2466 
2467  for ( const QVariant &val : values )
2468  {
2469  bool ok = false;
2470  int res = val.toInt( &ok );
2471  if ( !ok )
2472  return false;
2473  else if ( res < 0 || res >= mOptions.count() )
2474  return false;
2475  }
2476 
2477  return true;
2478  }
2479  else if ( input.type() == QVariant::String )
2480  {
2481  QStringList parts = input.toString().split( ',' );
2482  if ( parts.count() > 1 && !mAllowMultiple )
2483  return false;
2484 
2485  Q_FOREACH ( const QString &part, parts )
2486  {
2487  bool ok = false;
2488  int res = part.toInt( &ok );
2489  if ( !ok )
2490  return false;
2491  else if ( res < 0 || res >= mOptions.count() )
2492  return false;
2493  }
2494  return true;
2495  }
2496  else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
2497  {
2498  bool ok = false;
2499  int res = input.toInt( &ok );
2500  if ( !ok )
2501  return false;
2502  else if ( res >= 0 && res < mOptions.count() )
2503  return true;
2504  }
2505  return false;
2506 }
2507 
2509 {
2510  if ( !value.isValid() )
2511  return QStringLiteral( "None" );
2512 
2513  if ( value.canConvert<QgsProperty>() )
2514  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2515 
2516  if ( value.type() == QVariant::List )
2517  {
2518  QStringList parts;
2519  Q_FOREACH ( const QVariant &val, value.toList() )
2520  {
2521  parts << QString::number( static_cast< int >( val.toDouble() ) );
2522  }
2523  return parts.join( ',' ).prepend( '[' ).append( ']' );
2524  }
2525  else if ( value.type() == QVariant::String )
2526  {
2527  QStringList parts = value.toString().split( ',' );
2528  if ( parts.count() > 1 )
2529  {
2530  return parts.join( ',' ).prepend( '[' ).append( ']' );
2531  }
2532  }
2533 
2534  return QString::number( static_cast< int >( value.toDouble() ) );
2535 }
2536 
2538 {
2539  QString code = QStringLiteral( "##%1=" ).arg( mName );
2540  if ( mFlags & FlagOptional )
2541  code += QStringLiteral( "optional " );
2542  code += QStringLiteral( "enum " );
2543 
2544  if ( mAllowMultiple )
2545  code += QStringLiteral( "multiple " );
2546 
2547  code += mOptions.join( ';' ) + ' ';
2548 
2549  code += mDefault.toString();
2550  return code.trimmed();
2551 }
2552 
2554 {
2555  return mOptions;
2556 }
2557 
2559 {
2560  mOptions = options;
2561 }
2562 
2564 {
2565  return mAllowMultiple;
2566 }
2567 
2569 {
2570  mAllowMultiple = allowMultiple;
2571 }
2572 
2574 {
2576  map.insert( QStringLiteral( "options" ), mOptions );
2577  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
2578  return map;
2579 }
2580 
2581 bool QgsProcessingParameterEnum::fromVariantMap( const QVariantMap &map )
2582 {
2584  mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
2585  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
2586  return true;
2587 }
2588 
2589 QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2590 {
2591  QString defaultVal;
2592  bool multiple = false;
2593  QString def = definition;
2594  if ( def.startsWith( QStringLiteral( "multiple" ), Qt::CaseInsensitive ) )
2595  {
2596  multiple = true;
2597  def = def.mid( 9 );
2598  }
2599 
2600  QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
2601  QRegularExpressionMatch m = re.match( def );
2602  QString values = def;
2603  if ( m.hasMatch() )
2604  {
2605  values = m.captured( 1 ).trimmed();
2606  defaultVal = m.captured( 2 );
2607  }
2608 
2609  return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
2610 }
2611 
2612 QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
2613  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2614  , mMultiLine( multiLine )
2615 {
2616 
2617 }
2618 
2620 {
2621  return new QgsProcessingParameterString( *this );
2622 }
2623 
2625 {
2626  if ( !value.isValid() )
2627  return QStringLiteral( "None" );
2628 
2629  if ( value.canConvert<QgsProperty>() )
2630  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2631 
2632  QString s = value.toString();
2633  s.replace( '\n', QStringLiteral( "\\n" ) );
2635 }
2636 
2638 {
2639  QString code = QStringLiteral( "##%1=" ).arg( mName );
2640  if ( mFlags & FlagOptional )
2641  code += QStringLiteral( "optional " );
2642  code += QStringLiteral( "string " );
2643 
2644  if ( mMultiLine )
2645  code += QStringLiteral( "long " );
2646 
2647  code += mDefault.toString();
2648  return code.trimmed();
2649 }
2650 
2652 {
2653  return mMultiLine;
2654 }
2655 
2657 {
2658  mMultiLine = multiLine;
2659 }
2660 
2662 {
2664  map.insert( QStringLiteral( "multiline" ), mMultiLine );
2665  return map;
2666 }
2667 
2668 bool QgsProcessingParameterString::fromVariantMap( const QVariantMap &map )
2669 {
2671  mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
2672  return true;
2673 }
2674 
2675 QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2676 {
2677  QString def = definition;
2678  bool multiLine = false;
2679  if ( def.startsWith( QStringLiteral( "long" ), Qt::CaseInsensitive ) )
2680  {
2681  multiLine = true;
2682  def = def.mid( 5 );
2683  }
2684 
2685  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
2686  def = def.mid( 1 );
2687  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
2688  def.chop( 1 );
2689 
2690  QVariant defaultValue = def;
2691  if ( def == QStringLiteral( "None" ) )
2692  defaultValue = QVariant();
2693 
2694  return new QgsProcessingParameterString( name, description, defaultValue, multiLine, isOptional );
2695 }
2696 
2697 QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
2698  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2699  , mParentLayerParameterName( parentLayerParameterName )
2700 {
2701 
2702 }
2703 
2705 {
2706  return new QgsProcessingParameterExpression( *this );
2707 }
2708 
2710 {
2711  if ( !value.isValid() )
2712  return QStringLiteral( "None" );
2713 
2714  if ( value.canConvert<QgsProperty>() )
2715  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2716 
2717  QString s = value.toString();
2718  s.replace( '\n', QStringLiteral( "\\n" ) );
2720 }
2721 
2723 {
2724  QStringList depends;
2725  if ( !mParentLayerParameterName.isEmpty() )
2726  depends << mParentLayerParameterName;
2727  return depends;
2728 }
2729 
2731 {
2732  return mParentLayerParameterName;
2733 }
2734 
2736 {
2737  mParentLayerParameterName = parentLayerParameterName;
2738 }
2739 
2741 {
2743  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
2744  return map;
2745 }
2746 
2748 {
2750  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
2751  return true;
2752 }
2753 
2754 QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2755 {
2756  return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
2757 }
2758 
2759 QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
2760  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2762 {
2763 
2764 }
2765 
2767 {
2768  return new QgsProcessingParameterVectorLayer( *this );
2769 }
2770 
2772 {
2773  if ( !v.isValid() )
2774  return mFlags & FlagOptional;
2775 
2776  QVariant var = v;
2777 
2778  if ( var.canConvert<QgsProperty>() )
2779  {
2780  QgsProperty p = var.value< QgsProperty >();
2782  {
2783  var = p.staticValue();
2784  }
2785  else
2786  {
2787  return true;
2788  }
2789  }
2790 
2791  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
2792  return true;
2793 
2794  if ( var.type() != QVariant::String || var.toString().isEmpty() )
2795  return mFlags & FlagOptional;
2796 
2797  if ( !context )
2798  {
2799  // that's as far as we can get without a context
2800  return true;
2801  }
2802 
2803  // try to load as layer
2804  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
2805  return true;
2806 
2807  return false;
2808 }
2809 
2811 {
2812  if ( !val.isValid() )
2813  return QStringLiteral( "None" );
2814 
2815  if ( val.canConvert<QgsProperty>() )
2816  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2817 
2818  QVariantMap p;
2819  p.insert( name(), val );
2822  : QString();
2823 }
2824 
2826 {
2827  return mDataTypes;
2828 }
2829 
2831 {
2832  mDataTypes = types;
2833 }
2834 
2836 {
2838  QVariantList types;
2839  Q_FOREACH ( int type, mDataTypes )
2840  {
2841  types << type;
2842  }
2843  map.insert( QStringLiteral( "data_types" ), types );
2844  return map;
2845 }
2846 
2848 {
2850  mDataTypes.clear();
2851  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
2852  Q_FOREACH ( const QVariant &val, values )
2853  {
2854  mDataTypes << val.toInt();
2855  }
2856  return true;
2857 }
2858 
2859 QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2860 {
2861  return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
2862 }
2863 
2864 QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
2865  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2866  , mParentLayerParameterName( parentLayerParameterName )
2867  , mDataType( type )
2868  , mAllowMultiple( allowMultiple )
2869 {
2870 
2871 }
2872 
2874 {
2875  return new QgsProcessingParameterField( *this );
2876 }
2877 
2879 {
2880  if ( !input.isValid() )
2881  return mFlags & FlagOptional;
2882 
2883  if ( input.canConvert<QgsProperty>() )
2884  {
2885  return true;
2886  }
2887 
2888  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
2889  {
2890  if ( !mAllowMultiple )
2891  return false;
2892 
2893  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
2894  return false;
2895  }
2896  else if ( input.type() == QVariant::String )
2897  {
2898  if ( input.toString().isEmpty() )
2899  return mFlags & FlagOptional;
2900 
2901  QStringList parts = input.toString().split( ';' );
2902  if ( parts.count() > 1 && !mAllowMultiple )
2903  return false;
2904  }
2905  else
2906  {
2907  if ( input.toString().isEmpty() )
2908  return mFlags & FlagOptional;
2909  }
2910  return true;
2911 }
2912 
2914 {
2915  if ( !value.isValid() )
2916  return QStringLiteral( "None" );
2917 
2918  if ( value.canConvert<QgsProperty>() )
2919  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2920 
2921  if ( value.type() == QVariant::List )
2922  {
2923  QStringList parts;
2924  Q_FOREACH ( const QVariant &val, value.toList() )
2925  {
2926  parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
2927  }
2928  return parts.join( ',' ).prepend( '[' ).append( ']' );
2929  }
2930  else if ( value.type() == QVariant::StringList )
2931  {
2932  QStringList parts;
2933  Q_FOREACH ( QString s, value.toStringList() )
2934  {
2936  }
2937  return parts.join( ',' ).prepend( '[' ).append( ']' );
2938  }
2939 
2940  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
2941 }
2942 
2944 {
2945  QString code = QStringLiteral( "##%1=" ).arg( mName );
2946  if ( mFlags & FlagOptional )
2947  code += QStringLiteral( "optional " );
2948  code += QStringLiteral( "field " );
2949 
2950  switch ( mDataType )
2951  {
2952  case Numeric:
2953  code += QStringLiteral( "numeric " );
2954  break;
2955 
2956  case String:
2957  code += QStringLiteral( "string " );
2958  break;
2959 
2960  case DateTime:
2961  code += QStringLiteral( "datetime " );
2962  break;
2963 
2964  case Any:
2965  break;
2966  }
2967 
2968  if ( mAllowMultiple )
2969  code += QStringLiteral( "multiple " );
2970 
2971  code += mParentLayerParameterName + ' ';
2972 
2973  code += mDefault.toString();
2974  return code.trimmed();
2975 }
2976 
2978 {
2979  QStringList depends;
2980  if ( !mParentLayerParameterName.isEmpty() )
2981  depends << mParentLayerParameterName;
2982  return depends;
2983 }
2984 
2986 {
2987  return mParentLayerParameterName;
2988 }
2989 
2991 {
2992  mParentLayerParameterName = parentLayerParameterName;
2993 }
2994 
2996 {
2997  return mDataType;
2998 }
2999 
3001 {
3002  mDataType = dataType;
3003 }
3004 
3006 {
3007  return mAllowMultiple;
3008 }
3009 
3011 {
3012  mAllowMultiple = allowMultiple;
3013 }
3014 
3016 {
3018  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
3019  map.insert( QStringLiteral( "data_type" ), mDataType );
3020  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
3021  return map;
3022 }
3023 
3024 bool QgsProcessingParameterField::fromVariantMap( const QVariantMap &map )
3025 {
3027  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
3028  mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3029  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
3030  return true;
3031 }
3032 
3033 QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3034 {
3035  QString parent;
3036  DataType type = Any;
3037  bool allowMultiple = false;
3038  QString def = definition;
3039 
3040  if ( def.startsWith( QStringLiteral( "numeric " ), Qt::CaseInsensitive ) )
3041  {
3042  type = Numeric;
3043  def = def.mid( 8 );
3044  }
3045  else if ( def.startsWith( QStringLiteral( "string " ), Qt::CaseInsensitive ) )
3046  {
3047  type = String;
3048  def = def.mid( 7 );
3049  }
3050  else if ( def.startsWith( QStringLiteral( "datetime " ), Qt::CaseInsensitive ) )
3051  {
3052  type = DateTime;
3053  def = def.mid( 9 );
3054  }
3055 
3056  if ( def.startsWith( QStringLiteral( "multiple" ), Qt::CaseInsensitive ) )
3057  {
3058  allowMultiple = true;
3059  def = def.mid( 8 ).trimmed();
3060  }
3061 
3062  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
3063  QRegularExpressionMatch m = re.match( def );
3064  if ( m.hasMatch() )
3065  {
3066  parent = m.captured( 1 ).trimmed();
3067  def = m.captured( 2 );
3068  }
3069  else
3070  {
3071  parent = def;
3072  def.clear();
3073  }
3074 
3075  return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional );
3076 }
3077 
3078 QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
3079  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3081 {
3082 
3083 }
3084 
3086 {
3087  return new QgsProcessingParameterFeatureSource( *this );
3088 }
3089 
3091 {
3092  QVariant var = input;
3093  if ( !var.isValid() )
3094  return mFlags & FlagOptional;
3095 
3096  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
3097  {
3099  var = fromVar.source;
3100  }
3101 
3102  if ( var.canConvert<QgsProperty>() )
3103  {
3104  QgsProperty p = var.value< QgsProperty >();
3106  {
3107  var = p.staticValue();
3108  }
3109  else
3110  {
3111  return true;
3112  }
3113  }
3114  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
3115  {
3116  return true;
3117  }
3118 
3119  if ( var.type() != QVariant::String || var.toString().isEmpty() )
3120  return mFlags & FlagOptional;
3121 
3122  if ( !context )
3123  {
3124  // that's as far as we can get without a context
3125  return true;
3126  }
3127 
3128  // try to load as layer
3129  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
3130  return true;
3131 
3132  return false;
3133 }
3134 
3136 {
3137  if ( !value.isValid() )
3138  return QStringLiteral( "None" );
3139 
3140  if ( value.canConvert<QgsProperty>() )
3141  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3142 
3143  if ( value.canConvert<QgsProcessingFeatureSourceDefinition>() )
3144  {
3146  if ( fromVar.source.propertyType() == QgsProperty::StaticProperty )
3147  {
3148  if ( fromVar.selectedFeaturesOnly )
3149  {
3150  return QStringLiteral( "QgsProcessingFeatureSourceDefinition('%1', True)" ).arg( fromVar.source.staticValue().toString() );
3151  }
3152  else
3153  {
3154  QString layerString = fromVar.source.staticValue().toString();
3155  // prefer to use layer source instead of id if possible (since it's persistent)
3156  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context ) ) )
3157  layerString = layer->source();
3158  return QgsProcessingUtils::stringToPythonLiteral( layerString );
3159  }
3160  }
3161  else
3162  {
3163  if ( fromVar.selectedFeaturesOnly )
3164  {
3165  return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression('%1'), True)" ).arg( fromVar.source.asExpression() );
3166  }
3167  else
3168  {
3169  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.source.asExpression() );
3170  }
3171  }
3172  }
3173  else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
3174  {
3175  return QgsProcessingUtils::stringToPythonLiteral( layer->source() );
3176  }
3177 
3178  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3179 }
3180 
3182 {
3183  QString code = QStringLiteral( "##%1=" ).arg( mName );
3184  if ( mFlags & FlagOptional )
3185  code += QStringLiteral( "optional " );
3186  code += QStringLiteral( "source " );
3187 
3188  Q_FOREACH ( int type, mDataTypes )
3189  {
3190  switch ( type )
3191  {
3193  code += QStringLiteral( "point " );
3194  break;
3195 
3197  code += QStringLiteral( "line " );
3198  break;
3199 
3201  code += QStringLiteral( "polygon " );
3202  break;
3203 
3204  }
3205  }
3206 
3207  code += mDefault.toString();
3208  return code.trimmed();
3209 }
3210 
3212  : mDataTypes( types )
3213 {
3214 
3215 }
3216 
3218 {
3220  QVariantList types;
3221  Q_FOREACH ( int type, mDataTypes )
3222  {
3223  types << type;
3224  }
3225  map.insert( QStringLiteral( "data_types" ), types );
3226  return map;
3227 }
3228 
3230 {
3232  mDataTypes.clear();
3233  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3234  Q_FOREACH ( const QVariant &val, values )
3235  {
3236  mDataTypes << val.toInt();
3237  }
3238  return true;
3239 }
3240 
3241 QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3242 {
3243  QList< int > types;
3244  QString def = definition;
3245  while ( true )
3246  {
3247  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
3248  {
3250  def = def.mid( 6 );
3251  continue;
3252  }
3253  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
3254  {
3256  def = def.mid( 5 );
3257  continue;
3258  }
3259  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
3260  {
3262  def = def.mid( 8 );
3263  continue;
3264  }
3265  break;
3266  }
3267 
3268  return new QgsProcessingParameterFeatureSource( name, description, types, def, isOptional );
3269 }
3270 
3271 QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional, bool createByDefault )
3272  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3273  , mDataType( type )
3274 {
3275 }
3276 
3278 {
3279  return new QgsProcessingParameterFeatureSink( *this );
3280 }
3281 
3283 {
3284  QVariant var = input;
3285  if ( !var.isValid() )
3286  return mFlags & FlagOptional;
3287 
3288  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3289  {
3291  var = fromVar.sink;
3292  }
3293 
3294  if ( var.canConvert<QgsProperty>() )
3295  {
3296  QgsProperty p = var.value< QgsProperty >();
3298  {
3299  var = p.staticValue();
3300  }
3301  else
3302  {
3303  return true;
3304  }
3305  }
3306 
3307  if ( var.type() != QVariant::String )
3308  return false;
3309 
3310  if ( var.toString().isEmpty() )
3311  return mFlags & FlagOptional;
3312 
3313  return true;
3314 }
3315 
3317 {
3318  if ( !value.isValid() )
3319  return QStringLiteral( "None" );
3320 
3321  if ( value.canConvert<QgsProperty>() )
3322  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3323 
3324  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3325  {
3327  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3328  {
3329  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3330  }
3331  else
3332  {
3333  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3334  }
3335  }
3336 
3337  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3338 }
3339 
3341 {
3342  QString code = QStringLiteral( "##%1=" ).arg( mName );
3343  if ( mFlags & FlagOptional )
3344  code += QStringLiteral( "optional " );
3345  code += QStringLiteral( "sink " );
3346 
3347  switch ( mDataType )
3348  {
3350  code += QStringLiteral( "point " );
3351  break;
3352 
3354  code += QStringLiteral( "line " );
3355  break;
3356 
3358  code += QStringLiteral( "polygon " );
3359  break;
3360 
3362  code += QStringLiteral( "table " );
3363  break;
3364 
3365  default:
3366  break;
3367  }
3368 
3369  code += mDefault.toString();
3370  return code.trimmed();
3371 }
3372 
3374 {
3375  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
3376 }
3377 
3379 {
3380  if ( originalProvider() )
3381  {
3383  }
3384  else if ( QgsProcessingProvider *p = provider() )
3385  {
3386  return p->defaultVectorFileExtension( hasGeometry() );
3387  }
3388  else
3389  {
3390  QgsSettings settings;
3391  if ( hasGeometry() )
3392  {
3393  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3394  }
3395  else
3396  {
3397  return QStringLiteral( "dbf" );
3398  }
3399  }
3400 }
3401 
3403 {
3404  if ( originalProvider() )
3405  {
3407  }
3408  else if ( QgsProcessingProvider *p = provider() )
3409  {
3410  return p->supportedOutputVectorLayerExtensions();
3411  }
3412  else
3413  {
3415  }
3416 }
3417 
3419 {
3420  return mDataType;
3421 }
3422 
3424 {
3425  switch ( mDataType )
3426  {
3433  return true;
3434 
3437  return false;
3438  }
3439  return true;
3440 }
3441 
3443 {
3444  mDataType = type;
3445 }
3446 
3448 {
3450  map.insert( QStringLiteral( "data_type" ), mDataType );
3451  return map;
3452 }
3453 
3455 {
3457  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3458  return true;
3459 }
3460 
3462 {
3464  return QStringLiteral( "memory:%1" ).arg( description() );
3465  else
3467 }
3468 
3469 QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3470 {
3472  QString def = definition;
3473  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
3474  {
3476  def = def.mid( 6 );
3477  }
3478  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
3479  {
3481  def = def.mid( 5 );
3482  }
3483  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
3484  {
3486  def = def.mid( 8 );
3487  }
3488  else if ( def.startsWith( QStringLiteral( "table" ), Qt::CaseInsensitive ) )
3489  {
3491  def = def.mid( 6 );
3492  }
3493 
3494  return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
3495 }
3496 
3498  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3499 {
3500 }
3501 
3503 {
3504  return new QgsProcessingParameterRasterDestination( *this );
3505 }
3506 
3508 {
3509  QVariant var = input;
3510  if ( !var.isValid() )
3511  return mFlags & FlagOptional;
3512 
3513  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3514  {
3516  var = fromVar.sink;
3517  }
3518 
3519  if ( var.canConvert<QgsProperty>() )
3520  {
3521  QgsProperty p = var.value< QgsProperty >();
3523  {
3524  var = p.staticValue();
3525  }
3526  else
3527  {
3528  return true;
3529  }
3530  }
3531 
3532  if ( var.type() != QVariant::String )
3533  return false;
3534 
3535  if ( var.toString().isEmpty() )
3536  return mFlags & FlagOptional;
3537 
3538  return true;
3539 }
3540 
3542 {
3543  if ( !value.isValid() )
3544  return QStringLiteral( "None" );
3545 
3546  if ( value.canConvert<QgsProperty>() )
3547  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3548 
3549  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3550  {
3552  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3553  {
3554  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3555  }
3556  else
3557  {
3558  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3559  }
3560  }
3561 
3562  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3563 }
3564 
3566 {
3567  return new QgsProcessingOutputRasterLayer( name(), description() );
3568 }
3569 
3571 {
3572  if ( originalProvider() )
3573  {
3575  }
3576  else if ( QgsProcessingProvider *p = provider() )
3577  {
3578  return p->defaultRasterFileExtension();
3579  }
3580  else
3581  {
3582  QgsSettings settings;
3583  return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
3584  }
3585 }
3586 
3588 {
3589  if ( originalProvider() )
3590  {
3592  }
3593  else if ( QgsProcessingProvider *p = provider() )
3594  {
3595  return p->supportedOutputRasterLayerExtensions();
3596  }
3597  else
3598  {
3600  }
3601 }
3602 
3603 QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3604 {
3605  return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
3606 }
3607 
3608 
3609 QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional, bool createByDefault )
3610  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3611  , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3612 {
3613 
3614 }
3615 
3617 {
3618  return new QgsProcessingParameterFileDestination( *this );
3619 }
3620 
3622 {
3623  QVariant var = input;
3624  if ( !var.isValid() )
3625  return mFlags & FlagOptional;
3626 
3627  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3628  {
3630  var = fromVar.sink;
3631  }
3632 
3633  if ( var.canConvert<QgsProperty>() )
3634  {
3635  QgsProperty p = var.value< QgsProperty >();
3637  {
3638  var = p.staticValue();
3639  }
3640  else
3641  {
3642  return true;
3643  }
3644  }
3645 
3646  if ( var.type() != QVariant::String )
3647  return false;
3648 
3649  if ( var.toString().isEmpty() )
3650  return mFlags & FlagOptional;
3651 
3652  // possible enhancement - check that value is compatible with file filter?
3653 
3654  return true;
3655 }
3656 
3658 {
3659  if ( !value.isValid() )
3660  return QStringLiteral( "None" );
3661 
3662  if ( value.canConvert<QgsProperty>() )
3663  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3664 
3665  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3666  {
3668  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3669  {
3670  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3671  }
3672  else
3673  {
3674  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3675  }
3676  }
3677 
3678  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3679 }
3680 
3682 {
3683  if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
3684  {
3685  return new QgsProcessingOutputHtml( name(), description() );
3686  }
3687  else
3688  {
3689  return new QgsProcessingOutputFile( name(), description() );
3690  }
3691 }
3692 
3694 {
3695  if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
3696  return QStringLiteral( "file" );
3697 
3698  // get first extension from filter
3699  QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
3700  QRegularExpressionMatch match = rx.match( mFileFilter );
3701  if ( !match.hasMatch() )
3702  return QStringLiteral( "file" );
3703 
3704  return match.captured( 1 );
3705 }
3706 
3708 {
3709  return mFileFilter;
3710 }
3711 
3713 {
3714  mFileFilter = fileFilter;
3715 }
3716 
3718 {
3720  map.insert( QStringLiteral( "file_filter" ), mFileFilter );
3721  return map;
3722 }
3723 
3725 {
3727  mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
3728  return true;
3729 
3730 }
3731 
3732 QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3733 {
3734  return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
3735 }
3736 
3738  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3739 {}
3740 
3742 {
3743  return new QgsProcessingParameterFolderDestination( *this );
3744 }
3745 
3747 {
3748  QVariant var = input;
3749  if ( !var.isValid() )
3750  return mFlags & FlagOptional;
3751 
3752  if ( var.canConvert<QgsProperty>() )
3753  {
3754  QgsProperty p = var.value< QgsProperty >();
3756  {
3757  var = p.staticValue();
3758  }
3759  else
3760  {
3761  return true;
3762  }
3763  }
3764 
3765  if ( var.type() != QVariant::String )
3766  return false;
3767 
3768  if ( var.toString().isEmpty() )
3769  return mFlags & FlagOptional;
3770 
3771  return true;
3772 }
3773 
3775 {
3776  return new QgsProcessingOutputFolder( name(), description() );
3777 }
3778 
3780 {
3781  return QString();
3782 }
3783 
3784 QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3785 {
3786  return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
3787 }
3788 
3789 QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, bool createByDefault )
3790  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3791  , mCreateByDefault( createByDefault )
3792 {
3793 
3794 }
3795 
3797 {
3799  map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
3800  map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
3801  return map;
3802 }
3803 
3805 {
3807  mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
3808  mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
3809  return true;
3810 }
3811 
3813 {
3814  if ( defaultFileExtension().isEmpty() )
3815  {
3817  }
3818  else
3819  {
3821  }
3822 }
3823 
3825 {
3826  return mCreateByDefault;
3827 }
3828 
3830 {
3831  mCreateByDefault = createByDefault;
3832 }
3833 
3835  : QgsProcessingDestinationParameter( name, description, defaultValue, optional, createByDefault )
3836  , mDataType( type )
3837 {
3838 
3839 }
3840 
3842 {
3843  return new QgsProcessingParameterVectorDestination( *this );
3844 }
3845 
3847 {
3848  QVariant var = input;
3849  if ( !var.isValid() )
3850  return mFlags & FlagOptional;
3851 
3852  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3853  {
3855  var = fromVar.sink;
3856  }
3857 
3858  if ( var.canConvert<QgsProperty>() )
3859  {
3860  QgsProperty p = var.value< QgsProperty >();
3862  {
3863  var = p.staticValue();
3864  }
3865  else
3866  {
3867  return true;
3868  }
3869  }
3870 
3871  if ( var.type() != QVariant::String )
3872  return false;
3873 
3874  if ( var.toString().isEmpty() )
3875  return mFlags & FlagOptional;
3876 
3877  return true;
3878 }
3879 
3881 {
3882  if ( !value.isValid() )
3883  return QStringLiteral( "None" );
3884 
3885  if ( value.canConvert<QgsProperty>() )
3886  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3887 
3888  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3889  {
3891  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3892  {
3893  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3894  }
3895  else
3896  {
3897  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3898  }
3899  }
3900 
3901  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
3902 }
3903 
3905 {
3906  QString code = QStringLiteral( "##%1=" ).arg( mName );
3907  if ( mFlags & FlagOptional )
3908  code += QStringLiteral( "optional " );
3909  code += QStringLiteral( "vectorDestination " );
3910 
3911  switch ( mDataType )
3912  {
3914  code += QStringLiteral( "point " );
3915  break;
3916 
3918  code += QStringLiteral( "line " );
3919  break;
3920 
3922  code += QStringLiteral( "polygon " );
3923  break;
3924 
3925  default:
3926  break;
3927  }
3928 
3929  code += mDefault.toString();
3930  return code.trimmed();
3931 }
3932 
3934 {
3935  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
3936 }
3937 
3939 {
3940  if ( originalProvider() )
3941  {
3943  }
3944  else if ( QgsProcessingProvider *p = provider() )
3945  {
3946  return p->defaultVectorFileExtension( hasGeometry() );
3947  }
3948  else
3949  {
3950  QgsSettings settings;
3951  if ( hasGeometry() )
3952  {
3953  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3954  }
3955  else
3956  {
3957  return QStringLiteral( "dbf" );
3958  }
3959  }
3960 }
3961 
3963 {
3964  if ( originalProvider() )
3965  {
3967  }
3968  else if ( QgsProcessingProvider *p = provider() )
3969  {
3970  return p->supportedOutputVectorLayerExtensions();
3971  }
3972  else
3973  {
3975  }
3976 }
3977 
3979 {
3980  return mDataType;
3981 }
3982 
3984 {
3985  switch ( mDataType )
3986  {
3993  return true;
3994 
3997  return false;
3998  }
3999  return true;
4000 }
4001 
4003 {
4004  mDataType = type;
4005 }
4006 
4008 {
4010  map.insert( QStringLiteral( "data_type" ), mDataType );
4011  return map;
4012 }
4013 
4015 {
4017  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
4018  return true;
4019 }
4020 
4021 QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4022 {
4024  QString def = definition;
4025  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
4026  {
4028  def = def.mid( 6 );
4029  }
4030  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
4031  {
4033  def = def.mid( 5 );
4034  }
4035  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
4036  {
4038  def = def.mid( 8 );
4039  }
4040 
4041  return new QgsProcessingParameterVectorDestination( name, description, type, definition, isOptional );
4042 }
4043 
4044 QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
4045  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
4046  , mParentLayerParameterName( parentLayerParameterName )
4047 {
4048 
4049 }
4050 
4052 {
4053  return new QgsProcessingParameterBand( *this );
4054 }
4055 
4057 {
4058  if ( !input.isValid() )
4059  return mFlags & FlagOptional;
4060 
4061  if ( input.canConvert<QgsProperty>() )
4062  {
4063  return true;
4064  }
4065 
4066  bool ok = false;
4067  double res = input.toInt( &ok );
4068  Q_UNUSED( res );
4069  if ( !ok )
4070  return mFlags & FlagOptional;
4071 
4072  return true;
4073 }
4074 
4076 {
4077  if ( !value.isValid() )
4078  return QStringLiteral( "None" );
4079 
4080  if ( value.canConvert<QgsProperty>() )
4081  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
4082 
4083  return value.toString();
4084 }
4085 
4087 {
4088  QString code = QStringLiteral( "##%1=" ).arg( mName );
4089  if ( mFlags & FlagOptional )
4090  code += QStringLiteral( "optional " );
4091  code += QStringLiteral( "band " );
4092 
4093  code += mParentLayerParameterName + ' ';
4094 
4095  code += mDefault.toString();
4096  return code.trimmed();
4097 }
4098 
4100 {
4101  QStringList depends;
4102  if ( !mParentLayerParameterName.isEmpty() )
4103  depends << mParentLayerParameterName;
4104  return depends;
4105 }
4106 
4108 {
4109  return mParentLayerParameterName;
4110 }
4111 
4113 {
4114  mParentLayerParameterName = parentLayerParameterName;
4115 }
4116 
4118 {
4120  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
4121  return map;
4122 }
4123 
4124 bool QgsProcessingParameterBand::fromVariantMap( const QVariantMap &map )
4125 {
4127  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
4128  return true;
4129 }
4130 
4131 QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
4132 {
4133  QString parent;
4134  QString def = definition;
4135 
4136  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
4137  QRegularExpressionMatch m = re.match( def );
4138  if ( m.hasMatch() )
4139  {
4140  parent = m.captured( 1 ).trimmed();
4141  def = m.captured( 2 );
4142  }
4143  else
4144  {
4145  parent = def;
4146  def.clear();
4147  }
4148 
4149  return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional );
4150 }
4151 
4152 //
4153 // QgsProcessingParameterDistance
4154 //
4155 
4156 QgsProcessingParameterDistance::QgsProcessingParameterDistance( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentParameterName, bool optional, double minValue, double maxValue )
4157  : QgsProcessingParameterNumber( name, description, Double, defaultValue, optional, minValue, maxValue )
4158  , mParentParameterName( parentParameterName )
4159 {
4160 
4161 }
4162 
4164 {
4165  return new QgsProcessingParameterDistance( *this );
4166 }
4167 
4169 {
4170  return typeName();
4171 }
4172 
4174 {
4175  QStringList depends;
4176  if ( !mParentParameterName.isEmpty() )
4177  depends << mParentParameterName;
4178  return depends;
4179 }
4180 
4182 {
4183  return mParentParameterName;
4184 }
4185 
4187 {
4188  mParentParameterName = parentParameterName;
4189 }
4190 
4192 {
4193  QVariantMap map = QgsProcessingParameterNumber::toVariantMap();
4194  map.insert( QStringLiteral( "parent" ), mParentParameterName );
4195  return map;
4196 }
4197 
4199 {
4201  mParentParameterName = map.value( QStringLiteral( "parent" ) ).toString();
4202  return true;
4203 }
4204 
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterDefinition.
QgsProperty sink
Sink/layer definition.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsCoordinateReferenceSystem parameterAsCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a coordinate reference system.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
A boolean parameter for processing algorithms.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true)
Interprets a string as a map layer within the supplied context.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setDataTypes(const QList< int > &types)
Sets the geometry types for sources acceptable by the parameter.
static QString typeName()
Returns the type name for the parameter class.
An input file or folder parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
virtual QString asScriptCode() const
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
Base class for all map layer types.
Definition: qgsmaplayer.h:61
static QString descriptionFromName(const QString &name)
Creates an autogenerated parameter description from a parameter name.
static QString parameterAsString(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static string value.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
static int parameterAsEnum(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a enum value.
static QString typeName()
Returns the type name for the parameter class.
Base class for providing feedback from a processing algorithm.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
QgsProcessingParameterVectorDestination(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterVectorDestination.
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...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString type() const override
Unique parameter type name.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
bool createFromString(const QString &definition)
Set up this CRS from a string definition.
QString type() const override
Unique parameter type name.
A vector layer or feature source field parameter for processing algorithms.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
static QgsProcessingParameterExtent * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
DataType dataType() const
Returns the acceptable data type for the field.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported for this parameter. ...
void setHasFixedNumberRows(bool hasFixedNumberRows)
Sets whether the table has a fixed number of rows.
QgsProcessingParameterMultipleLayers(const QString &name, const QString &description=QString(), QgsProcessing::SourceType layerType=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMultipleLayers.
QgsProcessingParameterExpression(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterExpression.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterRange(const QString &name, const QString &description=QString(), QgsProcessingParameterNumber::Type type=QgsProcessingParameterNumber::Integer, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRange.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsProcessingParameterFeatureSource(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFeatureSource.
void setDataType(QgsProcessingParameterNumber::Type dataType)
Sets the acceptable data type for the range.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set...
static QString typeName()
Returns the type name for the parameter class.
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.
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const SIP_THROW(QgsCsException)
Transform the point from the source CRS to the destination CRS.
static QString typeName()
Returns the type name for the parameter class.
QString type() const override
Unique parameter type name.
double y
Definition: qgspointxy.h:48
A map layer parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QStringList headers() const
Returns a list of column headers (if set).
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
A class to represent a 2D point.
Definition: qgspointxy.h:43
A HTML file output for processing algorithms.
QgsProcessingProvider * provider() const
Returns a pointer to the provider for the algorithm which owns this parameter.
An expression parameter for processing algorithms.
A QgsPointXY with associated coordinate reference system.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterRange * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterString(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool multiLine=false, bool optional=false)
Constructor for QgsProcessingParameterString.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool createByDefault() const
Returns true if the destination should be created by default.
An interface for objects which accept features via addFeature(s) methods.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingProvider * originalProvider() const
Original (source) provider which this parameter has been derived from.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
QgsProcessingAlgorithm * mAlgorithm
Pointer to algorithm which owns this parameter.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
static QString typeName()
Returns the type name for the parameter class.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QString typeName()
Returns the type name for the parameter class.
QVariantMap mMetadata
Freeform metadata for parameter. Mostly used by widget wrappers to customize their appearance and beh...
double minimum() const
Returns the minimum value acceptable by the 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 QgsProcessingParameterCrs * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsRasterLayer * parameterAsRasterLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a raster layer.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
static QgsMapLayer * parameterAsLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a map layer.
static QgsProcessingParameterMultipleLayers * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Abstract base class for processing providers.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
bool allowMultiple() const
Returns true if the parameter allows multiple selected values.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingParameterMapLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMapLayer.
A raster band parameter for Processing algorithms.
QString type() const override
Unique parameter type name.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
virtual QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
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...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map...
QgsProcessingParameterEnum(const QString &name, const QString &description=QString(), const QStringList &options=QStringList(), bool allowMultiple=false, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterEnum.
QgsProcessingParameterBoolean(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterBoolean.
QString type() const override
Unique parameter type name.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
static QgsProcessingParameterMapLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString convertToCompatibleFormat(const QgsVectorLayer *layer, bool selectedFeaturesOnly, const QString &baseName, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingContext &context, QgsProcessingFeedback *feedback)
Converts a source vector layer to a file path to a vector layer of compatible format.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
static QString typeName()
Returns the type name for the parameter class.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setMaximum(double maximum)
Sets the maximum value acceptable by the parameter.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments...
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.
static QgsPointXY parameterAsPoint(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem())
Evaluates the parameter with matching definition to a point.
A vector layer output for processing algorithms.
static QgsProcessingParameterBand * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
static QString parameterAsFile(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file/folder name.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
A feature sink output for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString fileFilter() const
Returns the file filter string for file destinations compatible with this parameter.
static QList< QgsMapLayer * > parameterAsLayerList(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a list of map layers.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Files (i.e. non map layer sources, such as text files)
Definition: qgsprocessing.h:52
bool selectedFeaturesOnly
True if only selected features in the source should be used by algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
virtual QgsRectangle extent() const
Returns the extent of the layer.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
int minimumNumberInputs() const
Returns the minimum number of layers required for the parameter.
void setAllowMultiple(bool allowMultiple)
Sets whether multiple field selections are permitted.
static QgsProcessingParameterBoolean * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterBand(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterBand.
static QString typeName()
Returns the type name for the parameter class.
QgsProcessing::SourceType layerType() const
Returns the layer type for layers acceptable by the parameter.
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.
A numeric range parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterField * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QgsProcessingParameterCrs(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterCrs.
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
QList< int > dataTypes() const
Returns the geometry types for sources acceptable by the parameter.
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.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set...
static QString typeName()
Returns the type name for the parameter class.
QgsProcessingParameterFolderDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFolderDestination.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Can be inherited by parameters which require limits to their acceptable data types.
A raster layer parameter for processing algorithms.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessingParameterFeatureSink(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFeatureSink.
QgsProcessingParameterFile(const QString &name, const QString &description=QString(), Behavior behavior=File, const QString &extension=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFile.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
virtual QString toolTip() const
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
static QgsProcessingParameterNumber * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QgsCoordinateReferenceSystem parameterAsPointCrs(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Returns the coordinate reference system associated with an point parameter value. ...
QString valueAsString(const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a string.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
QgsProperty source
Source definition.
virtual QgsProcessingParameterDefinition * create(const QString &name) const =0
Creates a new parameter of this type.
Type propertyType() const
Returns the property type.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap())
Creates a feature sink ready for adding features.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
An enum based parameter for processing algorithms, allowing for selection from predefined values...
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
void setMinimum(double minimum)
Sets the minimum value acceptable by the parameter.
Flags flags() const
Returns any flags associated with the parameter.
QgsProcessing::SourceType dataType() const
Returns the layer type for this created vector layer.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterFeatureSink * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QVariant defaultValue() const
Returns the default value for the parameter.
bool hasGeometry() const
Returns true if the created layer is likely to include geometries.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterDistance * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
QgsGeometry densifyByCount(int extraNodesPerSegment) const
Returns a copy of the geometry which has been densified by adding the specified number of extra nodes...
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:237
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
A double numeric parameter for distance values.
A file output for processing algorithms.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:91
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).
QgsProcessingDestinationParameter(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingDestinationParameter.
QString parentParameterName() const
Returns the name of the parent parameter, or an empty string if this is not set.
bool valueAsBool(const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an boolean.
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
static QList< double > parameterAsRange(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a range of values.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterFolderDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
virtual QString type() const =0
Unique parameter type name.
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
Reads and writes project states.
Definition: qgsproject.h:85
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Vector polygon layers.
Definition: qgsprocessing.h:50
A vector layer (with or without geometry) parameter for processing algorithms.
static QgsProcessingParameterExpression * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
bool multiLine() const
Returns true if the parameter allows multiline strings.
virtual QString defaultFileExtension() const =0
Returns the default file extension for destination file paths associated with this parameter...
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
void setParentLayerParameterName(const QString &parentLayerParameterName)
Sets the name of the parent layer parameter.
void setParentParameterName(const QString &parentParameterName)
Sets the name of the parent layer parameter.
QStringList options() const
Returns the list of acceptable options for the parameter.
A QgsRectangle with associated coordinate reference system.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
QgsProcessingParameterNumber(const QString &name, const QString &description=QString(), Type type=Integer, const QVariant &defaultValue=QVariant(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterNumber.
A coordinate reference system parameter for processing algorithms.
static QgsProcessingFeatureSource * variantToSource(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a new feature source.
A store for object properties.
Definition: qgsproperty.h:229
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
A rectangular map extent parameter for processing algorithms.
void setMinimumNumberInputs(int minimum)
Sets the minimum number of layers required for the parameter.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
void setLayerType(QgsProcessing::SourceType type)
Sets the layer type for layers acceptable by the parameter.
Details for layers to load into projects.
void setAllowMultiple(bool allowMultiple)
Sets whether the parameter allows multiple selected values.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A numeric parameter for processing algorithms.
bool loadVariant(const QVariantMap &map)
Loads this output layer definition from a QVariantMap, wrapped in a QVariant.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
static QgsProcessingParameterVectorDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsExpressionContext & expressionContext()
Returns the expression context.
double x
Definition: qgspointxy.h:47
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDistance(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentParameterName=QString(), bool optional=false, double minValue=std::numeric_limits< double >::lowest()+1, double maxValue=std::numeric_limits< double >::max())
Constructor for QgsProcessingParameterDistance.
void setOptions(const QStringList &options)
Sets the list of acceptable options for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QString name() const
Returns the name of the parameter.
QList< int > mDataTypes
List of acceptable data types for the parameter.
Encapsulates settings relating to a feature source input to a processing algorithm.
void setMultiLine(bool multiLine)
Sets whether the parameter allows multiline strings.
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
void setDataType(Type type)
Sets the acceptable data type for the parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:176
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
bool supportsNonFileBasedOutput() const
Returns true if the destination parameter supports non filed-based outputs, such as memory layers or ...
Behavior behavior() const
Returns the parameter behavior (e.g.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QString generateTempFilename(const QString &basename)
Returns a temporary filename for a given file, putting it into a temporary folder (creating that fold...
QString type() const override
Unique parameter type name.
QVariant toVariant() const
Saves this output layer definition to a QVariantMap, wrapped in a QVariant.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model...
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:161
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
static QgsProcessingParameterFile * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition, Behavior behavior=File)
Creates a new parameter using the definition from a script code.
static QgsProcessingParameterMatrix * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
Base class for the definition of processing outputs.
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QgsProcessing::SourceType dataType() const
Returns the layer type for sinks associated with the parameter.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
QgsProcessingParameterLimitedDataTypes(const QList< int > &types=QList< int >())
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data types...
void setNumberRows(int rows)
Sets the fixed number of rows in the table.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set...
A point parameter for processing algorithms.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
void setDataType(DataType type)
Sets the acceptable data type for the field.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
Vector point layers.
Definition: qgsprocessing.h:48
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
static QgsProcessingParameterString * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QString source() const
Returns the source for the layer.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
An input feature source (such as vector layers) parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
int valueAsInt(const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an integer.
QString destinationName
Name to use for sink if it&#39;s to be loaded into a destination project.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterVectorLayer * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
Any map layer type (raster or vector)
Definition: qgsprocessing.h:46
Makes metadata of processing parameters available.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsFeatureSink * parameterAsSink(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context, QString &destinationIdentifier)
Evaluates the parameter with matching definition to a feature sink.
QString toolTip() const override
Returns a formatted tooltip for use with the parameter, which gives helpful information like paramete...
QgsProject * destinationProject
Destination project.
This class represents a coordinate reference system (CRS).
int numberRows() const
Returns the fixed number of rows in the table.
Base class for the definition of processing parameters.
Vector line layers.
Definition: qgsprocessing.h:49
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:429
virtual bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const
Checks whether the specified input value is acceptable for the parameter.
static QgsProcessingParameterDefinition * parameterFromScriptCode(const QString &code)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied script code st...
QVariant staticValue() const
Returns the current static value for the property.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:53
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
Class for doing transforms between two map coordinate systems.
static double parameterAsDouble(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static double value.
QVariant mDefault
Default value for parameter.
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.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterRasterDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:166
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.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
static QgsProcessingFeatureSource * parameterAsSource(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a feature source.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:171
static QString typeName()
Returns the type name for the parameter class.
static bool parameterAsBool(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context)
Evaluates the parameter with matching definition to a static boolean value.
virtual QString generateTemporaryDestination() const
Generates a temporary destination value for this parameter.
A folder output for processing algorithms.
static QString typeName()
Returns the type name for the parameter class.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static QgsProcessingParameterPoint * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterPoint(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterPoint.
A table (matrix) parameter for processing algorithms.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Type dataType() const
Returns the acceptable data type for the parameter.
QString type() const override
Unique parameter type name.
static QgsProcessingParameterEnum * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
QString mDescription
Parameter description.
void setFileFilter(const QString &filter)
Sets the file filter string for file destinations compatible with this parameter. ...
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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...
bool hasGeometry() const
Returns true if sink is likely to include geometries.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterExtent(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterExtent.
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
QgsProcessingParameterField(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), const QString &parentLayerParameterName=QString(), DataType type=Any, bool allowMultiple=false, bool optional=false)
Constructor for QgsProcessingParameterField.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString asScriptCode() const override
Returns the parameter definition encoded in a string which can be used within a Python processing scr...
static QgsProcessingParameterFeatureSource * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
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.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Invalid (not set) property.
Definition: qgsproperty.h:237
Contains information about the context in which a processing algorithm is executed.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the created vector layer.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
A string parameter for processing algorithms.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
QgsProcessingParameterVectorLayer(const QString &name, const QString &description=QString(), const QList< int > &types=QList< int >(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterVectorLayer.
void setDataType(QgsProcessing::SourceType type)
Sets the layer type for the sinks associated with the parameter.
Any vector layer with geometry.
Definition: qgsprocessing.h:47
QgsProcessingParameterNumber::Type dataType() const
Returns the acceptable data type for the range.
QgsProcessingParameterMatrix(const QString &name, const QString &description=QString(), int numberRows=3, bool hasFixedNumberRows=false, const QStringList &headers=QStringList(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterMatrix.
QString type() const override
Unique parameter type name.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
bool allowMultiple() const
Returns whether multiple field selections are permitted.
QString authid() const
Returns the authority identifier for the CRS.
QString defaultFileExtension() const override
Returns the default file extension for destination file paths associated with this parameter...
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this parameter.
QString generateTemporaryDestination() const override
Generates a temporary destination value for this parameter.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, TransformDirection direction=ForwardTransform, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
QgsProcessingParameterRasterLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterLayer.
QgsProcessingParameterFileDestination(const QString &name, const QString &description=QString(), const QString &fileFilter=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterFileDestination.
double maximum() const
Returns the maximum value acceptable by the parameter.
QVariantMap createOptions
Map of optional sink/layer creation options, which are passed to the underlying provider when creatin...
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
static QgsProcessingParameterFileDestination * fromScriptCode(const QString &name, const QString &description, bool isOptional, const QString &definition)
Creates a new parameter using the definition from a script code.
QgsProcessingParameterRasterDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, bool createByDefault=true)
Constructor for QgsProcessingParameterRasterDestination.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QgsProcessingOutputDefinition * toOutputDefinition() const override
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination parame...
static QString typeName()
Returns the type name for the parameter class.
A raster layer output for processing algorithms.
static QgsProcessingRegistry * processingRegistry()
Returns the application&#39;s processing registry, used for managing processing providers, algorithms, and various parameters and outputs.
static QString parameterAsFileOutput(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a file based output destination.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
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.
void setHeaders(const QStringList &headers)
Sets the list of column headers.