2

I have a QgsVectorLayer. How can I find the name of its geometry column, if the data format has one? The goal is to write layer filter queries that reference the 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.

1 Answer 1

1

You need to use QgsDataSourceUri :

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

Edit, after reading comments, it doesn't work for all layers. Not for GPKG. It works fine with PG layers. As it's written in the documentation above in the last part of the sentence : "Returns the name of the geometry column stored in the URI, if set. "

2
  • 1
    That does not work for a GPKG layer I tested. The geometry column is "geom" but the return value from the suggestions is an empty string. Commented Nov 22, 2022 at 11:20
  • It looks like for this to work the column name has to be set explicitly.
    – Matt
    Commented Nov 22, 2022 at 11:56

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.