Skip to content

Commit

Permalink
[api] Add QgsVectorDataProvider::geometryColumnName
Browse files Browse the repository at this point in the history
Allows easy retrieval of the geometry column name from the data
provider, if applicable.

(This was NOT possible to retrieve in a consistent, provider
independant way before)
  • Loading branch information
nyalldawson committed Dec 16, 2024
1 parent 4ca5fcb commit 9f4bb37
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ Returns list of indexes to fetch all attributes in :py:func:`~QgsVectorDataProvi
virtual QgsAttributeList pkAttributeIndexes() const;
%Docstring
Returns list of indexes of fields that make up the primary key
%End

virtual QString geometryColumnName() const;
%Docstring
Returns the name of the column storing geometry, if applicable.

.. versionadded:: 3.42
%End

QgsAttrPalIndexNameHash palAttributeIndexNames() const /Deprecated/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ Returns list of indexes to fetch all attributes in :py:func:`~QgsVectorDataProvi
virtual QgsAttributeList pkAttributeIndexes() const;
%Docstring
Returns list of indexes of fields that make up the primary key
%End

virtual QString geometryColumnName() const;
%Docstring
Returns the name of the column storing geometry, if applicable.

.. versionadded:: 3.42
%End

QgsAttrPalIndexNameHash palAttributeIndexNames() const /Deprecated/;
Expand Down
22 changes: 22 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ email : sherman at mrcc.com
#include "qgsvariantutils.h"
#include "qgsjsonutils.h"
#include "qgssetrequestinitiator_p.h"
#include "qgsthreadingutils.h"

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -3502,6 +3503,27 @@ Qgis::VectorDataProviderAttributeEditCapabilities QgsOgrProvider::attributeEditC
return mAttributeEditCapabilities;
}

QgsAttributeList QgsOgrProvider::pkAttributeIndexes() const
{
return mPrimaryKeyAttrs;
}

QString QgsOgrProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

if ( !mOgrLayer )
return QString();

QgsOgrFeatureDefn &featureDefinition = mOgrLayer->GetLayerDefn();
if ( featureDefinition.GetGeomFieldCount() )
{
OGRGeomFieldDefnH geomH = featureDefinition.GetGeomFieldDefn( 0 );
return QString::fromUtf8( OGR_GFld_GetNameRef( geomH ) );
}
return QString();
}

void QgsOgrProvider::computeCapabilities()
{
Qgis::VectorProviderCapabilities ability;
Expand Down
3 changes: 2 additions & 1 deletion src/core/providers/ogr/qgsogrprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class QgsOgrProvider final: public QgsVectorDataProvider
bool createAttributeIndex( int field ) override;
Qgis::VectorProviderCapabilities capabilities() const override;
Qgis::VectorDataProviderAttributeEditCapabilities attributeEditCapabilities() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QgsAttributeList pkAttributeIndexes() const override;
QString geometryColumnName() const override;
void setEncoding( const QString &e ) override;
bool enterUpdateMode() override { return _enterUpdateMode(); }
bool leaveUpdateMode() override;
Expand Down
7 changes: 7 additions & 0 deletions src/core/vector/qgsvectordataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ QgsAttributeList QgsVectorDataProvider::pkAttributeIndexes() const
return QgsAttributeList();
}

QString QgsVectorDataProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return QString();
}

QList<QgsVectorDataProvider::NativeType> QgsVectorDataProvider::nativeTypes() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Expand Down
7 changes: 7 additions & 0 deletions src/core/vector/qgsvectordataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
*/
virtual QgsAttributeList pkAttributeIndexes() const;

/**
* Returns the name of the column storing geometry, if applicable.
*
* \since QGIS 3.42
*/
virtual QString geometryColumnName() const;

