36#include <QProgressDialog>
40#include "moc_qgsvectorlayerexporter.cpp"
42using namespace Qt::StringLiterals;
50 QMap<int, int> *oldToNewAttrIdx,
51 QString *errorMessage,
52 const QMap<QString, QVariant> *options
62 mTransformContext = context;
67 return mTransformContext;
72 mDestinationCrs = crs;
77 return mDestinationCrs;
92 mFilterExpression = expression;
97 return mFilterExpression;
102 mExpressionContext = context;
107 return mExpressionContext;
112 return mOutputFields;
117 mOutputFields = fields;
127 const QString &providerKey,
132 const QMap<QString, QVariant> &options,
136 QMap<QString, QVariant> modifiedOptions( options );
138 if ( providerKey ==
"ogr"_L1
139 && options.contains( u
"driverName"_s )
140 && ( options[u
"driverName"_s].toString().compare(
"GPKG"_L1, Qt::CaseInsensitive ) == 0 || options[u
"driverName"_s].toString().compare(
"SQLite"_L1, Qt::CaseInsensitive ) == 0 ) )
147 QStringList modifiedLayerOptions;
148 if ( options.contains( u
"layerOptions"_s ) )
150 const QStringList layerOptions = options.value( u
"layerOptions"_s ).toStringList();
151 for (
const QString &layerOption : layerOptions )
153 if ( layerOption.compare(
"SPATIAL_INDEX=YES"_L1, Qt::CaseInsensitive ) == 0
154 || layerOption.compare(
"SPATIAL_INDEX=ON"_L1, Qt::CaseInsensitive ) == 0
155 || layerOption.compare(
"SPATIAL_INDEX=TRUE"_L1, Qt::CaseInsensitive ) == 0
156 || layerOption.compare(
"SPATIAL_INDEX=1"_L1, Qt::CaseInsensitive ) == 0 )
160 else if ( layerOption.compare(
"SPATIAL_INDEX=NO"_L1, Qt::CaseInsensitive ) == 0
161 || layerOption.compare(
"SPATIAL_INDEX=OFF"_L1, Qt::CaseInsensitive ) == 0
162 || layerOption.compare(
"SPATIAL_INDEX=FALSE"_L1, Qt::CaseInsensitive ) == 0
163 || layerOption.compare(
"SPATIAL_INDEX=0"_L1, Qt::CaseInsensitive ) == 0 )
165 mCreateSpatialIndex =
false;
169 modifiedLayerOptions << layerOption;
173 modifiedLayerOptions << u
"SPATIAL_INDEX=FALSE"_s;
174 modifiedOptions[u
"layerOptions"_s] = modifiedLayerOptions;
182 mError = pReg->
createEmptyLayer( providerKey, uri, fields, geometryType, crs, overwrite, mOldToNewAttrIdx, errMsg, !modifiedOptions.isEmpty() ? &modifiedOptions :
nullptr, uriUpdated );
186 mErrorMessage = errMsg;
190 const auto constMOldToNewAttrIdx = mOldToNewAttrIdx;
191 for (
const int idx : constMOldToNewAttrIdx )
193 if ( idx > mAttributeCount )
194 mAttributeCount = idx;
203 if ( providerKey ==
"oracle"_L1 )
209 auto vectorProvider = std::unique_ptr<QgsVectorDataProvider>( qobject_cast< QgsVectorDataProvider * >( pReg->
createProvider( providerKey, uriUpdated, providerOptions ) ) );
213 mErrorMessage = QObject::tr(
"Loading of layer failed" );
225 const QString fidName = options.value( u
"FID"_s, u
"FID"_s ).toString();
229 mOldToNewAttrIdx.remove( fidIdx );
233 mProvider = std::move( vectorProvider );
241 if ( mCreateSpatialIndex )
243 createSpatialIndex();
254 return mErrorMessage;
264 QgsFeatureList::iterator fIt = features.begin();
266 for ( ; fIt != features.end(); ++fIt )
283 for (
int i = 0; i < attrs.count(); ++i )
287 const int dstIdx = mOldToNewAttrIdx.value( i, -1 );
291 QgsDebugMsgLevel( u
"moving field from pos %1 to %2"_s.arg( i ).arg( dstIdx ), 3 );
295 mFeatureBuffer.append( newFeat );
298 if ( mFeatureBufferMemoryUsage >= 100 * 1000 * 1000 )
308 return mErrorMessage;
313 mFeatureBufferMemoryUsage = 0;
314 if ( mFeatureBuffer.count() <= 0 )
319 const QStringList errors = mProvider->errors();
320 mProvider->clearErrors();
323 = QObject::tr(
"Creation error for features from #%1 to #%2. Provider errors were: \n%3" ).arg( mFeatureBuffer.first().id() ).arg( mFeatureBuffer.last().id() ).arg( errors.join( QLatin1Char(
'\n' ) ) );
326 mErrorCount += mFeatureBuffer.count();
328 mFeatureBuffer.clear();
333 mFeatureBuffer.clear();
337bool QgsVectorLayerExporter::createSpatialIndex()
339 mCreateSpatialIndex =
false;
342 return mProvider->createSpatialIndex();
353 const QString &providerKey,
357 const QMap<QString, QVariant> &options,
376 bool shallTransform =
false;
385 shallTransform =
true;
390 outputCRS = layer->
crs();
393 QMap<QString, QVariant> providerOptions = _providerOptions;
394 const bool overwrite = providerOptions.take( u
"overwrite"_s ).toBool();
395 const bool forceSinglePartGeom = providerOptions.take( u
"forceSinglePartGeometryType"_s ).toBool();
398 bool useFieldMapping =
false;
399 QList<QgsExpression> expressions;
402 outputFields = layer->
fields();
406 useFieldMapping =
true;
407 const QList<QgsVectorLayerExporter::OutputField> exportFieldDefinitions = exportOptions.
outputFields();
410 outputFields.
append( field.field );
412 expressions.last().prepare( &expressionContext );
413 if ( expressions.last().hasParserError() )
416 *
errorMessage = QObject::tr(
"Parser error for field \"%1\" with expression \"%2\": %3" ).arg( field.field.name(), field.expression, expressions.last().parserErrorString() );
424 bool convertGeometryToSinglePart =
false;
428 convertGeometryToSinglePart =
true;
431 auto writer = std::make_unique< QgsVectorLayerExporter >( uri, providerKey, outputFields, wkbType, outputCRS, overwrite, providerOptions );
455 shallTransform =
false;
497 bool canceled =
false;
508 *
errorMessage +=
'\n' + QObject::tr(
"Import was canceled at %1 of %2" ).arg( n ).arg( approxTotal );
513 if ( writer->errorCount() > 1000 )
517 *
errorMessage +=
'\n' + QObject::tr(
"Stopping after %n error(s)",
nullptr, writer->errorCount() );
523 outputFeature.
setId( sourceFeature.
id() );
526 if ( shallTransform )
539 const QString msg = QObject::tr(
"Failed to transform feature with ID '%1'. Writing stopped. (Exception: %2)" ).arg( sourceFeature.
id() ).arg( e.
what() );
557 const QString msg = QObject::tr(
"Failed to transform a feature with ID '%1' to single part. Writing stopped." ).arg( sourceFeature.
id() );
567 if ( useFieldMapping )
570 attributes.reserve( expressions.size() );
571 for (
auto it = expressions.begin(); it != expressions.end(); ++it )
575 expressionContext.
setFeature( sourceFeature );
576 const QVariant value = it->evaluate( &expressionContext );
577 if ( it->hasEvalError() )
579 const QString msg = QObject::tr(
"Evaluation error in expression \"%1\": %2" ).arg( it->expression(), it->evalErrorString() );
585 attributes.append( value );
589 attributes.append( QVariant() );
599 if ( !writer->addFeature( outputFeature ) )
610 feedback->
setProgress( 100.0 *
static_cast< double >( n ) /
static_cast< double >( approxTotal ) );
615 if ( !writer->flushBuffer() )
622 const int errors = writer->errorCount();
624 if ( writer->mCreateSpatialIndex && !writer->createSpatialIndex() )
638 *
errorMessage +=
'\n' + QObject::tr(
"Only %1 of %2 features written." ).arg( n - errors ).arg( n );
648 else if ( errors > 0 )
663 , mOwnsLayer( ownsLayer )
665 , mDestProviderKey( providerKey )
666 , mOptions( options )
669 mExportOptions.setDestinationCrs( destinationCrs );
681 , mOwnsLayer( ownsLayer )
683 , mDestProviderKey( providerKey )
684 , mExportOptions( exportOptions )
685 , mOptions( providerOptions )
690 if ( mLayer && mOwnsLayer )
691 mLayer->moveToThread(
nullptr );
698 auto newTask = std::make_unique<QgsVectorLayerExporterTask>( layer, uri, providerKey, destinationCrs, options,
true );
699 return newTask.release();
704 mOwnedFeedback->cancel();
714 mLayer->moveToThread( QThread::currentThread() );
722 mLayer->moveToThread(
nullptr );
729 if ( mOwnsLayer && mLayer )
730 mLayer->moveToThread( QThread::currentThread() );
@ AddFeatures
Allows adding features.
@ CreateSpatialIndex
Allows creation of spatial index.
VectorExportResult
Vector layer export result codes.
@ Success
No errors were encountered.
@ ErrorInvalidLayer
Could not access newly created destination layer.
@ ErrorFeatureWriteFailed
An error occurred while writing a feature to the destination.
@ ErrorAttributeCreationFailed
Destination provider was unable to create an attribute.
@ UserCanceled
User canceled the export.
@ ErrorProjectingFeatures
An error occurred while reprojecting features to destination CRS.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QFlags< VectorDataProviderAttributeEditCapability > VectorDataProviderAttributeEditCapabilities
Attribute editing capabilities which may be supported by vector data providers.
WkbType
The WKB type describes the number of dimensions a geometry has.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Contains information about the context in which a coordinate transform is executed.
Custom exception class for Coordinate Reference System related exceptions.
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 & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
QFlags< SinkFlag > SinkFlags
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE bool setAttribute(int field, const QVariant &attr)
Sets an attribute's value by field index.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
int approximateMemoryUsage() const
Returns the approximate RAM usage of the feature, in bytes.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setId(QgsFeatureId id)
Sets the feature id for this feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool convertToSingleType()
Converts multi type geometry into single type geometry e.g.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QgsCoordinateReferenceSystem crs
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
A registry / canonical manager of data providers.
QVariantMap decodeUri(const QString &providerKey, const QString &uri)
Breaks a provider data source URI into its component paths (e.g.
QgsEmptyLayerCreationResult createEmptyLayer(const QString &providerKey, const QString &uri, const QgsFields &fields, Qgis::WkbType wkbType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), Qgis::CreateLayerActionOnExisting actionOnExisting=Qgis::CreateLayerActionOnExisting::Abort)
Creates new empty vector layer.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QgsDataProvider * createProvider(const QString &providerKey, const QString &dataSource, const QgsDataProvider::ProviderOptions &options=QgsDataProvider::ProviderOptions(), Qgis::DataProviderReadFlags flags=Qgis::DataProviderReadFlags())
Creates a new instance of a provider.
A rectangle specified with double values.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsRectangle with associated coordinate reference system.
virtual void cancel()
Notifies the task that it should terminate.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
@ CanCancel
Task can be canceled.
void setDependentLayers(const QList< QgsMapLayer * > &dependentLayers)
Sets a list of layers on which the task depends.
void setProgress(double progress)
Sets the task's current progress.
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.
QgsReferencedRectangle extent() const
Returns the extent filter for the features to export.
void setExtent(const QgsReferencedRectangle &extent)
Sets an extent filter for the features to export.
void setOutputFields(const QList< QgsVectorLayerExporter::OutputField > &fields)
Sets the output field definitions for the destination table.
void setSelectedOnly(bool selected)
Sets whether the export should only include selected features.
QString filterExpression() const
Returns the filter expression for features to export.
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination coordinate reference system to use for exported features.
const QgsExpressionContext & expressionContext() const
Returns the expression context used when a filterExpression() is set.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context used when transforming exported features.
void setFilterExpression(const QString &expression)
Set the filter expression for the features to export.
bool selectedOnly() const
Returns whether the export will only include selected features.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context to use when a filterExpression() is set.
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context to use when transforming exported features.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system used for exported features.
QList< QgsVectorLayerExporter::OutputField > outputFields() const
Returns the output field definitions for the destination table.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
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.
QString errorMessage() const
Returns any error message encountered during the export.
Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const
Returns the attribute capabilities of the exporter.
~QgsVectorLayerExporter() override
Finalizes the export and closes the new created layer.
QString lastError() const override
Returns the most recent error encountered by the sink, e.g.
bool flushBuffer() override
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to...
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.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
static Q_INVOKABLE QString displayString(Qgis::WkbType type)
Returns a non-translated display string type for a WKB type, e.g., the geometry name used in WKT geom...
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
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 c
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
Qgis::VectorExportResult createEmptyLayer_t(const QString &uri, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &destCRS, bool overwrite, QMap< int, int > *oldToNewAttrIdx, QString *errorMessage, const QMap< QString, QVariant > *options)
Setting options for creating vector data providers.
Encapsulates output field definition.