Skip to content
Permalink
Browse files

Add a lastError() getter to QgsFeatureSink for retrieving the last error

encountered by the sink (e.g. after adding a feature fails)
  • Loading branch information
nyalldawson committed Jul 22, 2020
1 parent 262a193 commit 3eec9a47f03f0629813631ef34e568a38ca6d491
@@ -72,6 +72,13 @@ Adds all features from the specified ``iterator`` to the sink. Feature addition
Flushes any internal buffer which may exist in the sink, causing any buffered features to be added to the sink's destination.

:return: ``False`` if any buffered features could not be added to the sink.
%End

virtual QString lastError() const;
%Docstring
Returns the most recent error encountered by the sink, e.g. when a call to :py:func:`~QgsFeatureSink.addFeatures` returns ``False``.

.. versionadded:: 3.16
%End
};

@@ -36,6 +36,7 @@ Constructs a new QgsProxyFeatureSink which forwards features onto a destination
virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
virtual QString lastError() const;

QgsFeatureSink *destinationSink();
%Docstring
@@ -192,6 +192,8 @@ Remaps a ``feature`` to a set of features compatible with the destination sink.

virtual bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


QgsFeatureSink *destinationSink();
%Docstring
@@ -234,6 +234,8 @@ or if the given attribute is not an enum type.

virtual bool addFeatures( QgsFeatureList &flist /In,Out/, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


virtual bool deleteFeatures( const QgsFeatureIds &id );
%Docstring
@@ -572,6 +572,8 @@ Retrieves error message

virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


bool addFeatureWithStyle( QgsFeature &feature, QgsFeatureRenderer *renderer, QgsUnitTypes::DistanceUnit outputUnit = QgsUnitTypes::DistanceMeters );
%Docstring
@@ -124,6 +124,8 @@ Returns the number of error messages encountered during the export.

virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );

virtual QString lastError() const;


~QgsVectorLayerExporter();

@@ -106,6 +106,13 @@ class CORE_EXPORT QgsFeatureSink
* \returns FALSE if any buffered features could not be added to the sink.
*/
virtual bool flushBuffer() { return true; }

/**
* Returns the most recent error encountered by the sink, e.g. when a call to addFeatures() returns FALSE.
*
* \since QGIS 3.16
*/
virtual QString lastError() const { return QString(); }
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureSink::Flags )
@@ -47,6 +47,7 @@ class CORE_EXPORT QgsProxyFeatureSink : public QgsFeatureSink
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeature( feature, flags ); }
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeatures( features, flags ); }
bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override { return mSink->addFeatures( iterator, flags ); }
QString lastError() const override { return mSink->lastError(); }

/**
* Returns the destination QgsFeatureSink which the proxy will forward features to.
@@ -130,6 +130,11 @@ bool QgsRemappingProxyFeatureSink::addFeatures( QgsFeatureIterator &iterator, Qg
return res;
}

QString QgsRemappingProxyFeatureSink::lastError() const
{
return mSink->lastError();
}

QVariant QgsRemappingSinkDefinition::toVariant() const
{
QVariantMap map;
@@ -219,6 +219,7 @@ class CORE_EXPORT QgsRemappingProxyFeatureSink : public QgsFeatureSink
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Returns the destination QgsFeatureSink which the proxy will forward features to.
@@ -89,6 +89,11 @@ bool QgsVectorDataProvider::addFeatures( QgsFeatureList &flist, Flags flags )
return false;
}

QString QgsVectorDataProvider::lastError() const
{
return mErrors.last();
}

bool QgsVectorDataProvider::deleteFeatures( const QgsFeatureIds &ids )
{
Q_UNUSED( ids )
@@ -260,6 +260,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
virtual void enumValues( int index, QStringList &enumList SIP_OUT ) const { Q_UNUSED( index ) enumList.clear(); }

bool addFeatures( QgsFeatureList &flist SIP_INOUT, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Deletes one or more features from the provider. This requires the DeleteFeatures capability.
@@ -2296,6 +2296,11 @@ bool QgsVectorFileWriter::addFeatures( QgsFeatureList &features, QgsFeatureSink:
return result;
}

QString QgsVectorFileWriter::lastError() const
{
return mErrorMessage;
}

bool QgsVectorFileWriter::addFeatureWithStyle( QgsFeature &feature, QgsFeatureRenderer *renderer, QgsUnitTypes::DistanceUnit outputUnit )
{
// create the feature
@@ -762,6 +762,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink

bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Adds a \a feature to the currently opened data source, using the style from a specified \a renderer.
@@ -188,6 +188,11 @@ bool QgsVectorLayerExporter::addFeature( QgsFeature &feat, Flags )
return true;
}

QString QgsVectorLayerExporter::lastError() const
{
return mErrorMessage;
}

bool QgsVectorLayerExporter::flushBuffer()
{
if ( mFeatureBuffer.count() <= 0 )
@@ -136,6 +136,7 @@ class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink

bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;

/**
* Finalizes the export and closes the new created layer.

0 comments on commit 3eec9a4

Please sign in to comment.
You can’t perform that action at this time.