QGIS API Documentation  3.0.2-Girona (307d082)
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 <functional>
29 
30 bool QgsProcessingParameters::isDynamic( const QVariantMap &parameters, const QString &name )
31 {
32  QVariant val = parameters.value( name );
33  if ( val.canConvert<QgsProperty>() )
34  return val.value< QgsProperty >().propertyType() != QgsProperty::StaticProperty;
35  else
36  return false;
37 }
38 
39 QString QgsProcessingParameters::parameterAsString( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
40 {
41  if ( !definition )
42  return QString();
43 
44  QVariant val = parameters.value( definition->name() );
45  if ( val.canConvert<QgsProperty>() )
46  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
47 
48  if ( !val.isValid() )
49  {
50  // fall back to default
51  val = definition->defaultValue();
52  }
53 
54  return val.toString();
55 }
56 
57 QString QgsProcessingParameters::parameterAsExpression( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
58 {
59  if ( !definition )
60  return QString();
61 
62  QVariant val = parameters.value( definition->name() );
63  if ( val.canConvert<QgsProperty>() )
64  return val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
65 
66  if ( val.isValid() && !val.toString().isEmpty() )
67  {
68  QgsExpression e( val.toString() );
69  if ( e.isValid() )
70  return val.toString();
71  }
72 
73  // fall back to default
74  return definition->defaultValue().toString();
75 }
76 
77 double QgsProcessingParameters::parameterAsDouble( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
78 {
79  if ( !definition )
80  return 0;
81 
82  QVariant val = parameters.value( definition->name() );
83  if ( val.canConvert<QgsProperty>() )
84  return val.value< QgsProperty >().valueAsDouble( context.expressionContext(), definition->defaultValue().toDouble() );
85 
86  bool ok = false;
87  double res = val.toDouble( &ok );
88  if ( ok )
89  return res;
90 
91  // fall back to default
92  val = definition->defaultValue();
93  return val.toDouble();
94 }
95 
96 int QgsProcessingParameters::parameterAsInt( 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 >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
104 
105  bool ok = false;
106  double dbl = val.toDouble( &ok );
107  if ( !ok )
108  {
109  // fall back to default
110  val = definition->defaultValue();
111  dbl = val.toDouble( &ok );
112  }
113 
114  //String representations of doubles in QVariant will not convert to int
115  //work around this by first converting to double, and then checking whether the double is convertible to int
116  if ( ok )
117  {
118  double round = std::round( dbl );
119  if ( round > INT_MAX || round < -INT_MAX )
120  {
121  //double too large to fit in int
122  return 0;
123  }
124  return std::round( dbl );
125  }
126 
127  return val.toInt();
128 }
129 
130 int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
131 {
132  if ( !definition )
133  return 0;
134 
135  int val = parameterAsInt( definition, parameters, context );
136  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
137  if ( enumDef && val >= enumDef->options().size() )
138  {
139  return enumDef->defaultValue().toInt();
140  }
141  return val;
142 }
143 
144 QList<int> QgsProcessingParameters::parameterAsEnums( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
145 {
146  if ( !definition )
147  return QList<int>();
148 
149  QVariantList resultList;
150  QVariant val = parameters.value( definition->name() );
151  if ( val.canConvert<QgsProperty>() )
152  resultList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
153  else if ( val.type() == QVariant::List )
154  {
155  Q_FOREACH ( const QVariant &var, val.toList() )
156  resultList << var;
157  }
158  else if ( val.type() == QVariant::String )
159  {
160  Q_FOREACH ( const QString &var, val.toString().split( ',' ) )
161  resultList << var;
162  }
163  else
164  resultList << val;
165 
166  if ( resultList.isEmpty() )
167  return QList< int >();
168 
169  if ( ( !val.isValid() || !resultList.at( 0 ).isValid() ) && definition )
170  {
171  resultList.clear();
172  // check default
173  if ( definition->defaultValue().type() == QVariant::List )
174  {
175  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
176  resultList << var;
177  }
178  else if ( definition->defaultValue().type() == QVariant::String )
179  {
180  Q_FOREACH ( const QString &var, definition->defaultValue().toString().split( ',' ) )
181  resultList << var;
182  }
183  else
184  resultList << definition->defaultValue();
185  }
186 
187  QList< int > result;
188  const QgsProcessingParameterEnum *enumDef = dynamic_cast< const QgsProcessingParameterEnum *>( definition );
189  Q_FOREACH ( const QVariant &var, resultList )
190  {
191  int resInt = var.toInt();
192  if ( !enumDef || resInt < enumDef->options().size() )
193  {
194  result << resInt;
195  }
196  }
197  return result;
198 }
199 
200 bool QgsProcessingParameters::parameterAsBool( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
201 {
202  if ( !definition )
203  return false;
204 
205  QVariant def = definition->defaultValue();
206 
207  QVariant val = parameters.value( definition->name() );
208  if ( val.canConvert<QgsProperty>() )
209  return val.value< QgsProperty >().valueAsBool( context.expressionContext(), def.toBool() );
210  else if ( val.isValid() )
211  return val.toBool();
212  else
213  return def.toBool();
214 }
215 
216 QgsFeatureSink *QgsProcessingParameters::parameterAsSink( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsFields &fields,
217  QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs,
218  QgsProcessingContext &context, QString &destinationIdentifier )
219 {
220  QVariant val;
221  if ( definition )
222  {
223  val = parameters.value( definition->name() );
224  }
225 
226  QgsProject *destinationProject = nullptr;
227  QString destName;
228  QVariantMap createOptions;
229  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
230  {
231  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
233  destinationProject = fromVar.destinationProject;
234  createOptions = fromVar.createOptions;
235  val = fromVar.sink;
236  destName = fromVar.destinationName;
237  }
238 
239  QString dest;
240  if ( val.canConvert<QgsProperty>() )
241  {
242  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
243  }
244  else if ( !val.isValid() || val.toString().isEmpty() )
245  {
246  if ( definition && definition->flags() & QgsProcessingParameterDefinition::FlagOptional && !definition->defaultValue().isValid() )
247  {
248  // unset, optional sink, no default => no sink
249  return nullptr;
250  }
251  // fall back to default
252  dest = definition->defaultValue().toString();
253  }
254  else
255  {
256  dest = val.toString();
257  }
258 
259  if ( dest.isEmpty() )
260  return nullptr;
261 
262  std::unique_ptr< QgsFeatureSink > sink( QgsProcessingUtils::createFeatureSink( dest, context, fields, geometryType, crs, createOptions ) );
263  destinationIdentifier = dest;
264 
265  if ( destinationProject )
266  {
267  if ( destName.isEmpty() && definition )
268  {
269  destName = definition->description();
270  }
271  QString outputName;
272  if ( definition )
273  outputName = definition->name();
274  context.addLayerToLoadOnCompletion( destinationIdentifier, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName ) );
275  }
276 
277  return sink.release();
278 }
279 
281 {
282  if ( !definition )
283  return nullptr;
284 
285  QVariant val = parameters.value( definition->name() );
286 
287  return QgsProcessingUtils::variantToSource( val, context, definition->defaultValue() );
288 }
289 
290 QString QgsProcessingParameters::parameterAsCompatibleSourceLayerPath( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat, QgsProcessingFeedback *feedback )
291 {
292  if ( !definition )
293  return QString();
294 
295  QVariant val = parameters.value( definition->name() );
296 
297  bool selectedFeaturesOnly = false;
298  if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() )
299  {
300  // input is a QgsProcessingFeatureSourceDefinition - get extra properties from it
302  selectedFeaturesOnly = fromVar.selectedFeaturesOnly;
303  val = fromVar.source;
304  }
305 
306  QString layerRef;
307  if ( val.canConvert<QgsProperty>() )
308  {
309  layerRef = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
310  }
311  else if ( !val.isValid() || val.toString().isEmpty() )
312  {
313  // fall back to default
314  layerRef = definition->defaultValue().toString();
315  }
316  else
317  {
318  layerRef = val.toString();
319  }
320 
321  if ( layerRef.isEmpty() )
322  return QString();
323 
324  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( layerRef, context ) );
325  if ( !vl )
326  return QString();
327 
328  return QgsProcessingUtils::convertToCompatibleFormat( vl, selectedFeaturesOnly, definition->name(),
329  compatibleFormats, preferredFormat, context, feedback );
330 }
331 
332 
334 {
335  if ( !definition )
336  return nullptr;
337 
338  QVariant val = parameters.value( definition->name() );
339  if ( val.canConvert<QgsProperty>() )
340  {
341  val = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
342  }
343 
344  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
345  {
346  return layer;
347  }
348 
349  if ( !val.isValid() || val.toString().isEmpty() )
350  {
351  // fall back to default
352  val = definition->defaultValue();
353  }
354 
355  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
356  {
357  return layer;
358  }
359 
360  QString layerRef = val.toString();
361  if ( layerRef.isEmpty() )
362  layerRef = definition->defaultValue().toString();
363 
364  if ( layerRef.isEmpty() )
365  return nullptr;
366 
367  return QgsProcessingUtils::mapLayerFromString( layerRef, context );
368 }
369 
371 {
372  return qobject_cast< QgsRasterLayer *>( parameterAsLayer( definition, parameters, context ) );
373 }
374 
375 QString QgsProcessingParameters::parameterAsOutputLayer( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
376 {
377  QVariant val;
378  if ( definition )
379  {
380  val = parameters.value( definition->name() );
381  }
382 
383  QgsProject *destinationProject = nullptr;
384  QVariantMap createOptions;
385  QString destName;
386  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
387  {
388  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
390  destinationProject = fromVar.destinationProject;
391  createOptions = fromVar.createOptions;
392  val = fromVar.sink;
393  destName = fromVar.destinationName;
394  }
395 
396  QString dest;
397  if ( val.canConvert<QgsProperty>() )
398  {
399  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
400  }
401  else if ( definition && ( !val.isValid() || val.toString().isEmpty() ) )
402  {
403  // fall back to default
404  dest = definition->defaultValue().toString();
405  }
406  else
407  {
408  dest = val.toString();
409  }
410 
411  if ( destinationProject )
412  {
413  QString outputName;
414  if ( destName.isEmpty() && definition )
415  {
416  destName = definition->description();
417  }
418  if ( definition )
419  outputName = definition->name();
420  context.addLayerToLoadOnCompletion( dest, QgsProcessingContext::LayerDetails( destName, destinationProject, outputName ) );
421  }
422 
423  return dest;
424 }
425 
426 QString QgsProcessingParameters::parameterAsFileOutput( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
427 {
428  QVariant val;
429  if ( definition )
430  {
431  val = parameters.value( definition->name() );
432  }
433 
434  if ( val.canConvert<QgsProcessingOutputLayerDefinition>() )
435  {
436  // input is a QgsProcessingOutputLayerDefinition - get extra properties from it
438  val = fromVar.sink;
439  }
440 
441  QString dest;
442  if ( val.canConvert<QgsProperty>() )
443  {
444  dest = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
445  }
446  else if ( !val.isValid() || val.toString().isEmpty() )
447  {
448  // fall back to default
449  dest = definition->defaultValue().toString();
450  }
451  else
452  {
453  dest = val.toString();
454  }
455 
456  return dest;
457 }
458 
460 {
461  return qobject_cast< QgsVectorLayer *>( parameterAsLayer( definition, parameters, context ) );
462 }
463 
465 {
466  if ( !definition )
468 
469  QVariant val = parameters.value( definition->name() );
470 
471  QString crsText = parameterAsString( definition, parameters, context );
472  if ( crsText.isEmpty() )
473  crsText = definition->defaultValue().toString();
474 
475  if ( crsText.isEmpty() )
477 
478  // maybe special string
479  if ( context.project() && crsText.compare( QStringLiteral( "ProjectCrs" ), Qt::CaseInsensitive ) == 0 )
480  return context.project()->crs();
481 
482  // maybe a map layer reference
483  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
484  return layer->crs();
485  else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
486  return layer->crs();
487 
488  // else CRS from string
490  crs.createFromString( crsText );
491  return crs;
492 }
493 
495  const QgsCoordinateReferenceSystem &crs )
496 {
497  if ( !definition )
498  return QgsRectangle();
499 
500  QVariant val = parameters.value( definition->name() );
501 
502  if ( val.canConvert< QgsRectangle >() )
503  {
504  return val.value<QgsRectangle>();
505  }
506  if ( val.canConvert< QgsReferencedRectangle >() )
507  {
509  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
510  {
511  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
512  try
513  {
514  return ct.transformBoundingBox( rr );
515  }
516  catch ( QgsCsException & )
517  {
518  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
519  }
520  }
521  return rr;
522  }
523 
524  // maybe parameter is a direct layer value?
525  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
526 
527  QString rectText;
528  if ( val.canConvert<QgsProperty>() )
529  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
530  else
531  rectText = val.toString();
532 
533  if ( rectText.isEmpty() && !layer )
534  return QgsRectangle();
535 
536  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
537  QRegularExpressionMatch match = rx.match( rectText );
538  if ( match.hasMatch() )
539  {
540  bool xMinOk = false;
541  double xMin = match.captured( 1 ).toDouble( &xMinOk );
542  bool xMaxOk = false;
543  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
544  bool yMinOk = false;
545  double yMin = match.captured( 3 ).toDouble( &yMinOk );
546  bool yMaxOk = false;
547  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
548  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
549  {
550  QgsRectangle rect( xMin, yMin, xMax, yMax );
551  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
552  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
553  {
554  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
555  try
556  {
557  return ct.transformBoundingBox( rect );
558  }
559  catch ( QgsCsException & )
560  {
561  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
562  }
563  }
564  return rect;
565  }
566  }
567 
568  // try as layer extent
569  if ( !layer )
570  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
571 
572  if ( layer )
573  {
574  QgsRectangle rect = layer->extent();
575  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
576  {
577  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
578  try
579  {
580  return ct.transformBoundingBox( rect );
581  }
582  catch ( QgsCsException & )
583  {
584  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
585  }
586  }
587  return rect;
588  }
589  return QgsRectangle();
590 }
591 
593 {
594  if ( !definition )
595  return QgsGeometry();
596 
597  QVariant val = parameters.value( definition->name() );
598 
599  if ( val.canConvert< QgsReferencedRectangle >() )
600  {
603  if ( crs.isValid() && rr.crs().isValid() && crs != rr.crs() )
604  {
605  g = g.densifyByCount( 20 );
606  QgsCoordinateTransform ct( rr.crs(), crs, context.project() );
607  try
608  {
609  g.transform( ct );
610  }
611  catch ( QgsCsException & )
612  {
613  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
614  }
615  return g;
616  }
617  }
618 
619  QString rectText;
620  if ( val.canConvert<QgsProperty>() )
621  rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
622  else
623  rectText = val.toString();
624 
625  if ( !rectText.isEmpty() )
626  {
627  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
628  QRegularExpressionMatch match = rx.match( rectText );
629  if ( match.hasMatch() )
630  {
631  bool xMinOk = false;
632  double xMin = match.captured( 1 ).toDouble( &xMinOk );
633  bool xMaxOk = false;
634  double xMax = match.captured( 2 ).toDouble( &xMaxOk );
635  bool yMinOk = false;
636  double yMin = match.captured( 3 ).toDouble( &yMinOk );
637  bool yMaxOk = false;
638  double yMax = match.captured( 4 ).toDouble( &yMaxOk );
639  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
640  {
641  QgsRectangle rect( xMin, yMin, xMax, yMax );
642  QgsCoordinateReferenceSystem rectCrs( match.captured( 5 ) );
644  if ( crs.isValid() && rectCrs.isValid() && crs != rectCrs )
645  {
646  g = g.densifyByCount( 20 );
647  QgsCoordinateTransform ct( rectCrs, crs, context.project() );
648  try
649  {
650  g.transform( ct );
651  }
652  catch ( QgsCsException & )
653  {
654  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
655  }
656  return g;
657  }
658  }
659  }
660  }
661 
662  // try as layer extent
663 
664  // maybe parameter is a direct layer value?
665  QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
666  if ( !layer )
667  layer = QgsProcessingUtils::mapLayerFromString( rectText, context );
668 
669  if ( layer )
670  {
671  QgsRectangle rect = layer->extent();
673  if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
674  {
675  g = g.densifyByCount( 20 );
676  QgsCoordinateTransform ct( layer->crs(), crs, context.project() );
677  try
678  {
679  g.transform( ct );
680  }
681  catch ( QgsCsException & )
682  {
683  QgsMessageLog::logMessage( QObject::tr( "Error transforming extent geometry" ) );
684  }
685  }
686  return g;
687  }
688 
689  return QgsGeometry::fromRect( parameterAsExtent( definition, parameters, context, crs ) );
690 }
691 
693 {
694  QVariant val = parameters.value( definition->name() );
695 
696  if ( val.canConvert< QgsReferencedRectangle >() )
697  {
699  if ( rr.crs().isValid() )
700  {
701  return rr.crs();
702  }
703  }
704 
705  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
706 
707  QString valueAsString = parameterAsString( definition, parameters, context );
708  QRegularExpressionMatch match = rx.match( valueAsString );
709  if ( match.hasMatch() )
710  {
711  QgsCoordinateReferenceSystem crs( match.captured( 5 ) );
712  if ( crs.isValid() )
713  return crs;
714  }
715 
716  // try as layer crs
717  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
718  return layer->crs();
719  else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
720  return layer->crs();
721 
722  if ( context.project() )
723  return context.project()->crs();
724  else
726 }
727 
729 {
730  if ( !definition )
731  return QgsPointXY();
732 
733  QVariant val = parameters.value( definition->name() );
734  if ( val.canConvert< QgsPointXY >() )
735  {
736  return val.value<QgsPointXY>();
737  }
738  if ( val.canConvert< QgsReferencedPointXY >() )
739  {
740  QgsReferencedPointXY rp = val.value<QgsReferencedPointXY>();
741  if ( crs.isValid() && rp.crs().isValid() && crs != rp.crs() )
742  {
743  QgsCoordinateTransform ct( rp.crs(), crs, context.project() );
744  try
745  {
746  return ct.transform( rp );
747  }
748  catch ( QgsCsException & )
749  {
750  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
751  }
752  }
753  return rp;
754  }
755 
756  QString pointText = parameterAsString( definition, parameters, context );
757  if ( pointText.isEmpty() )
758  pointText = definition->defaultValue().toString();
759 
760  if ( pointText.isEmpty() )
761  return QgsPointXY();
762 
763  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
764 
765  QString valueAsString = parameterAsString( definition, parameters, context );
766  QRegularExpressionMatch match = rx.match( valueAsString );
767  if ( match.hasMatch() )
768  {
769  bool xOk = false;
770  double x = match.captured( 1 ).toDouble( &xOk );
771  bool yOk = false;
772  double y = match.captured( 2 ).toDouble( &yOk );
773 
774  if ( xOk && yOk )
775  {
776  QgsPointXY pt( x, y );
777 
778  QgsCoordinateReferenceSystem pointCrs( match.captured( 3 ) );
779  if ( crs.isValid() && pointCrs.isValid() && crs != pointCrs )
780  {
781  QgsCoordinateTransform ct( pointCrs, crs, context.project() );
782  try
783  {
784  return ct.transform( pt );
785  }
786  catch ( QgsCsException & )
787  {
788  QgsMessageLog::logMessage( QObject::tr( "Error transforming point geometry" ) );
789  }
790  }
791  return pt;
792  }
793  }
794 
795  return QgsPointXY();
796 }
797 
799 {
800  QVariant val = parameters.value( definition->name() );
801 
802  if ( val.canConvert< QgsReferencedPointXY >() )
803  {
804  QgsReferencedPointXY rr = val.value<QgsReferencedPointXY>();
805  if ( rr.crs().isValid() )
806  {
807  return rr.crs();
808  }
809  }
810 
811  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
812 
813  QString valueAsString = parameterAsString( definition, parameters, context );
814  QRegularExpressionMatch match = rx.match( valueAsString );
815  if ( match.hasMatch() )
816  {
817  QgsCoordinateReferenceSystem crs( match.captured( 3 ) );
818  if ( crs.isValid() )
819  return crs;
820  }
821 
822  if ( context.project() )
823  return context.project()->crs();
824  else
826 }
827 
828 QString QgsProcessingParameters::parameterAsFile( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
829 {
830  if ( !definition )
831  return QString();
832 
833  QString fileText = parameterAsString( definition, parameters, context );
834  if ( fileText.isEmpty() )
835  fileText = definition->defaultValue().toString();
836  return fileText;
837 }
838 
839 QVariantList QgsProcessingParameters::parameterAsMatrix( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
840 {
841  if ( !definition )
842  return QVariantList();
843 
844  QString resultString;
845  QVariant val = parameters.value( definition->name() );
846  if ( val.canConvert<QgsProperty>() )
847  resultString = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
848  else if ( val.type() == QVariant::List )
849  return val.toList();
850  else
851  resultString = val.toString();
852 
853  if ( resultString.isEmpty() )
854  {
855  // check default
856  if ( definition->defaultValue().type() == QVariant::List )
857  return definition->defaultValue().toList();
858  else
859  resultString = definition->defaultValue().toString();
860  }
861 
862  QVariantList result;
863  Q_FOREACH ( const QString &s, resultString.split( ',' ) )
864  result << s;
865 
866  return result;
867 }
868 
869 QList<QgsMapLayer *> QgsProcessingParameters::parameterAsLayerList( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
870 {
871  if ( !definition )
872  return QList<QgsMapLayer *>();
873 
874  QVariant val = parameters.value( definition->name() );
875  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
876  {
877  return QList<QgsMapLayer *>() << layer;
878  }
879 
880  QList<QgsMapLayer *> layers;
881 
882  QStringList resultStringList;
883 
884  std::function< void( const QVariant &var ) > processVariant;
885  processVariant = [ &resultStringList, &layers, &context, &definition, &processVariant ]( const QVariant & var )
886  {
887  if ( var.type() == QVariant::List )
888  {
889  Q_FOREACH ( const QVariant &listVar, var.toList() )
890  {
891  processVariant( listVar );
892  }
893  }
894  else if ( var.type() == QVariant::StringList )
895  {
896  Q_FOREACH ( const QString &s, var.toStringList() )
897  {
898  resultStringList << s;
899  }
900  }
901  else if ( var.canConvert<QgsProperty>() )
902  resultStringList << var.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
903  else if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
904  {
905  layers << layer;
906  }
907  else
908  {
909  resultStringList << var.toString();
910  }
911  };
912 
913  processVariant( val );
914 
915  if ( layers.isEmpty() && ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
916  {
917  resultStringList.clear();
918  // check default
919  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
920  {
921  layers << layer;
922  }
923  else if ( definition->defaultValue().type() == QVariant::List )
924  {
925  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
926  {
927  if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( var ) ) )
928  {
929  layers << layer;
930  }
931  else
932  {
933  resultStringList << var.toString();
934  }
935  }
936  }
937  else
938  resultStringList << definition->defaultValue().toString();
939  }
940 
941  Q_FOREACH ( const QString &s, resultStringList )
942  {
944  if ( layer )
945  layers << layer;
946  }
947 
948  return layers;
949 }
950 
951 QList<double> QgsProcessingParameters::parameterAsRange( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
952 {
953  if ( !definition )
954  return QList<double>();
955 
956  QStringList resultStringList;
957  QVariant val = parameters.value( definition->name() );
958  if ( val.canConvert<QgsProperty>() )
959  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
960  else if ( val.type() == QVariant::List )
961  {
962  Q_FOREACH ( const QVariant &var, val.toList() )
963  resultStringList << var.toString();
964  }
965  else
966  resultStringList << val.toString();
967 
968  if ( ( resultStringList.isEmpty() || ( resultStringList.size() == 1 && resultStringList.at( 0 ).isEmpty() ) ) )
969  {
970  resultStringList.clear();
971  // check default
972  if ( definition->defaultValue().type() == QVariant::List )
973  {
974  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
975  resultStringList << var.toString();
976  }
977  else
978  resultStringList << definition->defaultValue().toString();
979  }
980 
981  if ( resultStringList.size() == 1 )
982  {
983  resultStringList = resultStringList.at( 0 ).split( ',' );
984  }
985 
986  if ( resultStringList.size() < 2 )
987  return QList< double >() << 0.0 << 0.0;
988 
989  return QList< double >() << resultStringList.at( 0 ).toDouble() << resultStringList.at( 1 ).toDouble();
990 }
991 
992 QStringList QgsProcessingParameters::parameterAsFields( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context )
993 {
994  if ( !definition )
995  return QStringList();
996 
997  QStringList resultStringList;
998  QVariant val = parameters.value( definition->name() );
999  if ( val.isValid() )
1000  {
1001  if ( val.canConvert<QgsProperty>() )
1002  resultStringList << val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
1003  else if ( val.type() == QVariant::List )
1004  {
1005  Q_FOREACH ( const QVariant &var, val.toList() )
1006  resultStringList << var.toString();
1007  }
1008  else
1009  resultStringList.append( val.toString().split( ';' ) );
1010  }
1011 
1012  if ( ( resultStringList.isEmpty() || resultStringList.at( 0 ).isEmpty() ) )
1013  {
1014  resultStringList.clear();
1015  // check default
1016  if ( definition->defaultValue().isValid() )
1017  {
1018  if ( definition->defaultValue().type() == QVariant::List )
1019  {
1020  Q_FOREACH ( const QVariant &var, definition->defaultValue().toList() )
1021  resultStringList << var.toString();
1022  }
1023  else
1024  resultStringList.append( definition->defaultValue().toString().split( ';' ) );
1025  }
1026  }
1027 
1028  return resultStringList;
1029 }
1030 
1032 {
1033  QString type = map.value( QStringLiteral( "parameter_type" ) ).toString();
1034  QString name = map.value( QStringLiteral( "name" ) ).toString();
1035  std::unique_ptr< QgsProcessingParameterDefinition > def;
1037  def.reset( new QgsProcessingParameterBoolean( name ) );
1038  else if ( type == QgsProcessingParameterCrs::typeName() )
1039  def.reset( new QgsProcessingParameterCrs( name ) );
1040  else if ( type == QgsProcessingParameterMapLayer::typeName() )
1041  def.reset( new QgsProcessingParameterMapLayer( name ) );
1042  else if ( type == QgsProcessingParameterExtent::typeName() )
1043  def.reset( new QgsProcessingParameterExtent( name ) );
1044  else if ( type == QgsProcessingParameterPoint::typeName() )
1045  def.reset( new QgsProcessingParameterPoint( name ) );
1046  else if ( type == QgsProcessingParameterFile::typeName() )
1047  def.reset( new QgsProcessingParameterFile( name ) );
1048  else if ( type == QgsProcessingParameterMatrix::typeName() )
1049  def.reset( new QgsProcessingParameterMatrix( name ) );
1051  def.reset( new QgsProcessingParameterMultipleLayers( name ) );
1052  else if ( type == QgsProcessingParameterNumber::typeName() )
1053  def.reset( new QgsProcessingParameterNumber( name ) );
1054  else if ( type == QgsProcessingParameterRange::typeName() )
1055  def.reset( new QgsProcessingParameterRange( name ) );
1056  else if ( type == QgsProcessingParameterRasterLayer::typeName() )
1057  def.reset( new QgsProcessingParameterRasterLayer( name ) );
1058  else if ( type == QgsProcessingParameterEnum::typeName() )
1059  def.reset( new QgsProcessingParameterEnum( name ) );
1060  else if ( type == QgsProcessingParameterString::typeName() )
1061  def.reset( new QgsProcessingParameterString( name ) );
1062  else if ( type == QgsProcessingParameterExpression::typeName() )
1063  def.reset( new QgsProcessingParameterExpression( name ) );
1064  else if ( type == QgsProcessingParameterVectorLayer::typeName() )
1065  def.reset( new QgsProcessingParameterVectorLayer( name ) );
1066  else if ( type == QgsProcessingParameterField::typeName() )
1067  def.reset( new QgsProcessingParameterField( name ) );
1068  else if ( type == QgsProcessingParameterFeatureSource::typeName() )
1069  def.reset( new QgsProcessingParameterFeatureSource( name ) );
1070  else if ( type == QgsProcessingParameterFeatureSink::typeName() )
1071  def.reset( new QgsProcessingParameterFeatureSink( name ) );
1073  def.reset( new QgsProcessingParameterVectorDestination( name ) );
1075  def.reset( new QgsProcessingParameterRasterDestination( name ) );
1077  def.reset( new QgsProcessingParameterFileDestination( name ) );
1079  def.reset( new QgsProcessingParameterFolderDestination( name ) );
1080  else if ( type == QgsProcessingParameterBand::typeName() )
1081  def.reset( new QgsProcessingParameterBand( name ) );
1082 
1083  if ( !def )
1084  return nullptr;
1085 
1086  def->fromVariantMap( map );
1087  return def.release();
1088 }
1089 
1090 QString QgsProcessingParameters::descriptionFromName( const QString &name )
1091 {
1092  QString desc = name;
1093  desc.replace( '_', ' ' );
1094  return desc;
1095 }
1096 
1098 {
1099  bool isOptional = false;
1100  QString name;
1101  QString definition;
1102  QString type;
1103  if ( !parseScriptCodeParameterOptions( code, isOptional, name, type, definition ) )
1104  return nullptr;
1105 
1106  QString description = descriptionFromName( name );
1107 
1108  if ( type == QStringLiteral( "boolean" ) )
1109  return QgsProcessingParameterBoolean::fromScriptCode( name, description, isOptional, definition );
1110  else if ( type == QStringLiteral( "crs" ) )
1111  return QgsProcessingParameterCrs::fromScriptCode( name, description, isOptional, definition );
1112  else if ( type == QStringLiteral( "layer" ) )
1113  return QgsProcessingParameterMapLayer::fromScriptCode( name, description, isOptional, definition );
1114  else if ( type == QStringLiteral( "extent" ) )
1115  return QgsProcessingParameterExtent::fromScriptCode( name, description, isOptional, definition );
1116  else if ( type == QStringLiteral( "point" ) )
1117  return QgsProcessingParameterPoint::fromScriptCode( name, description, isOptional, definition );
1118  else if ( type == QStringLiteral( "file" ) )
1119  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::File );
1120  else if ( type == QStringLiteral( "folder" ) )
1121  return QgsProcessingParameterFile::fromScriptCode( name, description, isOptional, definition, QgsProcessingParameterFile::Folder );
1122  else if ( type == QStringLiteral( "matrix" ) )
1123  return QgsProcessingParameterMatrix::fromScriptCode( name, description, isOptional, definition );
1124  else if ( type == QStringLiteral( "multiple" ) )
1125  return QgsProcessingParameterMultipleLayers::fromScriptCode( name, description, isOptional, definition );
1126  else if ( type == QStringLiteral( "number" ) )
1127  return QgsProcessingParameterNumber::fromScriptCode( name, description, isOptional, definition );
1128  else if ( type == QStringLiteral( "range" ) )
1129  return QgsProcessingParameterRange::fromScriptCode( name, description, isOptional, definition );
1130  else if ( type == QStringLiteral( "raster" ) )
1131  return QgsProcessingParameterRasterLayer::fromScriptCode( name, description, isOptional, definition );
1132  else if ( type == QStringLiteral( "enum" ) )
1133  return QgsProcessingParameterEnum::fromScriptCode( name, description, isOptional, definition );
1134  else if ( type == QStringLiteral( "string" ) )
1135  return QgsProcessingParameterString::fromScriptCode( name, description, isOptional, definition );
1136  else if ( type == QStringLiteral( "expression" ) )
1137  return QgsProcessingParameterExpression::fromScriptCode( name, description, isOptional, definition );
1138  else if ( type == QStringLiteral( "field" ) )
1139  return QgsProcessingParameterField::fromScriptCode( name, description, isOptional, definition );
1140  else if ( type == QStringLiteral( "vector" ) )
1141  return QgsProcessingParameterVectorLayer::fromScriptCode( name, description, isOptional, definition );
1142  else if ( type == QStringLiteral( "source" ) )
1143  return QgsProcessingParameterFeatureSource::fromScriptCode( name, description, isOptional, definition );
1144  else if ( type == QStringLiteral( "sink" ) )
1145  return QgsProcessingParameterFeatureSink::fromScriptCode( name, description, isOptional, definition );
1146  else if ( type == QStringLiteral( "vectordestination" ) )
1147  return QgsProcessingParameterVectorDestination::fromScriptCode( name, description, isOptional, definition );
1148  else if ( type == QStringLiteral( "rasterdestination" ) )
1149  return QgsProcessingParameterRasterDestination::fromScriptCode( name, description, isOptional, definition );
1150  else if ( type == QStringLiteral( "filedestination" ) )
1151  return QgsProcessingParameterFileDestination::fromScriptCode( name, description, isOptional, definition );
1152  else if ( type == QStringLiteral( "folderdestination" ) )
1153  return QgsProcessingParameterFolderDestination::fromScriptCode( name, description, isOptional, definition );
1154  else if ( type == QStringLiteral( "band" ) )
1155  return QgsProcessingParameterBand::fromScriptCode( name, description, isOptional, definition );
1156 
1157  return nullptr;
1158 }
1159 
1160 bool QgsProcessingParameters::parseScriptCodeParameterOptions( const QString &code, bool &isOptional, QString &name, QString &type, QString &definition )
1161 {
1162  QRegularExpression re( QStringLiteral( "(?:#*)(.*?)=\\s*(.*)" ) );
1163  QRegularExpressionMatch m = re.match( code );
1164  if ( !m.hasMatch() )
1165  return false;
1166 
1167  name = m.captured( 1 );
1168  QString tokens = m.captured( 2 );
1169  if ( tokens.startsWith( QStringLiteral( "optional" ), Qt::CaseInsensitive ) )
1170  {
1171  isOptional = true;
1172  tokens.remove( 0, 8 ); // length "optional" = 8
1173  }
1174  else
1175  {
1176  isOptional = false;
1177  }
1178 
1179  tokens = tokens.trimmed();
1180 
1181  QRegularExpression re2( QStringLiteral( "(.*?)\\s+(.*)" ) );
1182  m = re2.match( tokens );
1183  if ( !m.hasMatch() )
1184  {
1185  type = tokens.toLower().trimmed();
1186  definition.clear();
1187  }
1188  else
1189  {
1190  type = m.captured( 1 ).toLower().trimmed();
1191  definition = m.captured( 2 );
1192  }
1193  return true;
1194 }
1195 
1196 //
1197 // QgsProcessingParameterDefinition
1198 //
1199 
1200 QgsProcessingParameterDefinition::QgsProcessingParameterDefinition( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1201  : mName( name )
1202  , mDescription( description )
1203  , mDefault( defaultValue )
1204  , mFlags( optional ? FlagOptional : 0 )
1205 {}
1206 
1208 {
1209  if ( !input.isValid() )
1210  return mFlags & FlagOptional;
1211 
1212  if ( input.type() == QVariant::String && input.toString().isEmpty() )
1213  return mFlags & FlagOptional;
1214 
1215  return true;
1216 }
1217 
1219 {
1220  if ( value.canConvert<QgsProperty>() )
1221  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1222 
1223  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
1224 }
1225 
1227 {
1228  QString code = QStringLiteral( "##%1=" ).arg( mName );
1229  if ( mFlags & FlagOptional )
1230  code += QStringLiteral( "optional " );
1231  code += type() + ' ';
1232  code += mDefault.toString();
1233  return code.trimmed();
1234 }
1235 
1237 {
1238  QVariantMap map;
1239  map.insert( QStringLiteral( "parameter_type" ), type() );
1240  map.insert( QStringLiteral( "name" ), mName );
1241  map.insert( QStringLiteral( "description" ), mDescription );
1242  map.insert( QStringLiteral( "default" ), mDefault );
1243  map.insert( QStringLiteral( "flags" ), static_cast< int >( mFlags ) );
1244  map.insert( QStringLiteral( "metadata" ), mMetadata );
1245  return map;
1246 }
1247 
1249 {
1250  mName = map.value( QStringLiteral( "name" ) ).toString();
1251  mDescription = map.value( QStringLiteral( "description" ) ).toString();
1252  mDefault = map.value( QStringLiteral( "default" ) );
1253  mFlags = static_cast< Flags >( map.value( QStringLiteral( "flags" ) ).toInt() );
1254  mMetadata = map.value( QStringLiteral( "metadata" ) ).toMap();
1255  return true;
1256 }
1257 
1259 {
1260  return mAlgorithm;
1261 }
1262 
1264 {
1265  return mAlgorithm ? mAlgorithm->provider() : nullptr;
1266 }
1267 
1269 {
1270  return QStringLiteral( "<p><b>%1</b></p><p>%2</p>" ).arg(
1271  description(),
1272  QObject::tr( "Python identifier: ‘%1’" ).arg( QStringLiteral( "<i>%1</i>" ).arg( name() ) ) );
1273 }
1274 
1275 QgsProcessingParameterBoolean::QgsProcessingParameterBoolean( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1276  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1277 {}
1278 
1280 {
1281  return new QgsProcessingParameterBoolean( *this );
1282 }
1283 
1285 {
1286  if ( val.canConvert<QgsProperty>() )
1287  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1288  return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
1289 }
1290 
1292 {
1293  QString code = QStringLiteral( "##%1=" ).arg( mName );
1294  if ( mFlags & FlagOptional )
1295  code += QStringLiteral( "optional " );
1296  code += type() + ' ';
1297  code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
1298  return code.trimmed();
1299 }
1300 
1301 QgsProcessingParameterBoolean *QgsProcessingParameterBoolean::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1302 {
1303  return new QgsProcessingParameterBoolean( name, description, definition.toLower().trimmed() != QStringLiteral( "false" ), isOptional );
1304 }
1305 
1306 QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1307  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1308 {
1309 
1310 }
1311 
1313 {
1314  return new QgsProcessingParameterCrs( *this );
1315 }
1316 
1318 {
1319  if ( !input.isValid() )
1320  return mFlags & FlagOptional;
1321 
1322  if ( input.canConvert<QgsProperty>() )
1323  {
1324  return true;
1325  }
1326 
1327  // direct map layer value
1328  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1329  return true;
1330 
1331  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1332  return mFlags & FlagOptional;
1333 
1334  return true;
1335 }
1336 
1337 QString QgsProcessingParameterCrs::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1338 {
1339  if ( value.canConvert<QgsProperty>() )
1340  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1341 
1342  QVariantMap p;
1343  p.insert( name(), value );
1344  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1345  if ( layer )
1347 
1349 }
1350 
1351 QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1352 {
1353  return new QgsProcessingParameterCrs( name, description, definition.toLower() == QStringLiteral( "none" ) ? QVariant() : definition, isOptional );
1354 }
1355 
1356 QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1357  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1358 {
1359 
1360 }
1361 
1363 {
1364  return new QgsProcessingParameterMapLayer( *this );
1365 }
1366 
1368 {
1369  if ( !input.isValid() )
1370  return mFlags & FlagOptional;
1371 
1372  if ( input.canConvert<QgsProperty>() )
1373  {
1374  return true;
1375  }
1376 
1377  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1378  {
1379  return true;
1380  }
1381 
1382  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1383  return mFlags & FlagOptional;
1384 
1385  if ( !context )
1386  {
1387  // that's as far as we can get without a context
1388  return true;
1389  }
1390 
1391  // try to load as layer
1392  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
1393  return true;
1394 
1395  return false;
1396 }
1397 
1399 {
1400  if ( val.canConvert<QgsProperty>() )
1401  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
1402 
1403  QVariantMap p;
1404  p.insert( name(), val );
1405  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1407  : QString();
1408 }
1409 
1410 QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1411 {
1412  return new QgsProcessingParameterMapLayer( name, description, definition, isOptional );
1413 }
1414 
1415 QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1416  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1417 {
1418 
1419 }
1420 
1422 {
1423  return new QgsProcessingParameterExtent( *this );
1424 }
1425 
1427 {
1428  if ( !input.isValid() )
1429  return mFlags & FlagOptional;
1430 
1431  if ( input.canConvert<QgsProperty>() )
1432  {
1433  return true;
1434  }
1435 
1436  if ( input.canConvert< QgsRectangle >() )
1437  {
1438  QgsRectangle r = input.value<QgsRectangle>();
1439  return !r.isNull();
1440  }
1441  if ( input.canConvert< QgsReferencedRectangle >() )
1442  {
1444  return !r.isNull();
1445  }
1446 
1447  // direct map layer value
1448  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1449  return true;
1450 
1451  if ( input.type() != QVariant::String || input.toString().isEmpty() )
1452  return mFlags & FlagOptional;
1453 
1454  if ( !context )
1455  {
1456  // that's as far as we can get without a context
1457  return true;
1458  }
1459 
1460  QRegularExpression rx( QStringLiteral( "^(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" ) );
1461  QRegularExpressionMatch match = rx.match( input.toString() );
1462  if ( match.hasMatch() )
1463  {
1464  bool xMinOk = false;
1465  ( void )match.captured( 1 ).toDouble( &xMinOk );
1466  bool xMaxOk = false;
1467  ( void )match.captured( 2 ).toDouble( &xMaxOk );
1468  bool yMinOk = false;
1469  ( void )match.captured( 3 ).toDouble( &yMinOk );
1470  bool yMaxOk = false;
1471  ( void )match.captured( 4 ).toDouble( &yMaxOk );
1472  if ( xMinOk && xMaxOk && yMinOk && yMaxOk )
1473  return true;
1474  }
1475 
1476  // try as layer extent
1477  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
1478 }
1479 
1480 QString QgsProcessingParameterExtent::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1481 {
1482  if ( value.canConvert<QgsProperty>() )
1483  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1484 
1485  if ( value.canConvert< QgsRectangle >() )
1486  {
1487  QgsRectangle r = value.value<QgsRectangle>();
1488  return QStringLiteral( "'%1, %3, %2, %4'" ).arg( qgsDoubleToString( r.xMinimum() ),
1489  qgsDoubleToString( r.yMinimum() ),
1490  qgsDoubleToString( r.xMaximum() ),
1491  qgsDoubleToString( r.yMaximum() ) );
1492  }
1493  if ( value.canConvert< QgsReferencedRectangle >() )
1494  {
1496  return QStringLiteral( "'%1, %3, %2, %4 [%5]'" ).arg( qgsDoubleToString( r.xMinimum() ),
1497  qgsDoubleToString( r.yMinimum() ),
1498  qgsDoubleToString( r.xMaximum() ),
1499  qgsDoubleToString( r.yMaximum() ), r.crs().authid() );
1500  }
1501 
1502  QVariantMap p;
1503  p.insert( name(), value );
1504  QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
1505  if ( layer )
1506  return QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' );
1507 
1509 }
1510 
1511 QgsProcessingParameterExtent *QgsProcessingParameterExtent::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1512 {
1513  return new QgsProcessingParameterExtent( name, description, definition, isOptional );
1514 }
1515 
1516 QgsProcessingParameterPoint::QgsProcessingParameterPoint( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
1517  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1518 {
1519 
1520 }
1521 
1523 {
1524  return new QgsProcessingParameterPoint( *this );
1525 }
1526 
1528 {
1529  if ( !input.isValid() )
1530  return mFlags & FlagOptional;
1531 
1532  if ( input.canConvert<QgsProperty>() )
1533  {
1534  return true;
1535  }
1536 
1537  if ( input.canConvert< QgsPointXY >() )
1538  {
1539  return true;
1540  }
1541  if ( input.canConvert< QgsReferencedPointXY >() )
1542  {
1543  return true;
1544  }
1545 
1546  if ( input.type() == QVariant::String )
1547  {
1548  if ( input.toString().isEmpty() )
1549  return mFlags & FlagOptional;
1550  }
1551 
1552  QRegularExpression rx( QStringLiteral( "^\\s*\\(?\\s*(.*?)\\s*,\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*\\)?\\s*$" ) );
1553 
1554  QRegularExpressionMatch match = rx.match( input.toString() );
1555  if ( match.hasMatch() )
1556  {
1557  bool xOk = false;
1558  ( void )match.captured( 1 ).toDouble( &xOk );
1559  bool yOk = false;
1560  ( void )match.captured( 2 ).toDouble( &yOk );
1561  return xOk && yOk;
1562  }
1563  else
1564  return false;
1565 }
1566 
1567 QString QgsProcessingParameterPoint::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1568 {
1569  if ( value.canConvert<QgsProperty>() )
1570  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1571 
1572  if ( value.canConvert< QgsPointXY >() )
1573  {
1574  QgsPointXY r = value.value<QgsPointXY>();
1575  return QStringLiteral( "'%1,%2'" ).arg( qgsDoubleToString( r.x() ),
1576  qgsDoubleToString( r.y() ) );
1577  }
1578  if ( value.canConvert< QgsReferencedPointXY >() )
1579  {
1580  QgsReferencedPointXY r = value.value<QgsReferencedPointXY>();
1581  return QStringLiteral( "'%1,%2 [%3]'" ).arg( qgsDoubleToString( r.x() ),
1582  qgsDoubleToString( r.y() ),
1583  r.crs().authid() );
1584  }
1585 
1587 }
1588 
1589 QgsProcessingParameterPoint *QgsProcessingParameterPoint::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1590 {
1591  return new QgsProcessingParameterPoint( name, description, definition, isOptional );
1592 }
1593 
1594 QgsProcessingParameterFile::QgsProcessingParameterFile( const QString &name, const QString &description, Behavior behavior, const QString &extension, const QVariant &defaultValue, bool optional )
1595  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1596  , mBehavior( behavior )
1597  , mExtension( extension )
1598 {
1599 
1600 }
1601 
1603 {
1604  return new QgsProcessingParameterFile( *this );
1605 }
1606 
1608 {
1609  if ( !input.isValid() )
1610  return mFlags & FlagOptional;
1611 
1612  if ( input.canConvert<QgsProperty>() )
1613  {
1614  return true;
1615  }
1616 
1617  QString string = input.toString().trimmed();
1618 
1619  if ( input.type() != QVariant::String || string.isEmpty() )
1620  return mFlags & FlagOptional;
1621 
1622  switch ( mBehavior )
1623  {
1624  case File:
1625  {
1626  if ( !mExtension.isEmpty() )
1627  return string.endsWith( mExtension, Qt::CaseInsensitive );
1628  return true;
1629  }
1630 
1631  case Folder:
1632  return true;
1633  }
1634  return true;
1635 }
1636 
1638 {
1639  QString code = QStringLiteral( "##%1=" ).arg( mName );
1640  if ( mFlags & FlagOptional )
1641  code += QStringLiteral( "optional " );
1642  code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
1643  code += mDefault.toString();
1644  return code.trimmed();
1645 }
1646 
1648 {
1650  map.insert( QStringLiteral( "behavior" ), mBehavior );
1651  map.insert( QStringLiteral( "extension" ), mExtension );
1652  return map;
1653 }
1654 
1655 bool QgsProcessingParameterFile::fromVariantMap( const QVariantMap &map )
1656 {
1658  mBehavior = static_cast< Behavior >( map.value( QStringLiteral( "behavior" ) ).toInt() );
1659  mExtension = map.value( QStringLiteral( "extension" ) ).toString();
1660  return true;
1661 }
1662 
1664 {
1665  return new QgsProcessingParameterFile( name, description, behavior, QString(), definition, isOptional );
1666 }
1667 
1668 QgsProcessingParameterMatrix::QgsProcessingParameterMatrix( const QString &name, const QString &description, int numberRows, bool fixedNumberRows, const QStringList &headers, const QVariant &defaultValue, bool optional )
1669  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1670  , mHeaders( headers )
1671  , mNumberRows( numberRows )
1672  , mFixedNumberRows( fixedNumberRows )
1673 {
1674 
1675 }
1676 
1678 {
1679  return new QgsProcessingParameterMatrix( *this );
1680 }
1681 
1683 {
1684  if ( !input.isValid() )
1685  return mFlags & FlagOptional;
1686 
1687  if ( input.type() == QVariant::String )
1688  {
1689  if ( input.toString().isEmpty() )
1690  return mFlags & FlagOptional;
1691  return true;
1692  }
1693  else if ( input.type() == QVariant::List )
1694  {
1695  if ( input.toList().isEmpty() )
1696  return mFlags & FlagOptional;
1697  return true;
1698  }
1699  else if ( input.type() == QVariant::Double || input.type() == QVariant::Int )
1700  {
1701  return true;
1702  }
1703 
1704  return false;
1705 }
1706 
1707 QString QgsProcessingParameterMatrix::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
1708 {
1709  if ( value.canConvert<QgsProperty>() )
1710  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1711 
1712  QVariantMap p;
1713  p.insert( name(), value );
1714  QVariantList list = QgsProcessingParameters::parameterAsMatrix( this, p, context );
1715 
1716  QStringList parts;
1717  Q_FOREACH ( const QVariant &v, list )
1718  {
1719  if ( v.type() == QVariant::List )
1720  {
1721  QStringList parts2;
1722  Q_FOREACH ( const QVariant &v2, v.toList() )
1723  {
1724  parts2 << v2.toString();
1725  }
1726  parts << parts2.join( ',' ).prepend( '[' ).append( ']' );
1727  }
1728  else
1729  {
1730  parts << v.toString();
1731  }
1732  }
1733 
1734  return parts.join( ',' ).prepend( '[' ).append( ']' );
1735 }
1736 
1738 {
1739  return mHeaders;
1740 }
1741 
1743 {
1744  mHeaders = headers;
1745 }
1746 
1748 {
1749  return mNumberRows;
1750 }
1751 
1753 {
1754  mNumberRows = numberRows;
1755 }
1756 
1758 {
1759  return mFixedNumberRows;
1760 }
1761 
1763 {
1764  mFixedNumberRows = fixedNumberRows;
1765 }
1766 
1768 {
1770  map.insert( QStringLiteral( "headers" ), mHeaders );
1771  map.insert( QStringLiteral( "rows" ), mNumberRows );
1772  map.insert( QStringLiteral( "fixed_number_rows" ), mFixedNumberRows );
1773  return map;
1774 }
1775 
1776 bool QgsProcessingParameterMatrix::fromVariantMap( const QVariantMap &map )
1777 {
1779  mHeaders = map.value( QStringLiteral( "headers" ) ).toStringList();
1780  mNumberRows = map.value( QStringLiteral( "rows" ) ).toInt();
1781  mFixedNumberRows = map.value( QStringLiteral( "fixed_number_rows" ) ).toBool();
1782  return true;
1783 }
1784 
1785 QgsProcessingParameterMatrix *QgsProcessingParameterMatrix::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1786 {
1787  return new QgsProcessingParameterMatrix( name, description, 0, false, QStringList(), definition.isEmpty() ? QVariant() : definition, isOptional );
1788 }
1789 
1791  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1792  , mLayerType( layerType )
1793 {
1794 
1795 }
1796 
1798 {
1799  return new QgsProcessingParameterMultipleLayers( *this );
1800 }
1801 
1803 {
1804  if ( !input.isValid() )
1805  return mFlags & FlagOptional;
1806 
1807  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
1808  {
1809  return true;
1810  }
1811 
1812  if ( input.type() == QVariant::String )
1813  {
1814  if ( input.toString().isEmpty() )
1815  return mFlags & FlagOptional;
1816 
1817  if ( mMinimumNumberInputs > 1 )
1818  return false;
1819 
1820  if ( !context )
1821  return true;
1822 
1823  return QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
1824  }
1825  else if ( input.type() == QVariant::List )
1826  {
1827  if ( input.toList().count() < mMinimumNumberInputs )
1828  return mFlags & FlagOptional;
1829 
1830  if ( mMinimumNumberInputs > input.toList().count() )
1831  return false;
1832 
1833  if ( !context )
1834  return true;
1835 
1836  Q_FOREACH ( const QVariant &v, input.toList() )
1837  {
1838  if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( v ) ) )
1839  continue;
1840 
1841  if ( !QgsProcessingUtils::mapLayerFromString( v.toString(), *context ) )
1842  return false;
1843  }
1844  return true;
1845  }
1846  else if ( input.type() == QVariant::StringList )
1847  {
1848  if ( input.toStringList().count() < mMinimumNumberInputs )
1849  return mFlags & FlagOptional;
1850 
1851  if ( mMinimumNumberInputs > input.toStringList().count() )
1852  return false;
1853 
1854  if ( !context )
1855  return true;
1856 
1857  Q_FOREACH ( const QString &v, input.toStringList() )
1858  {
1859  if ( !QgsProcessingUtils::mapLayerFromString( v, *context ) )
1860  return false;
1861  }
1862  return true;
1863  }
1864  return false;
1865 }
1866 
1868 {
1869  if ( value.canConvert<QgsProperty>() )
1870  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
1871 
1872  QVariantMap p;
1873  p.insert( name(), value );
1874  QList<QgsMapLayer *> list = QgsProcessingParameters::parameterAsLayerList( this, p, context );
1875  if ( !list.isEmpty() )
1876  {
1877  QStringList parts;
1878  Q_FOREACH ( const QgsMapLayer *layer, list )
1879  {
1881  }
1882  return parts.join( ',' ).prepend( '[' ).append( ']' );
1883  }
1884 
1886 }
1887 
1889 {
1890  QString code = QStringLiteral( "##%1=" ).arg( mName );
1891  if ( mFlags & FlagOptional )
1892  code += QStringLiteral( "optional " );
1893  switch ( mLayerType )
1894  {
1896  code += QStringLiteral( "multiple raster" );
1897  break;
1898 
1900  code += QStringLiteral( "multiple file" );
1901  break;
1902 
1903  default:
1904  code += QStringLiteral( "multiple vector" );
1905  break;
1906  }
1907  code += ' ';
1908  if ( mDefault.type() == QVariant::List )
1909  {
1910  QStringList parts;
1911  Q_FOREACH ( const QVariant &var, mDefault.toList() )
1912  {
1913  parts << var.toString();
1914  }
1915  code += parts.join( ',' );
1916  }
1917  else if ( mDefault.type() == QVariant::StringList )
1918  {
1919  code += mDefault.toStringList().join( ',' );
1920  }
1921  else
1922  {
1923  code += mDefault.toString();
1924  }
1925  return code.trimmed();
1926 }
1927 
1929 {
1930  return mLayerType;
1931 }
1932 
1934 {
1935  mLayerType = type;
1936 }
1937 
1939 {
1940  return mMinimumNumberInputs;
1941 }
1942 
1944 {
1945  if ( mMinimumNumberInputs >= 1 || !( flags() & QgsProcessingParameterDefinition::FlagOptional ) )
1946  mMinimumNumberInputs = minimumNumberInputs;
1947 }
1948 
1950 {
1952  map.insert( QStringLiteral( "layer_type" ), mLayerType );
1953  map.insert( QStringLiteral( "min_inputs" ), mMinimumNumberInputs );
1954  return map;
1955 }
1956 
1958 {
1960  mLayerType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "layer_type" ) ).toInt() );
1961  mMinimumNumberInputs = map.value( QStringLiteral( "min_inputs" ) ).toInt();
1962  return true;
1963 }
1964 
1965 QgsProcessingParameterMultipleLayers *QgsProcessingParameterMultipleLayers::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
1966 {
1967  QString type = definition;
1968  QString defaultVal;
1969  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)" ) );
1970  QRegularExpressionMatch m = re.match( definition );
1971  if ( m.hasMatch() )
1972  {
1973  type = m.captured( 1 ).toLower().trimmed();
1974  defaultVal = m.captured( 2 );
1975  }
1977  if ( type == QStringLiteral( "vector" ) )
1979  else if ( type == QStringLiteral( "raster" ) )
1980  layerType = QgsProcessing::TypeRaster;
1981  else if ( type == QStringLiteral( "file" ) )
1982  layerType = QgsProcessing::TypeFile;
1983  return new QgsProcessingParameterMultipleLayers( name, description, layerType, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
1984 }
1985 
1986 QgsProcessingParameterNumber::QgsProcessingParameterNumber( const QString &name, const QString &description, Type type, const QVariant &defaultValue, bool optional, double minValue, double maxValue )
1987  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
1988  , mMin( minValue )
1989  , mMax( maxValue )
1990  , mDataType( type )
1991 {
1992 
1993 }
1994 
1996 {
1997  return new QgsProcessingParameterNumber( *this );
1998 }
1999 
2001 {
2002  if ( !input.isValid() )
2003  return mFlags & FlagOptional;
2004 
2005  if ( input.canConvert<QgsProperty>() )
2006  {
2007  return true;
2008  }
2009 
2010  bool ok = false;
2011  double res = input.toDouble( &ok );
2012  if ( !ok )
2013  return mFlags & FlagOptional;
2014 
2015  return !( res < mMin || res > mMax );
2016 }
2017 
2019 {
2020  if ( value.canConvert<QgsProperty>() )
2021  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2022 
2023  return value.toString();
2024 }
2025 
2027 {
2029  QStringList parts;
2030  if ( mMin > -DBL_MAX + 1 )
2031  parts << QObject::tr( "Minimum value: %1" ).arg( mMin );
2032  if ( mMax < DBL_MAX )
2033  parts << QObject::tr( "Maximum value: %1" ).arg( mMax );
2034  if ( mDefault.isValid() )
2035  parts << QObject::tr( "Default value: %1" ).arg( mDataType == Integer ? mDefault.toInt() : mDefault.toDouble() );
2036  QString extra = parts.join( QStringLiteral( "<br />" ) );
2037  if ( !extra.isEmpty() )
2038  text += QStringLiteral( "<p>%1</p>" ).arg( extra );
2039  return text;
2040 }
2041 
2043 {
2044  return mMin;
2045 }
2046 
2048 {
2049  mMin = min;
2050 }
2051 
2053 {
2054  return mMax;
2055 }
2056 
2058 {
2059  mMax = max;
2060 }
2061 
2063 {
2064  return mDataType;
2065 }
2066 
2068 {
2069  mDataType = dataType;
2070 }
2071 
2073 {
2075  map.insert( QStringLiteral( "min" ), mMin );
2076  map.insert( QStringLiteral( "max" ), mMax );
2077  map.insert( QStringLiteral( "data_type" ), mDataType );
2078  return map;
2079 }
2080 
2081 bool QgsProcessingParameterNumber::fromVariantMap( const QVariantMap &map )
2082 {
2084  mMin = map.value( QStringLiteral( "min" ) ).toDouble();
2085  mMax = map.value( QStringLiteral( "max" ) ).toDouble();
2086  mDataType = static_cast< Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2087  return true;
2088 }
2089 
2090 QgsProcessingParameterNumber *QgsProcessingParameterNumber::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2091 {
2092  return new QgsProcessingParameterNumber( name, description, Double, definition.isEmpty() ? QVariant()
2093  : ( definition.toLower().trimmed() == QStringLiteral( "none" ) ? QVariant() : definition ), isOptional );
2094 }
2095 
2097  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2098  , mDataType( type )
2099 {
2100 
2101 }
2102 
2104 {
2105  return new QgsProcessingParameterRange( *this );
2106 }
2107 
2109 {
2110  if ( !input.isValid() )
2111  return mFlags & FlagOptional;
2112 
2113  if ( input.canConvert<QgsProperty>() )
2114  {
2115  return true;
2116  }
2117 
2118  if ( input.type() == QVariant::String )
2119  {
2120  QStringList list = input.toString().split( ',' );
2121  if ( list.count() != 2 )
2122  return mFlags & FlagOptional;
2123  bool ok = false;
2124  list.at( 0 ).toDouble( &ok );
2125  bool ok2 = false;
2126  list.at( 1 ).toDouble( &ok2 );
2127  if ( !ok || !ok2 )
2128  return mFlags & FlagOptional;
2129  return true;
2130  }
2131  else if ( input.type() == QVariant::List )
2132  {
2133  if ( input.toList().count() != 2 )
2134  return mFlags & FlagOptional;
2135 
2136  bool ok = false;
2137  input.toList().at( 0 ).toDouble( &ok );
2138  bool ok2 = false;
2139  input.toList().at( 1 ).toDouble( &ok2 );
2140  if ( !ok || !ok2 )
2141  return mFlags & FlagOptional;
2142  return true;
2143  }
2144 
2145  return false;
2146 }
2147 
2148 QString QgsProcessingParameterRange::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
2149 {
2150  if ( value.canConvert<QgsProperty>() )
2151  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2152 
2153  QVariantMap p;
2154  p.insert( name(), value );
2155  QList< double > parts = QgsProcessingParameters::parameterAsRange( this, p, context );
2156 
2157  QStringList stringParts;
2158  Q_FOREACH ( double v, parts )
2159  {
2160  stringParts << QString::number( v );
2161  }
2162  return stringParts.join( ',' ).prepend( '[' ).append( ']' );
2163 }
2164 
2166 {
2167  return mDataType;
2168 }
2169 
2171 {
2172  mDataType = dataType;
2173 }
2174 
2176 {
2178  map.insert( QStringLiteral( "data_type" ), mDataType );
2179  return map;
2180 }
2181 
2182 bool QgsProcessingParameterRange::fromVariantMap( const QVariantMap &map )
2183 {
2185  mDataType = static_cast< QgsProcessingParameterNumber::Type >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2186  return true;
2187 }
2188 
2189 QgsProcessingParameterRange *QgsProcessingParameterRange::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2190 {
2191  return new QgsProcessingParameterRange( name, description, QgsProcessingParameterNumber::Double, definition.isEmpty() ? QVariant() : definition, isOptional );
2192 }
2193 
2194 QgsProcessingParameterRasterLayer::QgsProcessingParameterRasterLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2195  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2196 {
2197 
2198 }
2199 
2201 {
2202  return new QgsProcessingParameterRasterLayer( *this );
2203 }
2204 
2206 {
2207  if ( !input.isValid() )
2208  return mFlags & FlagOptional;
2209 
2210  if ( input.canConvert<QgsProperty>() )
2211  {
2212  return true;
2213  }
2214 
2215  if ( qobject_cast< QgsRasterLayer * >( qvariant_cast<QObject *>( input ) ) )
2216  return true;
2217 
2218  if ( input.type() != QVariant::String || input.toString().isEmpty() )
2219  return mFlags & FlagOptional;
2220 
2221  if ( !context )
2222  {
2223  // that's as far as we can get without a context
2224  return true;
2225  }
2226 
2227  // try to load as layer
2228  if ( QgsProcessingUtils::mapLayerFromString( input.toString(), *context ) )
2229  return true;
2230 
2231  return false;
2232 }
2233 
2235 {
2236  if ( val.canConvert<QgsProperty>() )
2237  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2238 
2239  QVariantMap p;
2240  p.insert( name(), val );
2242  return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
2243 }
2244 
2245 QgsProcessingParameterRasterLayer *QgsProcessingParameterRasterLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2246 {
2247  return new QgsProcessingParameterRasterLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
2248 }
2249 
2250 QgsProcessingParameterEnum::QgsProcessingParameterEnum( const QString &name, const QString &description, const QStringList &options, bool allowMultiple, const QVariant &defaultValue, bool optional )
2251  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2252  , mOptions( options )
2253  , mAllowMultiple( allowMultiple )
2254 {
2255 
2256 }
2257 
2259 {
2260  return new QgsProcessingParameterEnum( *this );
2261 }
2262 
2264 {
2265  if ( !input.isValid() )
2266  return mFlags & FlagOptional;
2267 
2268  if ( input.canConvert<QgsProperty>() )
2269  {
2270  return true;
2271  }
2272 
2273  if ( input.type() == QVariant::List )
2274  {
2275  if ( !mAllowMultiple )
2276  return false;
2277 
2278  const QVariantList values = input.toList();
2279  if ( values.empty() && !( mFlags & FlagOptional ) )
2280  return false;
2281 
2282  for ( const QVariant &val : values )
2283  {
2284  bool ok = false;
2285  int res = val.toInt( &ok );
2286  if ( !ok )
2287  return false;
2288  else if ( res < 0 || res >= mOptions.count() )
2289  return false;
2290  }
2291 
2292  return true;
2293  }
2294  else if ( input.type() == QVariant::String )
2295  {
2296  QStringList parts = input.toString().split( ',' );
2297  if ( parts.count() > 1 && !mAllowMultiple )
2298  return false;
2299 
2300  Q_FOREACH ( const QString &part, parts )
2301  {
2302  bool ok = false;
2303  int res = part.toInt( &ok );
2304  if ( !ok )
2305  return false;
2306  else if ( res < 0 || res >= mOptions.count() )
2307  return false;
2308  }
2309  return true;
2310  }
2311  else if ( input.type() == QVariant::Int || input.type() == QVariant::Double )
2312  {
2313  bool ok = false;
2314  int res = input.toInt( &ok );
2315  if ( !ok )
2316  return false;
2317  else if ( res >= 0 && res < mOptions.count() )
2318  return true;
2319  }
2320  return false;
2321 }
2322 
2324 {
2325  if ( value.canConvert<QgsProperty>() )
2326  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2327 
2328  if ( value.type() == QVariant::List )
2329  {
2330  QStringList parts;
2331  Q_FOREACH ( const QVariant &val, value.toList() )
2332  {
2333  parts << QString::number( static_cast< int >( val.toDouble() ) );
2334  }
2335  return parts.join( ',' ).prepend( '[' ).append( ']' );
2336  }
2337  else if ( value.type() == QVariant::String )
2338  {
2339  QStringList parts = value.toString().split( ',' );
2340  if ( parts.count() > 1 )
2341  {
2342  return parts.join( ',' ).prepend( '[' ).append( ']' );
2343  }
2344  }
2345 
2346  return QString::number( static_cast< int >( value.toDouble() ) );
2347 }
2348 
2350 {
2351  QString code = QStringLiteral( "##%1=" ).arg( mName );
2352  if ( mFlags & FlagOptional )
2353  code += QStringLiteral( "optional " );
2354  code += QStringLiteral( "enum " );
2355 
2356  if ( mAllowMultiple )
2357  code += QStringLiteral( "multiple " );
2358 
2359  code += mOptions.join( ';' ) + ' ';
2360 
2361  code += mDefault.toString();
2362  return code.trimmed();
2363 }
2364 
2366 {
2367  return mOptions;
2368 }
2369 
2371 {
2372  mOptions = options;
2373 }
2374 
2376 {
2377  return mAllowMultiple;
2378 }
2379 
2381 {
2382  mAllowMultiple = allowMultiple;
2383 }
2384 
2386 {
2388  map.insert( QStringLiteral( "options" ), mOptions );
2389  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
2390  return map;
2391 }
2392 
2393 bool QgsProcessingParameterEnum::fromVariantMap( const QVariantMap &map )
2394 {
2396  mOptions = map.value( QStringLiteral( "options" ) ).toStringList();
2397  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
2398  return true;
2399 }
2400 
2401 QgsProcessingParameterEnum *QgsProcessingParameterEnum::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2402 {
2403  QString defaultVal;
2404  bool multiple = false;
2405  QString def = definition;
2406  if ( def.startsWith( QStringLiteral( "multiple" ), Qt::CaseInsensitive ) )
2407  {
2408  multiple = true;
2409  def = def.mid( 9 );
2410  }
2411 
2412  QRegularExpression re( QStringLiteral( "(.*)\\s+(.*?)$" ) );
2413  QRegularExpressionMatch m = re.match( def );
2414  QString values = def;
2415  if ( m.hasMatch() )
2416  {
2417  values = m.captured( 1 ).trimmed();
2418  defaultVal = m.captured( 2 );
2419  }
2420 
2421  return new QgsProcessingParameterEnum( name, description, values.split( ';' ), multiple, defaultVal.isEmpty() ? QVariant() : defaultVal, isOptional );
2422 }
2423 
2424 QgsProcessingParameterString::QgsProcessingParameterString( const QString &name, const QString &description, const QVariant &defaultValue, bool multiLine, bool optional )
2425  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2426  , mMultiLine( multiLine )
2427 {
2428 
2429 }
2430 
2432 {
2433  return new QgsProcessingParameterString( *this );
2434 }
2435 
2437 {
2438  if ( value.canConvert<QgsProperty>() )
2439  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2440 
2441  QString s = value.toString();
2442  s.replace( '\n', QStringLiteral( "\\n" ) );
2443  return s.prepend( '\'' ).append( '\'' );
2444 }
2445 
2447 {
2448  QString code = QStringLiteral( "##%1=" ).arg( mName );
2449  if ( mFlags & FlagOptional )
2450  code += QStringLiteral( "optional " );
2451  code += QStringLiteral( "string " );
2452 
2453  if ( mMultiLine )
2454  code += QStringLiteral( "long " );
2455 
2456  code += mDefault.toString();
2457  return code.trimmed();
2458 }
2459 
2461 {
2462  return mMultiLine;
2463 }
2464 
2466 {
2467  mMultiLine = multiLine;
2468 }
2469 
2471 {
2473  map.insert( QStringLiteral( "multiline" ), mMultiLine );
2474  return map;
2475 }
2476 
2477 bool QgsProcessingParameterString::fromVariantMap( const QVariantMap &map )
2478 {
2480  mMultiLine = map.value( QStringLiteral( "multiline" ) ).toBool();
2481  return true;
2482 }
2483 
2484 QgsProcessingParameterString *QgsProcessingParameterString::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2485 {
2486  QString def = definition;
2487  bool multiLine = false;
2488  if ( def.startsWith( QStringLiteral( "long" ), Qt::CaseInsensitive ) )
2489  {
2490  multiLine = true;
2491  def = def.mid( 5 );
2492  }
2493 
2494  if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
2495  def = def.mid( 1 );
2496  if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
2497  def.chop( 1 );
2498 
2499  QVariant defaultValue = def;
2500  if ( def == QStringLiteral( "None" ) )
2501  defaultValue = QVariant();
2502 
2503  return new QgsProcessingParameterString( name, description, defaultValue, multiLine, isOptional );
2504 }
2505 
2506 QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
2507  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2508  , mParentLayerParameterName( parentLayerParameterName )
2509 {
2510 
2511 }
2512 
2514 {
2515  return new QgsProcessingParameterExpression( *this );
2516 }
2517 
2519 {
2520  if ( value.canConvert<QgsProperty>() )
2521  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2522 
2523  QString s = value.toString();
2524  s.replace( '\n', QStringLiteral( "\\n" ) );
2525  return s.prepend( '\'' ).append( '\'' );
2526 }
2527 
2529 {
2530  QStringList depends;
2531  if ( !mParentLayerParameterName.isEmpty() )
2532  depends << mParentLayerParameterName;
2533  return depends;
2534 }
2535 
2537 {
2538  return mParentLayerParameterName;
2539 }
2540 
2542 {
2543  mParentLayerParameterName = parentLayerParameterName;
2544 }
2545 
2547 {
2549  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
2550  return map;
2551 }
2552 
2554 {
2556  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
2557  return true;
2558 }
2559 
2560 QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2561 {
2562  return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
2563 }
2564 
2565 QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
2566  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2568 {
2569 
2570 }
2571 
2573 {
2574  return new QgsProcessingParameterVectorLayer( *this );
2575 }
2576 
2578 {
2579  if ( !var.isValid() )
2580  return mFlags & FlagOptional;
2581 
2582  if ( var.canConvert<QgsProperty>() )
2583  {
2584  return true;
2585  }
2586 
2587  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( var ) ) )
2588  return true;
2589 
2590  if ( var.type() != QVariant::String || var.toString().isEmpty() )
2591  return mFlags & FlagOptional;
2592 
2593  if ( !context )
2594  {
2595  // that's as far as we can get without a context
2596  return true;
2597  }
2598 
2599  // try to load as layer
2600  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
2601  return true;
2602 
2603  return false;
2604 }
2605 
2607 {
2608  if ( val.canConvert<QgsProperty>() )
2609  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( val.value< QgsProperty >().asExpression() );
2610 
2611  QVariantMap p;
2612  p.insert( name(), val );
2615  : QString();
2616 }
2617 
2619 {
2620  return mDataTypes;
2621 }
2622 
2624 {
2625  mDataTypes = types;
2626 }
2627 
2629 {
2631  QVariantList types;
2632  Q_FOREACH ( int type, mDataTypes )
2633  {
2634  types << type;
2635  }
2636  map.insert( QStringLiteral( "data_types" ), types );
2637  return map;
2638 }
2639 
2641 {
2643  mDataTypes.clear();
2644  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
2645  Q_FOREACH ( const QVariant &val, values )
2646  {
2647  mDataTypes << val.toInt();
2648  }
2649  return true;
2650 }
2651 
2652 QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2653 {
2654  return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
2655 }
2656 
2657 QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
2658  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2659  , mParentLayerParameterName( parentLayerParameterName )
2660  , mDataType( type )
2661  , mAllowMultiple( allowMultiple )
2662 {
2663 
2664 }
2665 
2667 {
2668  return new QgsProcessingParameterField( *this );
2669 }
2670 
2672 {
2673  if ( !input.isValid() )
2674  return mFlags & FlagOptional;
2675 
2676  if ( input.canConvert<QgsProperty>() )
2677  {
2678  return true;
2679  }
2680 
2681  if ( input.type() == QVariant::List || input.type() == QVariant::StringList )
2682  {
2683  if ( !mAllowMultiple )
2684  return false;
2685 
2686  if ( input.toList().isEmpty() && !( mFlags & FlagOptional ) )
2687  return false;
2688  }
2689  else if ( input.type() == QVariant::String )
2690  {
2691  if ( input.toString().isEmpty() )
2692  return mFlags & FlagOptional;
2693 
2694  QStringList parts = input.toString().split( ';' );
2695  if ( parts.count() > 1 && !mAllowMultiple )
2696  return false;
2697  }
2698  else
2699  {
2700  if ( input.toString().isEmpty() )
2701  return mFlags & FlagOptional;
2702  }
2703  return true;
2704 }
2705 
2707 {
2708  if ( value.canConvert<QgsProperty>() )
2709  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2710 
2711  if ( value.type() == QVariant::List )
2712  {
2713  QStringList parts;
2714  Q_FOREACH ( const QVariant &val, value.toList() )
2715  {
2716  parts << QgsProcessingUtils::stringToPythonLiteral( val.toString() );
2717  }
2718  return parts.join( ',' ).prepend( '[' ).append( ']' );
2719  }
2720  else if ( value.type() == QVariant::StringList )
2721  {
2722  QStringList parts;
2723  Q_FOREACH ( QString s, value.toStringList() )
2724  {
2726  }
2727  return parts.join( ',' ).prepend( '[' ).append( ']' );
2728  }
2729 
2730  return QgsProcessingUtils::stringToPythonLiteral( value.toString() );
2731 }
2732 
2734 {
2735  QString code = QStringLiteral( "##%1=" ).arg( mName );
2736  if ( mFlags & FlagOptional )
2737  code += QStringLiteral( "optional " );
2738  code += QStringLiteral( "field " );
2739 
2740  switch ( mDataType )
2741  {
2742  case Numeric:
2743  code += QStringLiteral( "numeric " );
2744  break;
2745 
2746  case String:
2747  code += QStringLiteral( "string " );
2748  break;
2749 
2750  case DateTime:
2751  code += QStringLiteral( "datetime " );
2752  break;
2753 
2754  case Any:
2755  break;
2756  }
2757 
2758  if ( mAllowMultiple )
2759  code += QStringLiteral( "multiple " );
2760 
2761  code += mParentLayerParameterName + ' ';
2762 
2763  code += mDefault.toString();
2764  return code.trimmed();
2765 }
2766 
2768 {
2769  QStringList depends;
2770  if ( !mParentLayerParameterName.isEmpty() )
2771  depends << mParentLayerParameterName;
2772  return depends;
2773 }
2774 
2776 {
2777  return mParentLayerParameterName;
2778 }
2779 
2781 {
2782  mParentLayerParameterName = parentLayerParameterName;
2783 }
2784 
2786 {
2787  return mDataType;
2788 }
2789 
2791 {
2792  mDataType = dataType;
2793 }
2794 
2796 {
2797  return mAllowMultiple;
2798 }
2799 
2801 {
2802  mAllowMultiple = allowMultiple;
2803 }
2804 
2806 {
2808  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
2809  map.insert( QStringLiteral( "data_type" ), mDataType );
2810  map.insert( QStringLiteral( "allow_multiple" ), mAllowMultiple );
2811  return map;
2812 }
2813 
2814 bool QgsProcessingParameterField::fromVariantMap( const QVariantMap &map )
2815 {
2817  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
2818  mDataType = static_cast< DataType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
2819  mAllowMultiple = map.value( QStringLiteral( "allow_multiple" ) ).toBool();
2820  return true;
2821 }
2822 
2823 QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
2824 {
2825  QString parent;
2826  DataType type = Any;
2827  bool allowMultiple = false;
2828  QString def = definition;
2829 
2830  if ( def.startsWith( QStringLiteral( "numeric " ), Qt::CaseInsensitive ) )
2831  {
2832  type = Numeric;
2833  def = def.mid( 8 );
2834  }
2835  else if ( def.startsWith( QStringLiteral( "string " ), Qt::CaseInsensitive ) )
2836  {
2837  type = String;
2838  def = def.mid( 7 );
2839  }
2840  else if ( def.startsWith( QStringLiteral( "datetime " ), Qt::CaseInsensitive ) )
2841  {
2842  type = DateTime;
2843  def = def.mid( 9 );
2844  }
2845 
2846  if ( def.startsWith( QStringLiteral( "multiple" ), Qt::CaseInsensitive ) )
2847  {
2848  allowMultiple = true;
2849  def = def.mid( 8 ).trimmed();
2850  }
2851 
2852  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
2853  QRegularExpressionMatch m = re.match( def );
2854  if ( m.hasMatch() )
2855  {
2856  parent = m.captured( 1 ).trimmed();
2857  def = m.captured( 2 );
2858  }
2859  else
2860  {
2861  parent = def;
2862  def.clear();
2863  }
2864 
2865  return new QgsProcessingParameterField( name, description, def.isEmpty() ? QVariant() : def, parent, type, allowMultiple, isOptional );
2866 }
2867 
2868 QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
2869  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2871 {
2872 
2873 }
2874 
2876 {
2877  return new QgsProcessingParameterFeatureSource( *this );
2878 }
2879 
2881 {
2882  QVariant var = input;
2883  if ( !var.isValid() )
2884  return mFlags & FlagOptional;
2885 
2886  if ( var.canConvert<QgsProcessingFeatureSourceDefinition>() )
2887  {
2889  var = fromVar.source;
2890  }
2891 
2892  if ( var.canConvert<QgsProperty>() )
2893  {
2894  return true;
2895  }
2896  if ( qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( input ) ) )
2897  {
2898  return true;
2899  }
2900 
2901  if ( var.type() != QVariant::String || var.toString().isEmpty() )
2902  return mFlags & FlagOptional;
2903 
2904  if ( !context )
2905  {
2906  // that's as far as we can get without a context
2907  return true;
2908  }
2909 
2910  // try to load as layer
2911  if ( QgsProcessingUtils::mapLayerFromString( var.toString(), *context ) )
2912  return true;
2913 
2914  return false;
2915 }
2916 
2918 {
2919  if ( value.canConvert<QgsProperty>() )
2920  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
2921 
2922  if ( value.canConvert<QgsProcessingFeatureSourceDefinition>() )
2923  {
2925  if ( fromVar.source.propertyType() == QgsProperty::StaticProperty )
2926  {
2927  if ( fromVar.selectedFeaturesOnly )
2928  {
2929  return QStringLiteral( "QgsProcessingFeatureSourceDefinition('%1', True)" ).arg( fromVar.source.staticValue().toString() );
2930  }
2931  else
2932  {
2933  QString layerString = fromVar.source.staticValue().toString();
2934  // prefer to use layer source instead of id if possible (since it's persistent)
2935  if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerString, context ) ) )
2936  layerString = layer->source();
2937  return layerString.prepend( '\'' ).append( '\'' );
2938  }
2939  }
2940  else
2941  {
2942  if ( fromVar.selectedFeaturesOnly )
2943  {
2944  return QStringLiteral( "QgsProcessingFeatureSourceDefinition(QgsProperty.fromExpression('%1'), True)" ).arg( fromVar.source.asExpression() );
2945  }
2946  else
2947  {
2948  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.source.asExpression() );
2949  }
2950  }
2951  }
2952  else if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
2953  {
2954  return layer->source().prepend( '\'' ).append( '\'' );
2955  }
2956 
2957  return value.toString().prepend( '\'' ).append( '\'' );
2958 }
2959 
2961 {
2962  QString code = QStringLiteral( "##%1=" ).arg( mName );
2963  if ( mFlags & FlagOptional )
2964  code += QStringLiteral( "optional " );
2965  code += QStringLiteral( "source " );
2966 
2967  Q_FOREACH ( int type, mDataTypes )
2968  {
2969  switch ( type )
2970  {
2972  code += QStringLiteral( "point " );
2973  break;
2974 
2976  code += QStringLiteral( "line " );
2977  break;
2978 
2980  code += QStringLiteral( "polygon " );
2981  break;
2982 
2983  }
2984  }
2985 
2986  code += mDefault.toString();
2987  return code.trimmed();
2988 }
2989 
2991  : mDataTypes( types )
2992 {
2993 
2994 }
2995 
2997 {
2999  QVariantList types;
3000  Q_FOREACH ( int type, mDataTypes )
3001  {
3002  types << type;
3003  }
3004  map.insert( QStringLiteral( "data_types" ), types );
3005  return map;
3006 }
3007 
3009 {
3011  mDataTypes.clear();
3012  QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
3013  Q_FOREACH ( const QVariant &val, values )
3014  {
3015  mDataTypes << val.toInt();
3016  }
3017  return true;
3018 }
3019 
3020 QgsProcessingParameterFeatureSource *QgsProcessingParameterFeatureSource::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3021 {
3022  QList< int > types;
3023  QString def = definition;
3024  while ( true )
3025  {
3026  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
3027  {
3029  def = def.mid( 6 );
3030  continue;
3031  }
3032  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
3033  {
3035  def = def.mid( 5 );
3036  continue;
3037  }
3038  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
3039  {
3041  def = def.mid( 8 );
3042  continue;
3043  }
3044  break;
3045  }
3046 
3047  return new QgsProcessingParameterFeatureSource( name, description, types, def, isOptional );
3048 }
3049 
3050 QgsProcessingParameterFeatureSink::QgsProcessingParameterFeatureSink( const QString &name, const QString &description, QgsProcessing::SourceType type, const QVariant &defaultValue, bool optional )
3051  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3052  , mDataType( type )
3053 {
3054 
3055 }
3056 
3058 {
3059  return new QgsProcessingParameterFeatureSink( *this );
3060 }
3061 
3063 {
3064  QVariant var = input;
3065  if ( !var.isValid() )
3066  return mFlags & FlagOptional;
3067 
3068  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3069  {
3071  var = fromVar.sink;
3072  }
3073 
3074  if ( var.canConvert<QgsProperty>() )
3075  {
3076  return true;
3077  }
3078 
3079  if ( var.type() != QVariant::String )
3080  return false;
3081 
3082  if ( var.toString().isEmpty() )
3083  return mFlags & FlagOptional;
3084 
3085  return true;
3086 }
3087 
3089 {
3090  if ( value.canConvert<QgsProperty>() )
3091  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3092 
3093  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3094  {
3096  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3097  {
3098  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3099  }
3100  else
3101  {
3102  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3103  }
3104  }
3105 
3106  return value.toString().prepend( '\'' ).append( '\'' );
3107 }
3108 
3110 {
3111  QString code = QStringLiteral( "##%1=" ).arg( mName );
3112  if ( mFlags & FlagOptional )
3113  code += QStringLiteral( "optional " );
3114  code += QStringLiteral( "sink " );
3115 
3116  switch ( mDataType )
3117  {
3119  code += QStringLiteral( "point " );
3120  break;
3121 
3123  code += QStringLiteral( "line " );
3124  break;
3125 
3127  code += QStringLiteral( "polygon " );
3128  break;
3129 
3131  code += QStringLiteral( "table " );
3132  break;
3133 
3134  default:
3135  break;
3136  }
3137 
3138  code += mDefault.toString();
3139  return code.trimmed();
3140 }
3141 
3143 {
3144  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
3145 }
3146 
3148 {
3149  if ( QgsProcessingProvider *p = provider() )
3150  {
3151  return p->defaultVectorFileExtension( hasGeometry() );
3152  }
3153  else
3154  {
3155  QgsSettings settings;
3156  if ( hasGeometry() )
3157  {
3158  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3159  }
3160  else
3161  {
3162  return QStringLiteral( "dbf" );
3163  }
3164  }
3165 }
3166 
3168 {
3169  return mDataType;
3170 }
3171 
3173 {
3174  switch ( mDataType )
3175  {
3182  return true;
3183 
3186  return false;
3187  }
3188  return true;
3189 }
3190 
3192 {
3193  mDataType = type;
3194 }
3195 
3197 {
3199  map.insert( QStringLiteral( "data_type" ), mDataType );
3200  return map;
3201 }
3202 
3204 {
3206  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3207  return true;
3208 }
3209 
3211 {
3213  return QStringLiteral( "memory:%1" ).arg( description() );
3214  else
3216 }
3217 
3218 QgsProcessingParameterFeatureSink *QgsProcessingParameterFeatureSink::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3219 {
3221  QString def = definition;
3222  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
3223  {
3225  def = def.mid( 6 );
3226  }
3227  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
3228  {
3230  def = def.mid( 5 );
3231  }
3232  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
3233  {
3235  def = def.mid( 8 );
3236  }
3237  else if ( def.startsWith( QStringLiteral( "table" ), Qt::CaseInsensitive ) )
3238  {
3240  def = def.mid( 6 );
3241  }
3242 
3243  return new QgsProcessingParameterFeatureSink( name, description, type, definition, isOptional );
3244 }
3245 
3247  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3248 {}
3249 
3251 {
3252  return new QgsProcessingParameterRasterDestination( *this );
3253 }
3254 
3256 {
3257  QVariant var = input;
3258  if ( !var.isValid() )
3259  return mFlags & FlagOptional;
3260 
3261  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3262  {
3264  var = fromVar.sink;
3265  }
3266 
3267  if ( var.canConvert<QgsProperty>() )
3268  {
3269  return true;
3270  }
3271 
3272  if ( var.type() != QVariant::String )
3273  return false;
3274 
3275  if ( var.toString().isEmpty() )
3276  return mFlags & FlagOptional;
3277 
3278  return true;
3279 }
3280 
3282 {
3283  if ( value.canConvert<QgsProperty>() )
3284  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3285 
3286  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3287  {
3289  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3290  {
3291  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3292  }
3293  else
3294  {
3295  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3296  }
3297  }
3298 
3299  return value.toString().prepend( '\'' ).append( '\'' );
3300 }
3301 
3303 {
3304  return new QgsProcessingOutputRasterLayer( name(), description() );
3305 }
3306 
3308 {
3309  if ( QgsProcessingProvider *p = provider() )
3310  {
3311  return p->defaultRasterFileExtension();
3312  }
3313  else
3314  {
3315  QgsSettings settings;
3316  return settings.value( QStringLiteral( "Processing/DefaultOutputRasterLayerExt" ), QStringLiteral( "tif" ), QgsSettings::Core ).toString();
3317  }
3318 }
3319 
3320 QgsProcessingParameterRasterDestination *QgsProcessingParameterRasterDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3321 {
3322  return new QgsProcessingParameterRasterDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
3323 }
3324 
3325 
3326 QgsProcessingParameterFileDestination::QgsProcessingParameterFileDestination( const QString &name, const QString &description, const QString &fileFilter, const QVariant &defaultValue, bool optional )
3327  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3328  , mFileFilter( fileFilter.isEmpty() ? QObject::tr( "All files (*.*)" ) : fileFilter )
3329 {
3330 
3331 }
3332 
3334 {
3335  return new QgsProcessingParameterFileDestination( *this );
3336 }
3337 
3339 {
3340  QVariant var = input;
3341  if ( !var.isValid() )
3342  return mFlags & FlagOptional;
3343 
3344  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3345  {
3347  var = fromVar.sink;
3348  }
3349 
3350  if ( var.canConvert<QgsProperty>() )
3351  {
3352  return true;
3353  }
3354 
3355  if ( var.type() != QVariant::String )
3356  return false;
3357 
3358  if ( var.toString().isEmpty() )
3359  return mFlags & FlagOptional;
3360 
3361  // possible enhancement - check that value is compatible with file filter?
3362 
3363  return true;
3364 }
3365 
3367 {
3368  if ( value.canConvert<QgsProperty>() )
3369  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3370 
3371  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3372  {
3374  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3375  {
3376  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3377  }
3378  else
3379  {
3380  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3381  }
3382  }
3383 
3384  return value.toString().prepend( '\'' ).append( '\'' );
3385 }
3386 
3388 {
3389  if ( !mFileFilter.isEmpty() && mFileFilter.contains( QStringLiteral( "htm" ), Qt::CaseInsensitive ) )
3390  {
3391  return new QgsProcessingOutputHtml( name(), description() );
3392  }
3393  else
3394  {
3395  return new QgsProcessingOutputFile( name(), description() );
3396  }
3397 }
3398 
3400 {
3401  if ( mFileFilter.isEmpty() || mFileFilter == QObject::tr( "All files (*.*)" ) )
3402  return QStringLiteral( "file" );
3403 
3404  // get first extension from filter
3405  QRegularExpression rx( QStringLiteral( ".*?\\(\\*\\.([a-zA-Z0-9._]+).*" ) );
3406  QRegularExpressionMatch match = rx.match( mFileFilter );
3407  if ( !match.hasMatch() )
3408  return QStringLiteral( "file" );
3409 
3410  return match.captured( 1 );
3411 }
3412 
3414 {
3415  return mFileFilter;
3416 }
3417 
3419 {
3420  mFileFilter = fileFilter;
3421 }
3422 
3424 {
3426  map.insert( QStringLiteral( "file_filter" ), mFileFilter );
3427  return map;
3428 }
3429 
3431 {
3433  mFileFilter = map.value( QStringLiteral( "file_filter" ) ).toString();
3434  return true;
3435 
3436 }
3437 
3438 QgsProcessingParameterFileDestination *QgsProcessingParameterFileDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3439 {
3440  return new QgsProcessingParameterFileDestination( name, description, QString(), definition.isEmpty() ? QVariant() : definition, isOptional );
3441 }
3442 
3444  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3445 {}
3446 
3448 {
3449  return new QgsProcessingParameterFolderDestination( *this );
3450 }
3451 
3453 {
3454  QVariant var = input;
3455  if ( !var.isValid() )
3456  return mFlags & FlagOptional;
3457 
3458  if ( var.canConvert<QgsProperty>() )
3459  {
3460  return true;
3461  }
3462 
3463  if ( var.type() != QVariant::String )
3464  return false;
3465 
3466  if ( var.toString().isEmpty() )
3467  return mFlags & FlagOptional;
3468 
3469  return true;
3470 }
3471 
3473 {
3474  return new QgsProcessingOutputFolder( name(), description() );
3475 }
3476 
3478 {
3479  return QString();
3480 }
3481 
3482 QgsProcessingParameterFolderDestination *QgsProcessingParameterFolderDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3483 {
3484  return new QgsProcessingParameterFolderDestination( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
3485 }
3486 
3487 QgsProcessingDestinationParameter::QgsProcessingDestinationParameter( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
3488  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3489 {
3490 
3491 }
3492 
3494 {
3496  map.insert( QStringLiteral( "supports_non_file_outputs" ), mSupportsNonFileBasedOutputs );
3497  map.insert( QStringLiteral( "create_by_default" ), mCreateByDefault );
3498  return map;
3499 }
3500 
3502 {
3504  mSupportsNonFileBasedOutputs = map.value( QStringLiteral( "supports_non_file_outputs" ) ).toBool();
3505  mCreateByDefault = map.value( QStringLiteral( "create_by_default" ), QStringLiteral( "1" ) ).toBool();
3506  return true;
3507 }
3508 
3510 {
3512 }
3513 
3515 {
3516  return mCreateByDefault;
3517 }
3518 
3520 {
3521  mCreateByDefault = createByDefault;
3522 }
3523 
3525  : QgsProcessingDestinationParameter( name, description, defaultValue, optional )
3526  , mDataType( type )
3527 {
3528 
3529 }
3530 
3532 {
3533  return new QgsProcessingParameterVectorDestination( *this );
3534 }
3535 
3537 {
3538  QVariant var = input;
3539  if ( !var.isValid() )
3540  return mFlags & FlagOptional;
3541 
3542  if ( var.canConvert<QgsProcessingOutputLayerDefinition>() )
3543  {
3545  var = fromVar.sink;
3546  }
3547 
3548  if ( var.canConvert<QgsProperty>() )
3549  {
3550  return true;
3551  }
3552 
3553  if ( var.type() != QVariant::String )
3554  return false;
3555 
3556  if ( var.toString().isEmpty() )
3557  return mFlags & FlagOptional;
3558 
3559  return true;
3560 }
3561 
3563 {
3564  if ( value.canConvert<QgsProperty>() )
3565  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3566 
3567  if ( value.canConvert<QgsProcessingOutputLayerDefinition>() )
3568  {
3570  if ( fromVar.sink.propertyType() == QgsProperty::StaticProperty )
3571  {
3572  return QStringLiteral( "'%1'" ).arg( fromVar.sink.staticValue().toString() );
3573  }
3574  else
3575  {
3576  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( fromVar.sink.asExpression() );
3577  }
3578  }
3579 
3580  return value.toString().prepend( '\'' ).append( '\'' );
3581 }
3582 
3584 {
3585  QString code = QStringLiteral( "##%1=" ).arg( mName );
3586  if ( mFlags & FlagOptional )
3587  code += QStringLiteral( "optional " );
3588  code += QStringLiteral( "vectorDestination " );
3589 
3590  switch ( mDataType )
3591  {
3593  code += QStringLiteral( "point " );
3594  break;
3595 
3597  code += QStringLiteral( "line " );
3598  break;
3599 
3601  code += QStringLiteral( "polygon " );
3602  break;
3603 
3604  default:
3605  break;
3606  }
3607 
3608  code += mDefault.toString();
3609  return code.trimmed();
3610 }
3611 
3613 {
3614  return new QgsProcessingOutputVectorLayer( name(), description(), mDataType );
3615 }
3616 
3618 {
3619  if ( QgsProcessingProvider *p = provider() )
3620  {
3621  return p->defaultVectorFileExtension( hasGeometry() );
3622  }
3623  else
3624  {
3625  QgsSettings settings;
3626  if ( hasGeometry() )
3627  {
3628  return settings.value( QStringLiteral( "Processing/DefaultOutputVectorLayerExt" ), QStringLiteral( "shp" ), QgsSettings::Core ).toString();
3629  }
3630  else
3631  {
3632  return QStringLiteral( "dbf" );
3633  }
3634  }
3635 }
3636 
3638 {
3639  return mDataType;
3640 }
3641 
3643 {
3644  switch ( mDataType )
3645  {
3652  return true;
3653 
3656  return false;
3657  }
3658  return true;
3659 }
3660 
3662 {
3663  mDataType = type;
3664 }
3665 
3667 {
3669  map.insert( QStringLiteral( "data_type" ), mDataType );
3670  return map;
3671 }
3672 
3674 {
3676  mDataType = static_cast< QgsProcessing::SourceType >( map.value( QStringLiteral( "data_type" ) ).toInt() );
3677  return true;
3678 }
3679 
3680 QgsProcessingParameterVectorDestination *QgsProcessingParameterVectorDestination::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3681 {
3683  QString def = definition;
3684  if ( def.startsWith( QStringLiteral( "point" ), Qt::CaseInsensitive ) )
3685  {
3687  def = def.mid( 6 );
3688  }
3689  else if ( def.startsWith( QStringLiteral( "line" ), Qt::CaseInsensitive ) )
3690  {
3692  def = def.mid( 5 );
3693  }
3694  else if ( def.startsWith( QStringLiteral( "polygon" ), Qt::CaseInsensitive ) )
3695  {
3697  def = def.mid( 8 );
3698  }
3699 
3700  return new QgsProcessingParameterVectorDestination( name, description, type, definition, isOptional );
3701 }
3702 
3703 QgsProcessingParameterBand::QgsProcessingParameterBand( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
3704  : QgsProcessingParameterDefinition( name, description, defaultValue, optional )
3705  , mParentLayerParameterName( parentLayerParameterName )
3706 {
3707 
3708 }
3709 
3711 {
3712  return new QgsProcessingParameterBand( *this );
3713 }
3714 
3716 {
3717  if ( !input.isValid() )
3718  return mFlags & FlagOptional;
3719 
3720  if ( input.canConvert<QgsProperty>() )
3721  {
3722  return true;
3723  }
3724 
3725  bool ok = false;
3726  double res = input.toInt( &ok );
3727  Q_UNUSED( res );
3728  if ( !ok )
3729  return mFlags & FlagOptional;
3730 
3731  return true;
3732 }
3733 
3735 {
3736  if ( value.canConvert<QgsProperty>() )
3737  return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
3738 
3739  return value.toString();
3740 }
3741 
3743 {
3744  QString code = QStringLiteral( "##%1=" ).arg( mName );
3745  if ( mFlags & FlagOptional )
3746  code += QStringLiteral( "optional " );
3747  code += QStringLiteral( "band " );
3748 
3749  code += mParentLayerParameterName + ' ';
3750 
3751  code += mDefault.toString();
3752  return code.trimmed();
3753 }
3754 
3756 {
3757  QStringList depends;
3758  if ( !mParentLayerParameterName.isEmpty() )
3759  depends << mParentLayerParameterName;
3760  return depends;
3761 }
3762 
3764 {
3765  return mParentLayerParameterName;
3766 }
3767 
3769 {
3770  mParentLayerParameterName = parentLayerParameterName;
3771 }
3772 
3774 {
3776  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
3777  return map;
3778 }
3779 
3780 bool QgsProcessingParameterBand::fromVariantMap( const QVariantMap &map )
3781 {
3783  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
3784  return true;
3785 }
3786 
3787 QgsProcessingParameterBand *QgsProcessingParameterBand::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
3788 {
3789  QString parent;
3790  QString def = definition;
3791 
3792  QRegularExpression re( QStringLiteral( "(.*?)\\s+(.*)$" ) );
3793  QRegularExpressionMatch m = re.match( def );
3794  if ( m.hasMatch() )
3795  {
3796  parent = m.captured( 1 ).trimmed();
3797  def = m.captured( 2 );
3798  }
3799  else
3800  {
3801  parent = def;
3802  def.clear();
3803  }
3804 
3805  return new QgsProcessingParameterBand( name, description, def.isEmpty() ? QVariant() : def, parent, isOptional );
3806 }
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.
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:39
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:56
static QString descriptionFromName(const QString &name)
Creates an autogenerated parameter description from a parameter name.
QgsProcessingDestinationParameter(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingDestinationParameter.
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.
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.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
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.
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:57
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.
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.
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...
QgsProcessingParameterFeatureSink(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFeatureSink.
QgsProcessingParameterRasterDestination(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterDestination.
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.
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, TransformDirection direction=ForwardTransform, const bool handle180Crossover=false) const
Transforms a rectangle from the source CRS to the destination CRS.
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 customise 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:111
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.
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.
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.
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...
QgsProcessingParameterNumber(const QString &name, const QString &description=QString(), Type type=Integer, const QVariant &defaultValue=QVariant(), bool optional=false, double minValue=-DBL_MAX+1, double maxValue=DBL_MAX)
Constructor for QgsProcessingParameterNumber.
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.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
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.
QgsProcessingParameterVectorDestination(const QString &name, const QString &description=QString(), QgsProcessing::SourceType type=QgsProcessing::TypeVectorAnyGeometry, const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterVectorDestination.
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:53
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.
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.
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
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.
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...
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.
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.
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
A file output for processing algorithms.
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:88
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.
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:82
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Vector polygon layers.
Definition: qgsprocessing.h:51
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.
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.
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.
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.
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...
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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.
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:130
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.
QgsProcessingParameterFileDestination(const QString &name, const QString &description=QString(), const QString &fileFilter=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterFileDestination.
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.
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:115
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...
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:49
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.
QgsPointXY transform(const QgsPointXY &point, TransformDirection direction=ForwardTransform) const
Transform the point from the source CRS to the destination CRS.
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
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:47
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:50
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
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:54
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:45
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
Details for layers to load into projects.
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:120
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:125
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.
An 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.
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.
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:48
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...
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.
QgsProcessingParameterRasterLayer(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false)
Constructor for QgsProcessingParameterRasterLayer.
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.
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 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.