Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add api to determine attribute capabilities for QgsVectorLayerExporter
  • Loading branch information
nyalldawson committed Apr 21, 2023
1 parent 308eb1d commit b28bf20
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
Expand Up @@ -103,6 +103,13 @@ Returns the number of error messages encountered during the export.
.. seealso:: :py:func:`errorMessage`

.. seealso:: :py:func:`errorCode`
%End

Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const;
%Docstring
Returns the attribute capabilities of the exporter.

.. versionadded:: 3.32
%End

virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() );
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgis.h
Expand Up @@ -654,6 +654,24 @@ class CORE_EXPORT Qgis
};
Q_ENUM( VectorExportResult )

/**
* Capabilities supported by a QgsVectorFileWriter object.
* \since QGIS 3.32
*/
enum class VectorFileWriterCapability : int
{
FieldAliases = 1 << 0, //!< Writer can support field aliases
FieldComments = 1 << 2, //!< Writer can support field comments
};
Q_ENUM( VectorFileWriterCapability )

/**
* Capabilities supported by a QgsVectorFileWriter object.
* \since QGIS 3.32
*/
Q_DECLARE_FLAGS( VectorFileWriterCapabilities, VectorFileWriterCapability )
Q_FLAG( VectorFileWriterCapabilities )

/**
* SqlLayerDefinitionCapability enum lists the arguments supported by the provider when creating SQL query layers.
* \since QGIS 3.22
Expand Down Expand Up @@ -3554,6 +3572,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::LayerTreeFilterFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::LabelLinePlacementFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::LabelPolygonPlacementFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::DatabaseProviderConnectionCapabilities2 )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::VectorFileWriterCapabilities )

// hack to workaround warnings when casting void pointers
// retrieved from QLibrary::resolve to function pointers.
Expand Down
5 changes: 5 additions & 0 deletions src/core/vector/qgsvectorlayerexporter.cpp
Expand Up @@ -200,6 +200,11 @@ QString QgsVectorLayerExporter::errorMessage() const
return mErrorMessage;
}

Qgis::VectorDataProviderAttributeEditCapabilities QgsVectorLayerExporter::attributeEditCapabilities() const
{
return mProvider ? mProvider->attributeEditCapabilities() : Qgis::VectorDataProviderAttributeEditCapabilities();
}

bool QgsVectorLayerExporter::addFeatures( QgsFeatureList &features, Flags flags )
{
QgsFeatureList::iterator fIt = features.begin();
Expand Down
7 changes: 7 additions & 0 deletions src/core/vector/qgsvectorlayerexporter.h
Expand Up @@ -120,6 +120,13 @@ class CORE_EXPORT QgsVectorLayerExporter : public QgsFeatureSink
*/
int errorCount() const { return mErrorCount; }

/**
* Returns the attribute capabilities of the exporter.
*
* \since QGIS 3.32
*/
Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const;

bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
QString lastError() const override;
Expand Down
34 changes: 34 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -3396,6 +3396,40 @@ def testFieldComment(self):
self.assertEqual(fields[2].name(), 'field2')
self.assertEqual(fields[2].comment(), '')

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 7, 0), "GDAL 3.7 required")
def test_exporter_capabilities(self):
with tempfile.TemporaryDirectory() as temp_dir:
dest_file_name = os.path.join(temp_dir,
'test_gpkg.gpkg')

layer = QgsVectorLayer("point?crs=epsg:4326&field=id:integer",
"Scratch point layer", "memory")

exporter = QgsVectorLayerExporter(dest_file_name,
'ogr',
layer.fields(),
layer.wkbType(),
layer.crs())

self.assertTrue(
exporter.attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditAlias)
self.assertTrue(
exporter.attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditComment)

dest_file_name = os.path.join(temp_dir,
'test_shp.shp')

exporter = QgsVectorLayerExporter(dest_file_name,
'ogr',
layer.fields(),
layer.wkbType(),
layer.crs())

self.assertFalse(
exporter.attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditAlias)
self.assertFalse(
exporter.attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditComment)


if __name__ == '__main__':
unittest.main()

0 comments on commit b28bf20

Please sign in to comment.