1

I wrote a plugin for selecting and instant updating the value of the selected feature in the attribute table. I tested with a big shapefile and to select and edit 5 features takes ~5 seconds. Which isn't really fast.

Can someone look at the piece of code and give me suggestions for how I can do for faster editing?

        lRect = self.canvas.mapRenderer().mapToLayerCoordinates(layer, r)
        layer.select(lRect, False)
        layer.startEditing()
        #for feat in layer.selectedFeatures():
        for feat in layer.selectedFeaturesIterator():
            feat["foo"] = my_value
            layer.updateFeature(feat)
        layer.commitChanges()
        layer.removeSelection()

I could use selectedFeatures() or selectedFeaturesIterator() but I tested it with both and I got nearly the same result (time).

1

I think this code with a QgsFeatureRequest() object speed up a bit:

        request = QgsFeatureRequest()
        lRect = self.canvas.mapRenderer().mapToLayerCoordinates(layer, r)
        request.setFilterRect(lRect)
        layer.startEditing()
        for feat in layer.getFeatures(request):
            feat['foo'] = value
            layer.updateFeature(feat)
        layer.commitChanges()
        layer.removeSelection()

found here http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/vector.html#iterating-over-a-subset-of-features

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.