QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
qgsvectorlayerexporter.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerexporter.h
3 -------------------
4 begin : Thu Aug 25 2011
5 copyright : (C) 2011 by Giuseppe Sucameli
6 email : brush.tyler at gmail.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#ifndef QGSVECTORLAYEREXPORTER_H
19#define QGSVECTORLAYEREXPORTER_H
20
21#include "qgis.h"
22#include "qgis_core.h"
23#include "qgis_sip.h"
24#include "qgsfeature.h"
25#include "qgsfeaturesink.h"
26#include "qgsfeedback.h"
28#include "qgstaskmanager.h"
29#include "qgsvectorlayer.h"
30
31#include <QPointer>
32
33class QProgressDialog;
35class QgsFields;
36
49class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
50{
51 public:
52
53
68 const QString &uri,
69 const QString &providerKey,
70 const QgsCoordinateReferenceSystem &destCRS,
71 bool onlySelected = false,
72 QString *errorMessage SIP_OUT = nullptr,
73 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
74 QgsFeedback *feedback = nullptr );
75
76
83 struct CORE_EXPORT OutputField
84 {
85
89 OutputField( const QgsField &field, const QString &expression )
90 : field( field )
92 {}
93
97 QString expression;
98 };
99
106 class CORE_EXPORT ExportOptions
107 {
108 public:
109
119 void setSelectedOnly( bool selected ) { mSelectedOnly = selected; }
120
130 bool selectedOnly() const { return mSelectedOnly; }
131
137 void setTransformContext( const QgsCoordinateTransformContext &context );
138
144 QgsCoordinateTransformContext transformContext() const;
145
151 void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
152
158 QgsCoordinateReferenceSystem destinationCrs() const;
159
169 void setExtent( const QgsReferencedRectangle &extent );
170
180 QgsReferencedRectangle extent() const;
181
194 void setFilterExpression( const QString &expression );
195
208 QString filterExpression() const;
209
215 void setExpressionContext( const QgsExpressionContext &context );
216
222 const QgsExpressionContext &expressionContext() const;
223
233 QList<QgsVectorLayerExporter::OutputField> outputFields() const;
234
244 void setOutputFields( const QList<QgsVectorLayerExporter::OutputField> &fields );
245
246 private:
247
248 bool mSelectedOnly = false;
249
250 QgsCoordinateReferenceSystem mDestinationCrs;
251
252 QgsCoordinateTransformContext mTransformContext;
253
255
256 QString mFilterExpression;
257 QgsExpressionContext mExpressionContext;
258
259 QList< QgsVectorLayerExporter::OutputField > mOutputFields;
260
261 };
262
277 static Qgis::VectorExportResult exportLayer( QgsVectorLayer *layer,
278 const QString &uri,
279 const QString &providerKey,
280 const QgsVectorLayerExporter::ExportOptions &exportOptions,
281 QString *errorMessage SIP_OUT = nullptr,
282 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
283 QgsFeedback *feedback = nullptr );
284
297 QgsVectorLayerExporter( const QString &uri,
298 const QString &provider,
299 const QgsFields &fields,
300 Qgis::WkbType geometryType,
302 bool overwrite = false,
303 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
305
308
315
321 QString errorMessage() const;
322
328 int errorCount() const { return mErrorCount; }
329
335 Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const;
336
338 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
339 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
340 QString lastError() const override;
341
345 ~QgsVectorLayerExporter() override;
346
347 bool flushBuffer() override;
348
349 private:
350
352 bool createSpatialIndex();
353
356 QString mErrorMessage;
357
358 int mErrorCount = 0;
359
360 QgsVectorDataProvider *mProvider = nullptr;
361
363 QMap<int, int> mOldToNewAttrIdx;
364 int mAttributeCount = -1;
365
366 QgsFeatureList mFeatureBuffer;
367 int mFeatureBufferMemoryUsage = 0;
368
369 bool mCreateSpatialIndex = true;
370
371#ifdef SIP_RUN
373#endif
374
375};
376
377
389class CORE_EXPORT QgsVectorLayerExporterTask : public QgsTask
390{
391 Q_OBJECT
392
393 public:
394
402 const QString &uri,
403 const QString &providerKey,
404 const QgsCoordinateReferenceSystem &destinationCrs,
405 const QMap<QString, QVariant> &options = QMap<QString, QVariant>(),
406 bool ownsLayer = false );
407
418 const QString &uri,
419 const QString &providerKey,
420 const QgsVectorLayerExporter::ExportOptions &exportOptions,
421 const QMap<QString, QVariant> &providerOptions = QMap<QString, QVariant>(),
422 bool ownsLayer = false );
423
431 const QString &uri,
432 const QString &providerKey,
433 const QgsCoordinateReferenceSystem &destinationCrs,
434 const QMap<QString, QVariant> &options = QMap<QString, QVariant>() ) SIP_FACTORY;
435
436 void cancel() override;
437
438 signals:
439
444
449 void errorOccurred( Qgis::VectorExportResult error, const QString &errorMessage );
450
451 protected:
452
453 bool run() override;
454 void finished( bool result ) override;
455
456 private:
457
458 QPointer< QgsVectorLayer > mLayer = nullptr;
459 bool mOwnsLayer = false;
460
461 QString mDestUri;
462 QString mDestProviderKey;
464 QMap<QString, QVariant> mOptions;
465
466 std::unique_ptr< QgsFeedback > mOwnedFeedback;
467
469 QString mErrorMessage;
470
471};
472
473#endif // QGSVECTORLAYEREXPORTER_H
VectorExportResult
Vector layer export result codes.
Definition qgis.h:1052
@ Success
No errors were encountered.
Definition qgis.h:1053
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
Definition qgis.h:602
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An interface for objects which accept features via addFeature(s) methods.
QFlags< SinkFlag > SinkFlags
virtual bool flushBuffer()
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
virtual bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())
Adds a single feature to the sink.
virtual QString lastError() const
Returns the most recent error encountered by the sink, e.g.
virtual bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags())=0
Adds a list of features to the sink.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
A QgsRectangle with associated coordinate reference system.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
Base class for vector data providers.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
QgsVectorLayerExporterTask(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), bool ownsLayer=false)
Constructor for QgsVectorLayerExporterTask.
void exportComplete()
Emitted when exporting the layer is successfully completed.
void errorOccurred(Qgis::VectorExportResult error, const QString &errorMessage)
Emitted when an error occurs which prevented the layer being exported (or if the task is canceled).
void cancel() override
Notifies the task that it should terminate.
static QgsVectorLayerExporterTask * withLayerOwnership(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destinationCrs, const QMap< QString, QVariant > &options=QMap< QString, QVariant >())
Creates a new QgsVectorLayerExporterTask which has ownership over a source layer.
bool run() override
Performs the task's operation.
Encapsulates options for use with QgsVectorLayerExporter.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
bool selectedOnly() const
Returns whether the export will only include selected features.
A convenience class for exporting vector layers to a destination data provider.
QgsVectorLayerExporter & operator=(const QgsVectorLayerExporter &rh)=delete
static Qgis::VectorExportResult exportLayer(QgsVectorLayer *layer, const QString &uri, const QString &providerKey, const QgsCoordinateReferenceSystem &destCRS, bool onlySelected=false, QString *errorMessage=nullptr, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeedback *feedback=nullptr)
Writes the contents of vector layer to a different datasource.
int errorCount() const
Returns the number of error messages encountered during the export.
QString errorMessage() const
Returns any error message encountered during the export.
QgsVectorLayerExporter(const QgsVectorLayerExporter &rh)=delete
Qgis::VectorExportResult errorCode() const
Returns any encountered error code, or false if no error was encountered.
QgsVectorLayerExporter(const QString &uri, const QString &provider, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, bool overwrite=false, const QMap< QString, QVariant > &options=QMap< QString, QVariant >(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags())
Constructor for QgsVectorLayerExporter.
Represents a vector layer which manages a vector based dataset.
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_FACTORY
Definition qgis_sip.h:84
QList< QgsFeature > QgsFeatureList
QString expression
The expression for the exported field from the source fields.
OutputField(const QgsField &field, const QString &expression)
Constructor for OutputField, with the specified field definition and source expression.
QgsField field
Destination field definition.