6

I am making a buffer analysis in a standalone script.

Is it possible to automatically, during the analysis, add index to the output vector layer (".qix" file)?

I tried to add after the analysis like in QGIS2:

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

layer_out.dataProvider().createSpatialIndex()

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 analysis is not possible, how to create it after it in standalone script?

6
  • 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, 2018 at 10:19
  • Without creating an index I have the proper shapefile from the buffer analisys. Oct 18, 2018 at 10:23
  • 1
    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, 2018 at 11:03
  • 1
    I have just used a version of your code in QGIS LTR 3.22.4 and it worked successfully for me on a Shapefile, the only difference was that I assigned the dataProvider to a new variable and called createSpatialIndex on that. Mar 10, 2022 at 10:39
  • @bugmenot123 What is your purpose of starting the bounty? Because layer_out.dataProvider().createSpatialIndex() works without an error and makes .qix file in a standalone script (QGIS 3.28). Mar 5, 2023 at 18:54

2 Answers 2

10
+250

If you use layer_out.dataProvider().createSpatialIndex() you don't get that error. To get the error you must use layer_out.createSpatialIndex(). The code in the question doesn't throw the error: AttributeError: 'QgsVectorLayer' object has no attribute 'createSpatialIndex'

layer_out.dataProvider().createSpatialIndex() works without an error and makes .qix file in a standalone script (QGIS 3.28).

import os
import sys
from processing.core.Processing import Processing
import processing
from qgis.core import QgsApplication, QgsVectorLayer
from qgis.analysis import QgsNativeAlgorithms

sys.path.append('C:\\Program Files\\QGIS 3.28.1/apps/qgis/python/plugins')
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX_PATH'], True)

qgs = QgsApplication([], True)
qgs.initQgis()
Processing.initialize()
qgs.processingRegistry().addProvider(QgsNativeAlgorithms())

input_file = 'c:/path/to/input.shp'
out_file = 'c:/path/to/out.shp'

out = processing.run("native:buffer",
                     {'INPUT': input_file,
                      'OUTPUT': out_file})["OUTPUT"]

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

# this line generates .qix file
layer_out.dataProvider().createSpatialIndex()

qgs.exec_()
qgs.exitQgis()
0

To create a spatial index in v3 try either:

index = QgsSpatialIndex()

or

index = QgsSpatialIndex(layer.getFeatures())

Using Spatial Index

1
  • 4
    The question is asking how to create a file based spatial index, not an in-memory one. If an instance of QgsSpatialIndex can be written out as a .qix file then please expand your answer to cover that. Mar 10, 2022 at 9:52

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.