I am trying to automate the repetative task of selecting by location and updating a field according to the selection. The issue is that field calculator uses all features instead of the selected. Is there a simple way to do this with qgis python? Here is my code:
import processing
moment_all = iface.activeLayer()
moment_all.startEditing()
processing.runalg("qgis:addfieldtoattributestable","filepath.shp","blast",2,2,0,"/Users/pgcseismolab/Desktop/qgis/moment_all.shp")
processing.runalg("qgis:selectbylocation","filepath.shp","filepath2.shp",['within'],0,0)
processing.runalg("qgis:fieldcalculator","filepath.shp","blast",0,10,3,False,"'b'","filepath.shp")
moment_all.commitChanges()
A similar thread I've found is here QGIS selecting features and using field calculator to update existing field using python, though I don't think it works for my situation. Would graphical modeler be a better way to do this?
processing.runalg("qgis:fieldcalculator",moment_all,"blast",0,10,3,False,"'b'","filepath.shp")