Saturday 7 January 2017

qgis - Maximum value of vector layer field in PyQGIS


If I have a vector layer in QGIS, how do I use Python to find the maximum value which a given field has? I need to convert the values in one field into values between 0 and 1. I guess I therefore need to set it to val/maxVal. That's why I need to find the maximum value.



Answer



This does not work in qgis3. See below for updated answer





It is not necessary to get a complete list of field values. In QgsVectorLayer exists 'maximumValue' method. So, this works well and it's shorter:


layer = iface.activeLayer()

idx = layer.fieldNameIndex('fieldName')
print layer.maximumValue(idx)

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...