2

I have been able to select features using an attribute from a Point vector file:

expr = QgsExpression("\"NAME\" = 'PESCOE'") 
collegePoint = collegeLayer.getFeatures( QgsFeatureRequest (expr))

#get ids  
ids = [i.id() for i in collegePoint]

#select the features  
collegeLayer.setSelectedFeatures(ids)

Now for each of the selected features I wish to create a temporary buffer layer (not as presented in Buffering in pyQGIS?, as it is not quite clear).

How do I achieve this?

1 Answer 1

3

You could use the following which:

  1. Creates a list of your selected features.
  2. Clears the selection.
  3. Iterates through each feature in the list, selects it and runs the processing buffer algorithm.
  4. Each buffer is a temporary output which are immediately loaded.

Note that the "Use only selected features" option must be enabled in:

Processing > Options > General > Use only selected features

Here is the code:

import processing
layer = iface.activeLayer()
feat_list = []
for feat in layer.selectedFeatures():
    feat_list.append(feat)

layer.removeSelection()

for selected_feat in feat_list:
    layer.select( selected_feat.id() )
    processing.runandload("qgis:fixeddistancebuffer", layer, 0.01, 99, False, None)
    layer.removeSelection()

Example:

Sample points:

Sample points

Result:

Results

4
  • what if i want all the buffers in one layer and not a separate layer for each buffer ? Commented Jun 12, 2017 at 11:13
  • @AbhijitGujar - Just run processing.runandload("qgis:fixeddistancebuffer", layer, 0.01, 99, False, None) without a for loop :)
    – Joseph
    Commented Jun 12, 2017 at 11:19
  • Thanks but what i want is to make buffer for 1000 features but i certainly don't want 1000 buffers in layer panel but i need all 1000 buffers in one layer Commented Jun 12, 2017 at 13:25
  • @AbhijitGujar - Could you ask this as a new question please? I'm not sure I follow. Also include any code you have used :)
    – Joseph
    Commented Jun 12, 2017 at 13:30

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.