Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
prov.createAttributeIndex(i)

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()
prov.createSpatialIndex()

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.