I would like to loop over all layers within a QGIS project and determine which layer don't have a spatial index and cause QGIS to slow down.

I tried the method hasSpatialIndex() but this always returns SpatialIndexUnknown even if it's for example a shapefile with qix file present in the same folder (created from QGIS itself).

source = iface.activeLayer()

if source.hasSpatialIndex() == QgsFeatureSource.SpatialIndexNotPresent:
    print("not present")
elif source.hasSpatialIndex() == QgsFeatureSource.SpatialIndexUnknown:
elif source.hasSpatialIndex() == QgsFeatureSource.SpatialIndexPresent:
    print ("present")
    print ("something else")

I guess it's a bug that QGIS does not recognize the qix files in the same folder where the shapefiles are stored?

Is there really no PyQGIS-method to check layers if they are indexed? I am pretty sure that QGIS itselfs checks the existence of a spatial index because it uses the spatial index automatically if it exists.

  • I think that hasSpatialIndex should work with GeoPackage layers.
    – user30184
    Jul 2, 2020 at 13:35
  • @user30184: yes I think geopackage and spatialite works better, even with ogr but I am looking for a more generic solution to query shapefiles, postgis layers and so on
    – Thomas B
    Jul 2, 2020 at 13:36
  • Your question is related to gis.stackexchange.com/questions/53670/…. SpatiaLite and GeoPackage are special with spatial index because index does not just work but index must be requested in a subquery.
    – user30184
    Jul 2, 2020 at 14:13
  • For shp, except checking the extension sbn/sbx or qix exist, not sure how to do it. hasSpatialIndex work well only for index in spatial databases (PostGIS, Gpkg and Spatialite)
    – ThomasG77
    Jul 2, 2020 at 15:43

1 Answer 1


QGIS is able to check if the layer has spatial index or not for several providers, including shapefiles.

I've create a new project with two shapefiles. For one, I've created a spatial index. Then I used the GUI and PyQGIS to check if the index is correctly detected and reported.

test project

GUI: Check layer properties

Layer properties panel will tell if the layer has spatial index or not.

For this shapefile layer, since a spatial index exists, the option "Create Spatial Index" is disabled (and a label Spatial Index Exists is used).

Layer properties with Create Spatial Index disabled

For another layer, the spatial index does not exist.

Layer properties with Create Spatial Index enabled


If the GUI is showing the correct information, PyQGIS will do the same. You can test with this tiny script.

layers = QgsProject.instance().mapLayers().values()
for layer in layers:
    if (layer.dataProvider().hasSpatialIndex() == 1):
        print( f'The {layer.name()} has no spatial index' )
    if (layer.dataProvider().hasSpatialIndex() == 2):
        print( f'The {layer.name()} has a spatial index' )

Running this is writing the following output:

The madeira_com_indice has a spatial index
The madeira_sem_indice has no spatial index

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.