QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsprocessingprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingprovider.cpp
3  --------------------------
4  begin : December 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsprocessingprovider.h"
19 #include "qgsapplication.h"
20 #include "qgsvectorfilewriter.h"
21 #include "qgsrasterfilewriter.h"
22 #include "qgssettings.h"
23 
25  : QObject( parent )
26 {}
27 
28 
30 {
31  qDeleteAll( mAlgorithms );
32 }
33 
35 {
36  return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
37 }
38 
40 {
41  return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
42 }
43 
44 QgsProcessingProvider::Flags QgsProcessingProvider::flags() const
45 {
46  return QgsProcessingProvider::Flags();
47 }
48 
50 {
51  return QString();
52 }
53 
55 {
56  return name();
57 }
58 
60 {
61  return QString();
62 }
63 
65 {
67 }
68 
70 {
71  return QStringList();
72 }
73 
75 {
76  qDeleteAll( mAlgorithms );
77  mAlgorithms.clear();
78  if ( isActive() )
79  {
81  emit algorithmsLoaded();
82  }
83 }
84 
85 QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
86 {
87  return mAlgorithms.values();
88 }
89 
90 const QgsProcessingAlgorithm *QgsProcessingProvider::algorithm( const QString &name ) const
91 {
92  return mAlgorithms.value( name );
93 }
94 
96 {
97  if ( !algorithm )
98  return false;
99 
100  if ( mAlgorithms.contains( algorithm->name() ) )
101  {
102  QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
103  return false;
104  }
105 
106  // init the algorithm - this allows direct querying of the algorithm's parameters
107  // and outputs from the provider's copy
108  algorithm->initAlgorithm( QVariantMap() );
109 
110  algorithm->setProvider( this );
111  mAlgorithms.insert( algorithm->name(), algorithm );
112  return true;
113 }
114 
116 {
118 }
119 
121 {
123 }
124 
125 bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
126 {
127  error.clear();
128  QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context ).trimmed();
129 
130  if ( outputPath.isEmpty() )
131  {
133  {
134  return true;
135  }
136  else
137  {
138  error = tr( "Missing parameter value %1" ).arg( parameter->description() );
139  return false;
140  }
141  }
142 
145  {
146  if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
147  {
149  {
150  error = tr( "This algorithm only supports disk-based outputs" );
151  return false;
152  }
153  return true;
154  }
155 
156  QString providerKey;
157  QString uri;
158  QString layerName;
159  QMap<QString, QVariant> options;
160  bool useWriter = false;
161  QString format;
162  QString extension;
163  QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
164 
165  if ( providerKey != QLatin1String( "ogr" ) )
166  {
168  {
169  error = tr( "This algorithm only supports disk-based outputs" );
170  return false;
171  }
172  return true;
173  }
174 
175  if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
176  {
177  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
178  return false;
179  }
180  return true;
181  }
182  else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
183  {
184  const QFileInfo fi( outputPath );
185  const QString extension = fi.completeSuffix();
186  if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
187  {
188  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
189  return false;
190  }
191  return true;
192  }
193  else if ( parameter->type() == QgsProcessingParameterPointCloudDestination::typeName() )
194  {
195  const QFileInfo fi( outputPath );
196  const QString extension = fi.completeSuffix();
197  if ( !supportedOutputPointCloudLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
198  {
199  error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
200  return false;
201  }
202  return true;
203  }
204  else
205  {
206  return true;
207  }
208 }
209 
210 QString QgsProcessingProvider::defaultVectorFileExtension( bool hasGeometry ) const
211 {
212  const QString userDefault = QgsProcessingUtils::defaultVectorExtension();
213 
214  const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
215  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
216  {
217  // user set default is supported by provider, use that
218  return userDefault;
219  }
220  else if ( !supportedExtensions.empty() )
221  {
222  return supportedExtensions.at( 0 );
223  }
224  else
225  {
226  // who knows? provider says it has no file support at all...
227  // let's say shp. even MapInfo supports shapefiles.
228  return hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
229  }
230 }
231 
233 {
234  const QString userDefault = QgsProcessingUtils::defaultRasterExtension();
235 
236  const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
237  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
238  {
239  // user set default is supported by provider, use that
240  return userDefault;
241  }
242  else if ( !supportedExtensions.empty() )
243  {
244  return supportedExtensions.at( 0 );
245  }
246  else
247  {
248  // who knows? provider says it has no file support at all...
249  return QStringLiteral( "tif" );
250  }
251 }
252 
254 {
255  const QString userDefault = QgsProcessingUtils::defaultPointCloudExtension();
256 
257  const QStringList supportedExtensions = supportedOutputPointCloudLayerExtensions();
258  if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
259  {
260  // user set default is supported by provider, use that
261  return userDefault;
262  }
263  else if ( !supportedExtensions.empty() )
264  {
265  return supportedExtensions.at( 0 );
266  }
267  else
268  {
269  // who knows? provider says it has no file support at all...
270  return QStringLiteral( "las" );
271  }
272 }
273 
275 {
276  return true;
277 }
QgsProcessingProvider::isSupportedOutputValue
virtual bool isSupportedOutputValue(const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error) const
Returns true if the specified outputValue is of a supported file format for the given destination par...
Definition: qgsprocessingprovider.cpp:125
QgsProcessingProvider::refreshAlgorithms
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
Definition: qgsprocessingprovider.cpp:74
QgsProcessingUtils::defaultVectorExtension
static QString defaultVectorExtension()
Returns the default vector extension to use, in the absence of all other constraints (e....
Definition: qgsprocessingutils.cpp:1305
QgsProcessingParameterRasterDestination::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparameters.h:3408
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:502
algorithm
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
QgsProcessingParameterDefinition::flags
Flags flags() const
Returns any flags associated with the parameter.
Definition: qgsprocessingparameters.h:594
QgsProcessingProvider::supportedOutputTableExtensions
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider.
Definition: qgsprocessingprovider.cpp:120
QgsProcessingParameterVectorDestination::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparameters.h:3333
QgsProcessingProvider::icon
virtual QIcon icon() const
Returns an icon for the provider.
Definition: qgsprocessingprovider.cpp:34
QgsProcessingProvider::algorithm
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
Definition: qgsprocessingprovider.cpp:90
QgsProcessingProvider::flags
virtual Flags flags() const
Returns the flags indicating how and when the provider operates and should be exposed to users.
Definition: qgsprocessingprovider.cpp:44
QgsApplication::iconPath
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Definition: qgsapplication.cpp:682
QgsProcessingProvider::versionInfo
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
Definition: qgsprocessingprovider.cpp:59
qgsrasterfilewriter.h
QgsProcessingProvider::supportedOutputPointCloudLayerExtensions
virtual QStringList supportedOutputPointCloudLayerExtensions() const
Returns a list of the point cloud format file extensions supported by this provider.
Definition: qgsprocessingprovider.cpp:69
QgsProcessingParameterFeatureSink::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparameters.h:3235
QgsProcessingDestinationParameter
Base class for all parameter definitions which represent file or layer destinations,...
Definition: qgsprocessingparameters.h:3097
qgsapplication.h
QgsProcessingProvider::longName
virtual QString longName() const
Returns the longer version of the provider name, which can include extra details such as version numb...
Definition: qgsprocessingprovider.cpp:54
QgsProcessingProvider::addAlgorithm
bool addAlgorithm(QgsProcessingAlgorithm *algorithm)
Adds an algorithm to the provider.
Definition: qgsprocessingprovider.cpp:95
QgsProcessingProvider::name
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
QgsProcessingProvider::supportedOutputVectorLayerExtensions
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
Definition: qgsprocessingprovider.cpp:115
QgsRasterFileWriter::supportedFormatExtensions
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
Definition: qgsrasterfilewriter.cpp:1206
QgsProcessingAlgorithm::name
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
QgsProcessingUtils::defaultPointCloudExtension
static QString defaultPointCloudExtension()
Returns the default point cloud extension to use, in the absence of all other constraints (e....
Definition: qgsprocessingutils.cpp:1321
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingProvider::supportedOutputRasterLayerExtensions
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
Definition: qgsprocessingprovider.cpp:64
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsVectorFileWriter::supportedFormatExtensions
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
Definition: qgsvectorfilewriter.cpp:3580
QgsProcessingProvider::svgIconPath
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider's icon.
Definition: qgsprocessingprovider.cpp:39
QgsProcessingProvider::loadAlgorithms
virtual void loadAlgorithms()=0
Loads all algorithms belonging to this provider.
QgsProcessingProvider::helpId
virtual QString helpId() const
Returns the provider help id string, used for creating QgsHelp urls for algorithms belong to this pro...
Definition: qgsprocessingprovider.cpp:49
QgsProcessingAlgorithm::setProvider
void setProvider(QgsProcessingProvider *provider)
Associates this algorithm with its provider.
Definition: qgsprocessingalgorithm.cpp:135
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsProcessingProvider::isActive
virtual bool isActive() const
Returns true if the provider is active and able to run algorithms.
Definition: qgsprocessingprovider.h:151
QgsProcessingParameters::parameterAsOutputLayer
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
Definition: qgsprocessingparameters.cpp:928
qgssettings.h
QgsProcessingProvider::defaultPointCloudFileExtension
virtual QString defaultPointCloudFileExtension() const
Returns the default file extension to use for point cloud outputs created by the provider.
Definition: qgsprocessingprovider.cpp:253
QgsProcessingProvider::~QgsProcessingProvider
~QgsProcessingProvider() override
Definition: qgsprocessingprovider.cpp:29
QgsProcessingProvider::algorithms
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of algorithms supplied by this provider.
Definition: qgsprocessingprovider.cpp:85
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsProcessingProvider::algorithmsLoaded
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
QgsProcessingParameterDefinition::type
virtual QString type() const =0
Unique parameter type name.
QgsProcessingProvider::QgsProcessingProvider
QgsProcessingProvider(QObject *parent=nullptr)
Constructor for QgsProcessingProvider.
Definition: qgsprocessingprovider.cpp:24
qgsvectorfilewriter.h
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:453
QgsProcessingProvider::defaultVectorFileExtension
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
Definition: qgsprocessingprovider.cpp:210
qgsprocessingprovider.h
QgsProcessingUtils::defaultRasterExtension
static QString defaultRasterExtension()
Returns the default raster extension to use, in the absence of all other constraints (e....
Definition: qgsprocessingutils.cpp:1313
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsProcessingProvider::supportsNonFileBasedOutput
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
Definition: qgsprocessingprovider.cpp:274
QgsProcessingParameterPointCloudDestination::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparameters.h:4361
QgsProcessingProvider::defaultRasterFileExtension
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
Definition: qgsprocessingprovider.cpp:232
QgsProcessingAlgorithm::initAlgorithm
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.