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
You could use the following which:
- Creates a list of your selected features.
- Clears the selection.
- Iterates through each feature in the list, selects it and runs the processing buffer algorithm.
- 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:
Result:
answered Mar 15, 2017 at 11:09
-
what if i want all the buffers in one layer and not a separate layer for each buffer ?Abhijit Gujar– Abhijit Gujar2017年06月12日 11:13:11 +00:00Commented Jun 12, 2017 at 11:13
-
@AbhijitGujar - Just run
processing.runandload("qgis:fixeddistancebuffer", layer, 0.01, 99, False, None)
without afor
loop :)Joseph– Joseph2017年06月12日 11:19:53 +00:00Commented 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 layerAbhijit Gujar– Abhijit Gujar2017年06月12日 13:25:42 +00:00Commented 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– Joseph2017年06月12日 13:30:58 +00:00Commented Jun 12, 2017 at 13:30
lang-py