I need to list all layers with geometries but not those without.

Can I be sure geometryType = 4 always are tables without geometries?

I cannot find the documentation explaining which number is which type in plain text.

for lyr in QgsProject.instance().mapLayers().values():
   #if lyr.geometryType() != 4: #Do something
   print(lyr, lyr.geometryType())

enter image description here


You can simply use QgsVectorLayer.isSpatial() to filter geometry and geometryless layers.

Additionally, you should not use numbers to do such comparisons. Recommendations below.

Filtering layers with no geometry

for layer in QgsProject.instance().mapLayers().values():
    if layer.type() == QgsMapLayer.VectorLayer:
        if not layer.isSpatial():
            # Now we have a layer without geometry

Filtering layers by geometry type

This is the recommended way of filtering by geometry type:

layer.geometryType() == QgsWkbTypes.PolygonGeometry

Note: Such comparison includes all polygon variations, like multi, with Z, with M, and others.

Filtering layers by specific geometry type variations

If you need to test a particular polygon variation, you can use:

layer.wkbType() == QgsWkbTypes.MultiPolygonZ

Source: https://qgis.org/api/classQgsWkbTypes.html

| improve this answer | |
  • isSpatial(), so simple.. Wish I could accept two answers – BERA Oct 26 at 14:27

To be sure, you can also use the wkbType() method.

for lyr in QgsProject.instance().mapLayers().values():
    if lyr.wkbType() == 1:
        print(lyr.name(), "is a point layer")
    elif lyr.wkbType() == 2:
        print(lyr.name(), "is a line layer")
    elif lyr.wkbType() == 3:
        print(lyr.name(), "is a polygon layer")
    elif lyr.wkbType() == 100:
        print(lyr.name(), "is a layer without geometry")
        print(lyr.name(), "has another type of geometry")

The multi-geometries have a distinct number (For example, 5 for a multi-line geometry).

You can get the correspondence of the numbers on the following link: https://qgis.org/pyqgis/3.10/core/QgsWkbTypes.html?highlight=geometrytype#qgis.core.QgsWkbTypes.NoGeometry

| improve this answer | |
  • 1
    Thanks for the link. I have added another link that refers to the class QgsWkbTypes with the correspondance. – Vincent Bré Oct 26 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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