QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsalgorithmextractbinary.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractbinary.cpp
3 -----------------------------------
4 begin : November 2018
5 copyright : (C) 2018 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 "qgsfeaturerequest.h"
21
23
24QString QgsExtractBinaryFieldAlgorithm::name() const
25{
26 return QStringLiteral( "extractbinary" );
27}
28
29QString QgsExtractBinaryFieldAlgorithm::displayName() const
30{
31 return QObject::tr( "Extract binary field" );
32}
33
34QString QgsExtractBinaryFieldAlgorithm::shortHelpString() const
35{
36 return QObject::tr( "This algorithm extracts contents from a binary field, saving them to individual files.\n\n"
37 "Filenames can be generated using values taken from "
38 "an attribute in the source table or based on a more complex expression." );
39}
40
41QString QgsExtractBinaryFieldAlgorithm::shortDescription() const
42{
43 return QObject::tr( "Extracts contents from a binary field, saving them to individual files." );
44}
45
46QStringList QgsExtractBinaryFieldAlgorithm::tags() const
47{
48 return QObject::tr( "blob,binaries,save,file,contents,field,column" ).split( ',' );
49}
50
51QString QgsExtractBinaryFieldAlgorithm::group() const
52{
53 return QObject::tr( "Vector table" );
54}
55
56QString QgsExtractBinaryFieldAlgorithm::groupId() const
57{
58 return QStringLiteral( "vectortable" );
59}
60
61QgsExtractBinaryFieldAlgorithm *QgsExtractBinaryFieldAlgorithm::createInstance() const
62{
63 return new QgsExtractBinaryFieldAlgorithm();
64}
65
66void QgsExtractBinaryFieldAlgorithm::initAlgorithm( const QVariantMap & )
67{
68 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
69
70 addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Binary field" ), QVariant(), QStringLiteral( "INPUT" ), Qgis::ProcessingFieldParameterDataType::Any ) );
71
72 addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FILENAME" ), QObject::tr( "File name" ), QVariant(), QStringLiteral( "INPUT" ) ) );
73
74 addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "FOLDER" ), QObject::tr( "Destination folder" ) ) );
75}
76
77QVariantMap QgsExtractBinaryFieldAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
78{
79 std::unique_ptr<QgsProcessingFeatureSource> input( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
80 if ( !input )
81 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
82
83 const QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
84 const int fieldIndex = input->fields().lookupField( fieldName );
85 if ( fieldIndex < 0 )
86 throw QgsProcessingException( QObject::tr( "Invalid binary field" ) );
87
88 const QString folder = parameterAsString( parameters, QStringLiteral( "FOLDER" ), context );
89 if ( !QDir().mkpath( folder ) )
90 throw QgsProcessingException( QObject::tr( "Failed to create output directory." ) );
91
92 const QDir dir( folder );
93 const QString filenameExpressionString = parameterAsString( parameters, QStringLiteral( "FILENAME" ), context );
94 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, input.get() );
95
96 QSet<QString> fields;
97 fields.insert( fieldName );
98 QgsFeatureRequest request;
99
100 QgsExpression filenameExpression( filenameExpressionString );
101 filenameExpression.prepare( &expressionContext );
102 fields.unite( filenameExpression.referencedColumns() );
103 request.setSubsetOfAttributes( fields, input->fields() );
104 if ( !filenameExpression.needsGeometry() )
106
108 const double step = input->featureCount() > 0 ? 100.0 / input->featureCount() : 1;
109 int i = 0;
110 QgsFeature feat;
111 while ( features.nextFeature( feat ) )
112 {
113 i++;
114 if ( feedback->isCanceled() )
115 {
116 break;
117 }
118
119 feedback->setProgress( i * step );
120
121 const QByteArray ba = feat.attribute( fieldIndex ).toByteArray();
122 if ( ba.isEmpty() )
123 continue;
124
125 expressionContext.setFeature( feat );
126 const QString name = filenameExpression.evaluate( &expressionContext ).toString();
127 if ( filenameExpression.hasEvalError() )
128 {
129 feedback->reportError( QObject::tr( "Error evaluating filename: %1" ).arg( filenameExpression.evalErrorString() ) );
130 continue;
131 }
132
133 const QString path = dir.filePath( name );
134 QFile file( path );
135 if ( !file.open( QIODevice::WriteOnly | QFile::Truncate ) )
136 {
137 feedback->reportError( QObject::tr( "Could not open %1 for writing" ).arg( path ) );
138 }
139 else
140 {
141 file.write( ba );
142 file.close();
143 feedback->pushInfo( QObject::tr( "Extracted %1" ).arg( path ) );
144 }
145 }
146
147 QVariantMap outputs;
148 outputs.insert( QStringLiteral( "FOLDER" ), folder );
149 return outputs;
150}
151
152
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2196
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Handles parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
An expression parameter for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...