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()
No comments:
Post a Comment