Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Forked from olivierdalang/gist:4437246
Created January 3, 2013 12:21
Show Gist options
  • Save NathanW2/4443083 to your computer and use it in GitHub Desktop.
Save NathanW2/4443083 to your computer and use it in GitHub Desktop.
computeFieldIndex = layer.fieldNameIndex('population')
allFieldsMap = layer.pendingFields()
expression = QgsExpression('"affected" = 1')
expression.prepare(allFieldsMap)
if expression.hasParserError():
raise Exception('Parser error !!')
def getFeatures():
if self.selectedOnly:
# Just return the selected features
return layer.selectedFeatures()
else:
# Select all the features
layer.select(allFieldsMap)
# The layer is an iterator that returns a QgsFeature on next()
# so just return the layer
return layer
# Loop the features.
for feature in getFeatures():
result = expression.evaluate(feature)
if expression.hasEvalError():
raise Exception('Eval error !!')
continue
if result.toBool():
total += feature.attributeMap()[computeFieldIndex].toDouble()[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment