-
-
Save NathanW2/4443083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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