0

I'm doing a lot (really a lot) of feature requests on a vector layer. These requests are capsuled in iterations and functions, so no way of reducing the total number of requests.

Although I consider the #1 tip concerning QgsVectorLayer.getFeatures() - use a specific request whenever possible (Why use QgsFeatureRequest() when iterating?) - I end up with low performance and cProfile tells me the bottleneck is still the feature request.

Btw, using layer.getFeature(fid) instead of

request = QgsFeatureRequest().setFilterFid(fid)
feature = next(layer.getFeatures(request))

doesn't make any difference.

How do I speed up my feature requests, when I know each feature is called lots of times?

0

My solution is a dictionary called lineIndex, with the format {fid: QgsFeature}:

self.lineIndex={}
lines = layer.getFeatures()
for line in lines:  # every line
    self.lineIndex[line.id()] = line

Now I can acess any feature in no time by feat = self.lineIndex[fid].

For more complex questions, tuples as dictionary keys could be considered (https://codereview.stackexchange.com/questions/205141/search-dictionary-for-matching-values).

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.