/**
* Returns list of indexes to names for QgsPalLabeling fix
*
Expand Down
8 changes: 8 additions & 0 deletions src/providers/hana/qgshanaprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "qgshanadataitems.h"
#include "qgslogger.h"
#include "qgsrectangle.h"
#include "qgsthreadingutils.h"

#include <QtGlobal>

Expand Down Expand Up @@ -494,6 +495,13 @@ long long QgsHanaProvider::featureCount() const
return mFeaturesCount;
}

QString QgsHanaProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return mGeometryColumn;
}

QgsFields QgsHanaProvider::fields() const
{
return mFields;
Expand Down
1 change: 1 addition & 0 deletions src/providers/hana/qgshanaprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class QgsHanaProvider final : public QgsVectorDataProvider
QString dataComment() const override;
long long featureCount() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QString geometryColumnName() const override;
QgsFields fields() const override;
QVariant minimumValue( int index ) const override;
QVariant maximumValue( int index ) const override;
Expand Down
8 changes: 8 additions & 0 deletions src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsdbquerylog.h"
#include "qgsdbquerylog_p.h"
#include "qgsvariantutils.h"
#include "qgsthreadingutils.h"

#include <QtGlobal>
#include <QFileInfo>
Expand Down Expand Up @@ -2034,6 +2035,13 @@ QgsAttributeList QgsMssqlProvider::pkAttributeIndexes() const
return mPrimaryKeyAttrs;
}

QString QgsMssqlProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return mGeometryColName;
}

QStringList QgsMssqlProvider::subLayers() const
{
return mTables;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class QgsMssqlProvider final : public QgsVectorDataProvider
QString description() const override;

QgsAttributeList pkAttributeIndexes() const override;

QString geometryColumnName() const override;
QgsRectangle extent() const override;

bool isValid() const override;
Expand Down
8 changes: 8 additions & 0 deletions src/providers/oracle/qgsoracleprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "qgsprojectstorageguiprovider.h"
#include "qgsprojectstorageregistry.h"
#include "qgsvectorlayer.h"
#include "qgsthreadingutils.h"

#include "qgsoracleprovider.h"
#include "moc_qgsoracleprovider.cpp"
Expand Down Expand Up @@ -1184,6 +1185,13 @@ bool QgsOracleProvider::isValid() const
return mValid;
}

QString QgsOracleProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return mGeometryColumn;
}

QVariant QgsOracleProvider::defaultValue( int fieldId ) const
{
QString defVal = mDefaultValues.value( fieldId, QString() ).toString();
Expand Down
1 change: 1 addition & 0 deletions src/providers/oracle/qgsoracleprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class QgsOracleProvider final : public QgsVectorDataProvider
QSet<QVariant> uniqueValues( int index, int limit = -1 ) const override;
bool isValid() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QString geometryColumnName() const override;
QVariant defaultValue( QString fieldName, QString tableName = QString(), QString schemaName = QString() );
QVariant defaultValue( int fieldId ) const override;
QString defaultValueClause( int fieldId ) const override;
Expand Down
8 changes: 8 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "qgsdbquerylog.h"
#include "qgsdbquerylog_p.h"
#include "qgspostgreslayermetadataprovider.h"
#include "qgsthreadingutils.h"

#include "qgspostgresprovider.h"
#include "qgsprovidermetadata.h"
Expand Down Expand Up @@ -3659,6 +3660,13 @@ QgsAttributeList QgsPostgresProvider::attributeIndexes() const
return lst;
}

QString QgsPostgresProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return mGeometryColumn;
}

Qgis::VectorProviderCapabilities QgsPostgresProvider::capabilities() const
{
return mEnabledCapabilities;
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class QgsPostgresProvider final : public QgsVectorDataProvider
Qgis::ProviderStyleStorageCapabilities styleStorageCapabilities() const override;
QgsAttributeList attributeIndexes() const override;
QgsAttributeList pkAttributeIndexes() const override { return mPrimaryKeyAttrs; }
QString geometryColumnName() const override;
QString defaultValueClause( int fieldId ) const override;
QVariant defaultValue( int fieldId ) const override;
bool skipConstraintCheck( int fieldIndex, QgsFieldConstraints::Constraint constraint, const QVariant &value = QVariant() ) const override;
Expand Down
9 changes: 8 additions & 1 deletion src/providers/spatialite/qgsspatialiteprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ email : [email protected]
#include "qgsspatialiteproviderconnection.h"
#include "qgsdbquerylog.h"
#include "qgsdbquerylog_p.h"

#include "qgsthreadingutils.h"
#include "qgsjsonutils.h"
#include "qgsvectorlayer.h"

Expand Down Expand Up @@ -5889,6 +5889,13 @@ QgsAttributeList QgsSpatiaLiteProvider::pkAttributeIndexes() const
return mPrimaryKeyAttrs;
}

QString QgsSpatiaLiteProvider::geometryColumnName() const
{
QGIS_PROTECT_QOBJECT_THREAD_ACCESS

return mGeometryColumn;
}

QList<QgsVectorLayer *> QgsSpatiaLiteProvider::searchLayers( const QList<QgsVectorLayer *> &layers, const QString &connectionInfo, const QString &tableName )
{
QList<QgsVectorLayer *> result;
Expand Down
1 change: 1 addition & 0 deletions src/providers/spatialite/qgsspatialiteprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class QgsSpatiaLiteProvider final : public QgsVectorDataProvider
QString name() const override;
QString description() const override;
QgsAttributeList pkAttributeIndexes() const override;
QString geometryColumnName() const override;
void invalidateConnections( const QString &connection ) override;
QList<QgsRelation> discoverRelations( const QgsVectorLayer *target, const QList<QgsVectorLayer *> &layers ) const override;

Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ def testTriangleTINPolyhedralSurface(self):
self.assertTrue(f.geometry())
self.assertEqual(f.geometry().constGet().asWkt(), row[2])

def test_geometry_column_name(self):
"""Test geometry column name from shapefile"""
vl = QgsVectorLayer(
self.get_test_data_path("points.shp").as_posix(), "test", "ogr"
)
self.assertTrue(vl.isValid())
self.assertEqual(vl.dataProvider().geometryColumnName(), "")

def testSetupProxy(self):
"""Test proxy setup"""
settings = QgsSettings()
Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,17 @@ def testPkAttributeIndexes(self):
& QgsFieldConstraints.Constraint.ConstraintUnique
)

def test_geometry_column_name(self):
"""Test geometry column name from geopackage"""
vl = QgsVectorLayer(
self.get_test_data_path("points_gpkg.gpkg").as_posix()
+ "|layername=points_gpkg",
"test",
"ogr",
)
self.assertTrue(vl.isValid())
self.assertEqual(vl.dataProvider().geometryColumnName(), "geom")

def testSublayerWithComplexLayerName(self):
"""Test reading a gpkg with a sublayer name containing :"""
tmpfile = os.path.join(self.basetestpath, "testGeopackageComplexLayerName.gpkg")
Expand Down
12 changes: 12 additions & 0 deletions tests/src/python/test_provider_spatialite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,18 @@ def testUniqueConstraint(self):
QgsFieldConstraints.ConstraintOrigin.ConstraintOriginProvider,
)

def test_geometry_column_name(self):
"""Test geometry column name from geopackage"""
vl = QgsVectorLayer(
"""dbname='{}' table="somedata" (geom)""".format(
self.get_test_data_path("provider/spatialite.db").as_posix()
),
"test",
"spatialite",
)
self.assertTrue(vl.isValid())
self.assertEqual(vl.dataProvider().geometryColumnName(), "geom")

def testSkipConstraintCheck(self):
vl = QgsVectorLayer(
f"dbname={self.dbname} table=test_autoincrement",
Expand Down

0 comments on commit 9f4bb37

Please sign in to comment.