I am making a buffer analisys in a standalone script. Is it possible to automatically during the analisys add index to the output vector layer (".qix" file)?

I tried to add after the analisys like in QGIS2:

layer_out = QgsVectorLayer(out, out[-7:-4],'ogr')


where out is a path to shapefile, out[-7:-4] the shapefile name. However in QGIS 3.2 Bonn, I got the error message:

AttributeError: 'QgsVectorLayer' object has no attribute 'createSpatialIndex'

If an automated way of creating index during analisys is not possible, how to create it after it in standalone script?

  • The defaul format in QGIS 3 is GeoPackage. Maybe that has some effect. Or does your code still create a shapefile with QGIS 3.2 if you do not try to create the spatial index? – user30184 Oct 18 '18 at 10:19
  • Without creating an index I have the proper shapefile from the buffer analisys. – Justyna Dębicka Oct 18 '18 at 10:23
  • If you just need to have .qix index you can create it with shptree utility. It is originally made for Mapserver github.com/mapserver/mapserver/wiki/ShpTree but it is often delivered together with GDAL. But of course it should still be possible to create it with Python. – user30184 Oct 18 '18 at 11:03

To create a spatial index in v3 try either:

index = QgsSpatialIndex()


index = QgsSpatialIndex(layer.getFeatures())

Using Spatial Index

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.