Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Adding an Attribute Index to a Shapefile fails for a Python Plugin in QGIS: For example - the following python Code in a Plugin

ly = QgsVectorLayer(data_source, layer_name,"ogr")
prov = ly.dataProvider()

results in the error Message:

'QgsVectorDataProvider' object has no attribute 'createAttributeIndex'

Whereas Adding a Spatial Index works without any problem

ly = QgsVectorLayer(data_source, layer_name,"ogr")
prov = ly.dataProvider()

Is this a Bug?

Thanks in Advance

share|improve this question
The above Error message appears with the QGIS 1.8.0 standard build on Windows. Meanwhile I tested this piece of code with QGIS 1.8.0 on Debian, too. The result was the same Error. ('QgsVectorDataProvider' object has no attribute 'createAttributeIndex'). Seams that this Method doesn't work for Objects created with Python. In Addition, it seams that an attribute index has no influence on the performance of queries in QGIS, at least for shapefiles. – klausb Nov 13 '12 at 9:40
up vote 2 down vote accepted

This method is not exposed to python in QGis 1.8, but it is in the current development (master) release.

Please be aware, that before calling createAttributeIndex(), you should check the provider capabilities:

if prov.capabilities() & QgsVectorDataProvider.CreateAttributeIndex:

but for the ogr provider this is implemented and if you are sure you are always dealing with this data provider this step can safely be skipped.

If you notice no performance impact after creating an index, that is most likely a problem in the provider, as QGis itself does no indexing and only forwards the create index request to the data provider.

share|improve this answer
Thank you for the information! – klausb May 27 '13 at 13:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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