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?

Indian
  • 318
  • 2
  • 16

1 Answers1

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

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • what if i want all the buffers in one layer and not a separate layer for each buffer ? – Abhijit Gujar Jun 12 '17 at 11:13
  • @AbhijitGujar - Just run processing.runandload("qgis:fixeddistancebuffer", layer, 0.01, 99, False, None) without a for loop :) – Joseph Jun 12 '17 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 – Abhijit Gujar Jun 12 '17 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 Jun 12 '17 at 13:30