3

I am wondering how to select features based on conditions from multiple fields to update an existing field in QGIS. This is the code I have so far. The expression I want to use to select features is SFHA_TF = T, and the field I want to update is the newly added one.

##layer=vector
##fld_zone=field layer
##zone_subty=field layer
##sfha=field layer

from qgis.core import *
import qgis.utils
from PyQt4.QtCore import *

layer=processing.getObject(layer)
zone_index=layer.fieldNameIndex(fld_zone)
subty_index=layer.fieldNameIndex(zone_subty)
sfha_index=layer.fieldNameIndex(sfha)

layer.startEditing()
provider= layer.dataProvider()

fields_to_delete = [fid for fid in range(len(provider.fields())) if fid !=      zone_index and fid !=subty_index and fid !=sfha_index]
provider.deleteAttributes(fields_to_delete)
layer.updateFields()

provider.addAttributes([QgsField('FLOOD_RISK', QVariant.String)])


layer.commitChanges()
5
  • Please be more specific - what conditions, what fields? Jan 22 '17 at 19:25
  • Does your code work? However, it's not clear which value you want to store in the 'FLOOD_RISK' field.
    – mgri
    Jan 22 '17 at 20:54
  • The code works up until the expr. For this case in the code, I am interested in deleting the features selected, however; there are other cases that have different conditions that I am interested in filling up the FLOOD_RISK field such as '100 YEAR FLOOD PLAIN', '500 YEAR' etc...
    – monkeyon
    Jan 22 '17 at 21:37
  • It appears you are deleting attributes not selecting them, was this your intention?
    – artwork21
    Jan 23 '17 at 12:48
  • In this code, I'm deleting all of the fields besides the ones that contain the conditions that I'm using to select by. Once these are selected, I'm trying to update the new field that is added. The part i'm stuck at is trying to select features from the fields still existing. I don't have any code written for that at this point.
    – monkeyon
    Jan 23 '17 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.