10

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:
    print("unknown")
elif source.hasSpatialIndex() == QgsFeatureSource.SpatialIndexPresent:
    print ("present")
else:
    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.

4
  • I think that hasSpatialIndex should work with GeoPackage layers.
    – user30184
    Jul 2 '20 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 Jul 2 '20 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 '20 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 '20 at 15:43
2

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

PyQGIS

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
0

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.