5

I have a QgsVectorLayer whose data source is a vector GeoPackage file. How can I find the name of its geometry column?

It is possible to use ogr to get it, but I am looking for a native PyQGIS approach without additional imports or separately loading the dataset again.

0

3 Answers 3

2

You need to use QgsDataSourceUri :

from qgis.utils import iface

layer = iface.activeLayer()
layer.dataProvider().uri().geometryColumn()

Edit, after reading comments. It doesn't work for GPKG.

0
1

Use this:

from qgis.utils import iface
from qgis.core import QgsMapLayerUtils

v_layer = iface.activeLayer()

conn = QgsMapLayerUtils.databaseConnection(v_layer)
true_layer_name = v_layer.source().split('layername=')[-1]
table = conn.table('', true_layer_name)

print(table.geometryColumn())
0
1

Couldn't find a dataProvider property either, that shows the geometry column.

As a temporary hack, I would use GDAL OGR:

from osgeo import ogr
from qgis.utils import iface

layer = iface.activeLayer()
source = layer.source().split('|')
gpkg = source[0]
lname = source[1].split('=')[1]

conn = ogr.Open(gpkg)
l = conn.GetLayerByName(lname)
geomName = l.GetGeometryColumn()
print(geomName)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.