QGIS API Documentation 3.41.0-Master (88383c3d16f)
Loading...
Searching...
No Matches
qgsalgorithmexporttospreadsheet.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmexporttospreadsheet.cpp
3 ------------------
4 begin : December 2020
5 copyright : (C) 2020 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 "qgsogrutils.h"
20#include "qgsvectorfilewriter.h"
21#include "qgsvectorlayer.h"
22#include "qgsapplication.h"
24#include "qgsfieldformatter.h"
25
27
28class FieldValueConverter : public QgsVectorFileWriter::FieldValueConverter
29{
30 public:
32 : mLayer( vl )
33 {
34 const QStringList formattersAllowList { QStringLiteral( "KeyValue" ), QStringLiteral( "List" ), QStringLiteral( "ValueRelation" ), QStringLiteral( "ValueMap" ) };
35
36 for ( int i = 0; i < mLayer->fields().count(); ++i )
37 {
38 const QgsEditorWidgetSetup setup = mLayer->fields().at( i ).editorWidgetSetup();
40 if ( formattersAllowList.contains( fieldFormatter->id() ) )
41 {
42 mFormatters[i] = fieldFormatter;
43 mConfig[i] = setup.config();
44 }
45 }
46 }
47
48 QgsField fieldDefinition( const QgsField &field ) override
49 {
50 if ( !mLayer )
51 return field;
52
53 const int idx = mLayer->fields().indexFromName( field.name() );
54 if ( mFormatters.contains( idx ) )
55 {
56 QgsField newField( field.name(), QMetaType::Type::QString );
57 newField.setAlias( field.alias() );
58 return newField;
59 }
60 return field;
61 }
62
63 QVariant convert( int i, const QVariant &value ) override
64 {
65 const QgsFieldFormatter *formatter = mFormatters.value( i );
66 if ( !formatter )
67 return value;
68
69 QVariant cache;
70 if ( mCaches.contains( i ) )
71 {
72 cache = mCaches.value( i );
73 }
74 else
75 {
76 cache = formatter->createCache( mLayer.data(), i, mConfig.value( i ) );
77 mCaches[i] = cache;
78 }
79
80 return formatter->representValue( mLayer.data(), i, mConfig.value( i ), cache, value );
81 }
82
83 FieldValueConverter *clone() const override
84 {
85 return new FieldValueConverter( *this );
86 }
87
88 private:
89 QPointer<QgsVectorLayer> mLayer;
90 QMap<int, const QgsFieldFormatter *> mFormatters;
91 QMap<int, QVariantMap> mConfig;
92 QMap<int, QVariant> mCaches;
93};
94
95QString QgsExportToSpreadsheetAlgorithm::name() const
96{
97 return QStringLiteral( "exporttospreadsheet" );
98}
99
100QString QgsExportToSpreadsheetAlgorithm::displayName() const
101{
102 return QObject::tr( "Export to spreadsheet" );
103}
104
105QStringList QgsExportToSpreadsheetAlgorithm::tags() const
106{
107 return QObject::tr( "microsoft,excel,xls,xlsx,calc,open,office,libre,ods" ).split( ',' );
108}
109
110QString QgsExportToSpreadsheetAlgorithm::group() const
111{
112 return QObject::tr( "Layer tools" );
113}
114
115QString QgsExportToSpreadsheetAlgorithm::groupId() const
116{
117 return QStringLiteral( "layertools" );
118}
119
120void QgsExportToSpreadsheetAlgorithm::initAlgorithm( const QVariantMap & )
121{
122 addParameter( new QgsProcessingParameterMultipleLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ), Qgis::ProcessingSourceType::Vector ) );
123 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "USE_ALIAS" ), QObject::tr( "Use field aliases as column headings" ), false ) );
124 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "FORMATTED_VALUES" ), QObject::tr( "Export formatted values instead of raw values" ), false ) );
125 QgsProcessingParameterFileDestination *outputParameter = new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Destination spreadsheet" ), QObject::tr( "Microsoft Excel (*.xlsx);;Open Document Spreadsheet (*.ods)" ) );
126 outputParameter->setMetadata( QVariantMap( { { QStringLiteral( "widget_wrapper" ), QVariantMap( { { QStringLiteral( "dontconfirmoverwrite" ), true } } ) } } ) );
127 addParameter( outputParameter );
128 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "OVERWRITE" ), QObject::tr( "Overwrite existing spreadsheet" ), true ) );
129 addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Layers within spreadsheet" ) ) );
130}
131
132QString QgsExportToSpreadsheetAlgorithm::shortHelpString() const
133{
134 return QObject::tr( "This algorithm collects a number of existing layers and exports them into a spreadsheet document.\n\n"
135 "Optionally the layers can be appended to an existing spreadsheet as additional sheets.\n\n" );
136}
137
138QgsExportToSpreadsheetAlgorithm *QgsExportToSpreadsheetAlgorithm::createInstance() const
139{
140 return new QgsExportToSpreadsheetAlgorithm();
141}
142
143bool QgsExportToSpreadsheetAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
144{
145 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
146 for ( QgsMapLayer *layer : layers )
147 {
148 mLayers.emplace_back( layer->clone() );
149 }
150
151 if ( mLayers.empty() )
152 feedback->reportError( QObject::tr( "No layers selected, spreadsheet will be empty" ), false );
153
154 return true;
155}
156
157QVariantMap QgsExportToSpreadsheetAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
158{
159 const bool overwrite = parameterAsBoolean( parameters, QStringLiteral( "OVERWRITE" ), context );
160 const QString outputPath = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context );
161 if ( outputPath.isEmpty() )
162 throw QgsProcessingException( QObject::tr( "No output file specified." ) );
163
164 const bool useAlias = parameterAsBoolean( parameters, QStringLiteral( "USE_ALIAS" ), context );
165 const bool formattedValues = parameterAsBoolean( parameters, QStringLiteral( "FORMATTED_VALUES" ), context );
166 bool createNew = true;
167 // delete existing spreadsheet if it exists
168 if ( overwrite && QFile::exists( outputPath ) )
169 {
170 feedback->pushInfo( QObject::tr( "Removing existing file '%1'" ).arg( outputPath ) );
171 if ( !QFile( outputPath ).remove() )
172 {
173 throw QgsProcessingException( QObject::tr( "Could not remove existing file '%1'" ).arg( outputPath ) );
174 }
175 }
176 else if ( QFile::exists( outputPath ) )
177 {
178 createNew = false;
179 }
180
181 const QFileInfo fi( outputPath );
182 const QString driverName = QgsVectorFileWriter::driverForExtension( fi.suffix() );
183
184 OGRSFDriverH hDriver = OGRGetDriverByName( driverName.toLocal8Bit().constData() );
185 if ( !hDriver )
186 {
187 if ( driverName == QLatin1String( "ods" ) )
188 throw QgsProcessingException( QObject::tr( "Open Document Spreadsheet driver not found." ) );
189 else
190 throw QgsProcessingException( QObject::tr( "Microsoft Excel driver not found." ) );
191 }
192
194#if 0
195 if ( !QFile::exists( outputPath ) )
196 {
197 hDS = gdal::ogr_datasource_unique_ptr( OGR_Dr_CreateDataSource( hDriver, outputPath.toUtf8().constData(), nullptr ) );
198 if ( !hDS )
199 throw QgsProcessingException( QObject::tr( "Creation of spreadsheet %1 failed (OGR error: %2)" ).arg( outputPath, QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
200 }
201#endif
202 bool errored = false;
203
204 QgsProcessingMultiStepFeedback multiStepFeedback( mLayers.size(), feedback );
205
206 QStringList outputLayers;
207 int i = 0;
208 for ( const auto &layer : mLayers )
209 {
210 if ( feedback->isCanceled() )
211 break;
212
213 multiStepFeedback.setCurrentStep( i );
214 i++;
215
216 if ( !layer )
217 {
218 // don't throw immediately - instead do what we can and error out later
219 feedback->pushDebugInfo( QObject::tr( "Error retrieving map layer." ) );
220 errored = true;
221 continue;
222 }
223
224 feedback->pushInfo( QObject::tr( "Exporting layer %1/%2: %3" ).arg( i ).arg( mLayers.size() ).arg( layer ? layer->name() : QString() ) );
225
226 FieldValueConverter converter( qobject_cast<QgsVectorLayer *>( layer.get() ) );
227
228 if ( !exportVectorLayer( qobject_cast<QgsVectorLayer *>( layer.get() ), outputPath, context, &multiStepFeedback, driverName, createNew, useAlias, formattedValues ? &converter : nullptr ) )
229 errored = true;
230 else
231 {
232 outputLayers.append( QStringLiteral( "%1|layername=%2" ).arg( outputPath, layer->name() ) );
233 createNew = false;
234 }
235 }
236
237 if ( errored )
238 throw QgsProcessingException( QObject::tr( "Error obtained while exporting one or more layers." ) );
239
240 QVariantMap outputs;
241 outputs.insert( QStringLiteral( "OUTPUT" ), outputPath );
242 outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers );
243 return outputs;
244}
245
246bool QgsExportToSpreadsheetAlgorithm::exportVectorLayer( QgsVectorLayer *layer, const QString &path, QgsProcessingContext &context, QgsProcessingFeedback *feedback, const QString &driverName, bool createNew, bool preferAlias, QgsVectorFileWriter::FieldValueConverter *converter )
247{
249 options.driverName = driverName;
250 options.layerName = layer->name();
252 options.fileEncoding = context.defaultEncoding();
253 options.feedback = feedback;
255 options.fieldValueConverter = converter;
256
257
258 QString error;
259 QString newFilename;
260 QString newLayer;
261 if ( QgsVectorFileWriter::writeAsVectorFormatV3( layer, path, context.transformContext(), options, &error, &newFilename, &newLayer ) != QgsVectorFileWriter::NoError )
262 {
263 feedback->reportError( QObject::tr( "Exporting layer failed: %1" ).arg( error ) );
264 return false;
265 }
266 else
267 {
268 return true;
269 }
270}
271
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
static QgsFieldFormatterRegistry * fieldFormatterRegistry()
Gets the registry of available field formatters.
Holder for the widget type and its configuration for a field.
QVariantMap config() const
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
QgsFieldFormatter * fieldFormatter(const QString &id) const
Gets a field formatter by its id.
A field formatter helps to handle and display values for a field.
virtual QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Create a cache for a given field.
virtual QString id() const =0
Returns a unique id for this field formatter.
virtual QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const
Create a pretty String representation of the value.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString name
Definition qgsfield.h:62
QString alias
Definition qgsfield.h:63
Base class for all map layer types.
Definition qgsmaplayer.h:76
QString name
Definition qgsmaplayer.h:80
Contains information about the context in which a processing algorithm is executed.
QString defaultEncoding() const
Returns the default encoding to use for newly created files.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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 pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Processing feedback object for multi-step operations.
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
A boolean parameter for processing algorithms.
void setMetadata(const QVariantMap &metadata)
Sets the parameter's freeform metadata.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A parameter for processing algorithms which accepts multiple map layers.
Interface to convert raw field values to their user-friendly value.
virtual QVariant convert(int fieldIdxInLayer, const QVariant &value)
Convert the provided value, for field fieldIdxInLayer.
virtual QgsVectorFileWriter::FieldValueConverter * clone() const
Creates a clone of the FieldValueConverter.
virtual QgsField fieldDefinition(const QgsField &field)
Returns a possibly modified field definition.
Options to pass to writeAsVectorFormat()
FieldNameSource fieldNameSource
Source for exported field names.
QString layerName
Layer name. If let empty, it will be derived from the filename.
QgsVectorFileWriter::FieldValueConverter * fieldValueConverter
Field value converter.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
QgsFeedback * feedback
Optional feedback object allowing cancellation of layer save.
static QgsVectorFileWriter::WriterError writeAsVectorFormatV3(QgsVectorLayer *layer, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *errorMessage=nullptr, QString *newFilename=nullptr, QString *newLayer=nullptr)
Writes a layer out to a vector file.
static QString driverForExtension(const QString &extension)
Returns the OGR driver name for a specified file extension.
@ PreferAlias
Use the field alias as the exported field name, wherever one is set. Otherwise use the original field...
@ Original
Use original field names.
@ CreateOrOverwriteLayer
Create or overwrite layer.
@ CreateOrOverwriteFile
Create or overwrite file.
Represents a vector layer which manages a vector based data sets.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.