0

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?

12
  • did you try to set qgis:fieldcalculator input's argument on the activeLayer than the shapefile itself "filepath.shp" ? Jul 21, 2017 at 20:17
  • I just tried using processing.runalg("qgis:fieldcalculator","filepath.shp","blast",0,10,3,False,"'b'","filepath.shp") moment_all.commitChanges() still affects the entire column, but I think using the active layer s]instead will resolve this so I will post any updates
    – jrowley
    Jul 21, 2017 at 20:39
  • I mean, something like this (not sure of the syntax) : processing.runalg("qgis:fieldcalculator",moment_all,"blast",0,10,3,False,"'b'","filepath.shp") Jul 21, 2017 at 21:18
  • that seems to work, though I'm not getting any output. If i do these steps manually it works, but when I take my processing history and put it into python as shown nothing really happens..
    – jrowley
    Jul 24, 2017 at 19:33
  • 1
    Obviously, there are several misunderstanding. First addfieldtoattributestable might not be necessary cause the fieldcalculator could create a new field if you need (set the proper parameter to True). Selectbylocation is done on filepath.shp not on the active layer. Then, in qgis:fieldcalculator, your are trying to set a string as a float (). Finaly your output will be populate with selected features, If there is no selection, all the feature are saved. Be sure your selection is done on the input layer of fieldcalculator. Please send a the simplest code as possible to be more helpfull. Jul 25, 2017 at 9:15

1 Answer 1

1

Obviously, there are several misunderstanding. First addfieldtoattributestable might not be necessary cause the fieldcalculator could create a new field if you need (set the proper parameter to True). Selectbylocation is done on filepath.shp not on the active layer. Then, in qgis:fieldcalculator, your are trying to set a string as a float (). Finaly your output will be populate with selected features, If there is no selection, all the feature are saved. Be sure your selection is done on the input layer of fieldcalculator. Please send a the simplest code as possible to be more helpfull.

1
  • From this, I got my code to work as seen in the last comments above
    – jrowley
    Jul 26, 2017 at 16:48

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.