QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
19
20#include "qgsapplication.h"
21#include "qgsmessagelog.h"
22#include "qgsrasterfilewriter.h"
23#include "qgsvectorfilewriter.h"
24
25#include <QRegularExpressionMatch>
26#include <QString>
27
28#include "moc_qgsprocessingprovider.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : QObject( parent )
34{}
35
36
38{
39 qDeleteAll( mAlgorithms );
40}
41
43{
44 return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
45}
46
48{
49 return QgsApplication::iconPath( u"processingAlgorithm.svg"_s );
50}
51
56
58{
59 return QString();
60}
61
63{
64 return name();
65}
66
68{
69 return QString();
70}
71
73{
74 const QList<QPair<QString, QString>> formatAndExtensions = supportedOutputRasterLayerFormatAndExtensions();
75 QSet< QString > extensions;
76 QStringList res;
77 for ( const QPair<QString, QString> &formatAndExt : std::as_const( formatAndExtensions ) )
78 {
79 if ( !extensions.contains( formatAndExt.second ) )
80 {
81 extensions.insert( formatAndExt.second );
82 res << formatAndExt.second;
83 }
84 }
85 return res;
86}
87
92
94{
96 QList<QPair<QString, QString>> res;
97
98 const thread_local QRegularExpression rx( u"\\*\\.([a-zA-Z0-9]*)"_s );
99
100 for ( const QgsRasterFileWriter::FilterFormatDetails &format : formats )
101 {
102 const QString ext = format.filterString;
103 const QRegularExpressionMatch match = rx.match( ext );
104 if ( !match.hasMatch() )
105 continue;
106
107 const QString matched = match.captured( 1 );
108 res << QPair<QString, QString>( format.driverName, matched );
109 }
110
111 std::sort( res.begin(), res.end(), []( const QPair<QString, QString> &a, const QPair<QString, QString> &b ) -> bool
112 {
113 for ( const QString &tifExt : { u"tif"_s, u"tiff"_s } )
114 {
115 if ( a.second == tifExt )
116 {
117 if ( b.second == a.second )
118 {
119 if ( a.first == "GTiff"_L1 )
120 return true;
121 else if ( b.first == "GTiff"_L1 )
122 return false;
123 return a.first.toLower().localeAwareCompare( b.first.toLower() ) < 0;
124 }
125 return true;
126 }
127 else if ( b.second == tifExt )
128 return false;
129 }
130
131 if ( a.second == "gpkg"_L1 )
132 {
133 if ( b.second == a.second )
134 return a.first.toLower().localeAwareCompare( b.first.toLower() ) < 0;
135 return true;
136 }
137 else if ( b.second == "gpkg"_L1 )
138 return false;
139
140 return a.second.toLower().localeAwareCompare( b.second.toLower() ) < 0;
141 } );
142
143 return res;
144}
145
147{
148 return QStringList();
149}
150
155
157{
158 qDeleteAll( mAlgorithms );
159 mAlgorithms.clear();
160 if ( isActive() )
161 {
163 emit algorithmsLoaded();
164 }
165}
166
167QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
168{
169 return mAlgorithms.values();
170}
171
173{
174 return mAlgorithms.value( name );
175}
176
178{
179 if ( !algorithm )
180 return false;
181
182 if ( mAlgorithms.contains( algorithm->name() ) )
183 {
184 QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
185 return false;
186 }
187
188 // init the algorithm - this allows direct querying of the algorithm's parameters
189 // and outputs from the provider's copy
190 algorithm->initAlgorithm( QVariantMap() );
191
192 algorithm->setProvider( this );
193 mAlgorithms.insert( algorithm->name(), algorithm );
194 return true;
195}
196
201
206
207bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
208{
209 error.clear();
210 QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context, true ).trimmed();
211
212 if ( outputPath.isEmpty() )
213 {
215 {
216 return true;
217 }
218 else
219 {
220 error = tr( "Missing parameter value %1" ).arg( parameter->description() );
221 return false;
222 }
223 }
224
227 {
228 if ( outputPath.startsWith( "memory:"_L1 ) )
229 {
231 {
232 error = tr( "This algorithm only supports disk-based outputs" );
233 return false;
234 }
235 return true;
236 }
237
238 QString providerKey;
239 QString uri;
240 QString layerName;
241 QMap<QString, QVariant> options;
242 bool useWriter = false;
243 QString format;
244 QString extension;
245 QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
246
247 if ( providerKey != "ogr"_L1 )
248 {
250 {
251 error = tr( "This algorithm only supports disk-based outputs" );
252 return false;
253 }
254 return true;
255 }
256
257 if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
258 {
259 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
260 return false;
261 }
262 return true;
263 }
264 else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
265 {
266 const QFileInfo fi( outputPath );
267 const QString extension = fi.suffix();
268 if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
269 {
270 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
271 return false;
272 }
273 return true;
274 }
276 {
277 const QFileInfo fi( outputPath );
278 const QString extension = fi.completeSuffix();
279 if ( !supportedOutputPointCloudLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
280 {
281 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
282 return false;
283 }
284 return true;
285 }
287 {
288 const QFileInfo fi( outputPath );
289 const QString extension = fi.completeSuffix();
290 if ( !supportedOutputVectorTileLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
291 {
292 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
293 return false;
294 }
295 return true;
296 }
297 else
298 {
299 return true;
300 }
301}
302
304{
305 const QString userDefault = QgsProcessingUtils::defaultVectorExtension();
306
307 const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
308 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
309 {
310 // user set default is supported by provider, use that
311 return userDefault;
312 }
313 else if ( !supportedExtensions.empty() )
314 {
315 return supportedExtensions.at( 0 );
316 }
317 else
318 {
319 // who knows? provider says it has no file support at all...
320 // let's say shp. even MapInfo supports shapefiles.
321 return hasGeometry ? u"shp"_s : u"dbf"_s;
322 }
323}
324
326{
327 const QString userDefault = QgsProcessingUtils::defaultRasterFormat();
328
329 const QList<QPair<QString, QString>> formatAndExtensions = supportedOutputRasterLayerFormatAndExtensions();
330 for ( const QPair<QString, QString> &formatAndExt : std::as_const( formatAndExtensions ) )
331 {
332 if ( formatAndExt.first.compare( userDefault, Qt::CaseInsensitive ) == 0 )
333 {
334 // user set default is supported by provider, use that
335 return userDefault;
336 }
337 }
338
339 if ( !formatAndExtensions.empty() )
340 {
341 return formatAndExtensions.at( 0 ).first;
342 }
343 else
344 {
345 // who knows? provider says it has no file support at all...
346 return u"GTiff"_s;
347 }
348}
349
351{
352 QString format = defaultRasterFileFormat();
353 QStringList extensions = QgsRasterFileWriter::extensionsForFormat( format );
354 if ( !extensions.isEmpty() )
355 return extensions[0];
356
357 return u"tif"_s;
358}
359
361{
362 const QString userDefault = QgsProcessingUtils::defaultPointCloudExtension();
363
364 const QStringList supportedExtensions = supportedOutputPointCloudLayerExtensions();
365 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
366 {
367 // user set default is supported by provider, use that
368 return userDefault;
369 }
370 else if ( !supportedExtensions.empty() )
371 {
372 return supportedExtensions.at( 0 );
373 }
374 else
375 {
376 // who knows? provider says it has no file support at all...
377 return u"las"_s;
378 }
379}
380
382{
383 const QString userDefault = QgsProcessingUtils::defaultVectorTileExtension();
384
385 const QStringList supportedExtensions = supportedOutputVectorTileLayerExtensions();
386 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
387 {
388 // user set default is supported by provider, use that
389 return userDefault;
390 }
391 else if ( !supportedExtensions.empty() )
392 {
393 return supportedExtensions.at( 0 );
394 }
395 else
396 {
397 // who knows? provider says it has no file support at all...
398 return u"mbtiles"_s;
399 }
400}
401
403{
404 return true;
405}
QFlags< ProcessingProviderFlag > ProcessingProviderFlags
Flags indicating how and when an processing provider operates and should be exposed to users.
Definition qgis.h:3630
@ Optional
Parameter is optional.
Definition qgis.h:3824
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
Base class for all parameter definitions which represent file or layer destinations,...
QString description() const
Returns the description for the parameter.
virtual QString type() const =0
Unique parameter type name.
Qgis::ProcessingParameterFlags flags() const
Returns any flags associated with the parameter.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
virtual QList< QPair< QString, QString > > supportedOutputRasterLayerFormatAndExtensions() const
Returns a list of (format, file extension) supported by this provider.
virtual QIcon icon() const
Returns an icon for the provider.
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
virtual QString helpId() const
Returns the provider help id string, used for creating QgsHelp urls for algorithms belong to this pro...
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...
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider.
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
virtual Qgis::ProcessingProviderFlags flags() const
Returns the flags indicating how and when the provider operates and should be exposed to users.
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
virtual QString defaultVectorTileFileExtension() const
Returns the default file extension to use for vector tile outputs created by the provider.
QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
virtual QStringList supportedOutputVectorTileLayerExtensions() const
Returns a list of the vector tile format file extensions supported by this provider.
QgsProcessingProvider(QObject *parent=nullptr)
Constructor for QgsProcessingProvider.
virtual bool isActive() const
Returns true if the provider is active and able to run algorithms.
virtual QStringList supportedOutputPointCloudLayerExtensions() const
Returns a list of the point cloud format file extensions supported by this provider.
QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
virtual QString defaultRasterFileFormat() const
Returns the default file format to use for raster outputs created by the provider.
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider's icon.
static QList< QPair< QString, QString > > supportedOutputRasterLayerFormatAndExtensionsDefault()
Returns a list of (format, file extension) supported by GDAL.
virtual void loadAlgorithms()=0
Loads all algorithms belonging to this provider.
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
virtual QString longName() const
Returns the longer version of the provider name, which can include extra details such as version numb...
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
virtual QString defaultPointCloudFileExtension() const
Returns the default file extension to use for point cloud outputs created by the provider.
bool addAlgorithm(QgsProcessingAlgorithm *algorithm)
Adds an algorithm to the provider.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of algorithms supplied by this provider.
static QString defaultVectorExtension()
Returns the default vector extension to use, in the absence of all other constraints (e....
static QString defaultRasterFormat()
Returns the default raster format to use, in the absence of all other constraints (e....
static QString defaultVectorTileExtension()
Returns the default vector tile extension to use, in the absence of all other constraints (e....
static QString defaultPointCloudExtension()
Returns the default point cloud extension to use, in the absence of all other constraints (e....
static QStringList extensionsForFormat(const QString &format)
Returns a list of known file extensions for the given GDAL driver format.
static QList< QgsRasterFileWriter::FilterFormatDetails > supportedFiltersAndFormats(RasterFormatOptions options=SortRecommended)
Returns a list or pairs, with format filter string as first element and GDAL format key as second ele...
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
Details of available filters and formats.