Tuesday, 12 December 2017

QGIS selecting features and using field calculator to update existing field using python


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

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...