13

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.

Tom Chadwin
  • 5,842
  • 4
  • 25
  • 48
  • and what if I'd like to evaluate the maximum for each unique value? This doesn't work at all...could someone find the error? thanks uniquevalues=layer.uniqueValues(index,limit=10000) for uv in uniquevalues: features = layer.getFeatures() for feat in features: values = [] attrs = feat.attributes() values.append(attrs[field]) if feat[field] == uv: maxarea = max(values) – FedericaZ May 17 '17 at 15:44

4 Answers4

20

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)

xunilk
  • 29,891
  • 4
  • 41
  • 80
  • 3
    Just a note - the biggest advantage of using QgsVectorLayer.maximumValue over iterating through every value is that maximumValue can hand calculation of the maximum over to the data source itself. Ie, for a Postgres layer QGIS will send a simple "select max("field") from ..." query to the database, rather then having to fetch every single feature from the database and iterate through them. It's a dramatic performance win. – ndawson Sep 15 '16 at 22:07
  • Not working on string columns. Code proposed by Joseph fix te issue. – Kostadin Jan 16 '18 at 14:07
  • Notice that the precision seems limited, I have a few dozen points with a field set at 438.56, and the result is 438.57. Using layer.maximumValue(layer.fields().indexFromName('field_alt')), QGIS 3.22, Win 10 Pro. – S.E. Apr 22 '22 at 14:30
11

The accepted answer does not work in QGIS3.

Now one has to do:

fieldname='id'
layer=iface.activeLayer()
idx=layer.fields().indexFromName(fieldname)
print(layer.maximumValue(idx))

(I am setting fieldname as the first line to make it easier to cut and paste for someone wanting to test it with their layer)

MortenSickel
  • 3,734
  • 1
  • 19
  • 40
5

You could use the following in the Python Console to print out the maximum value for a given field:

values = []
layer = qgis.utils.iface.activeLayer()
idx = layer.fieldNameIndex('fieldName')
for feat in layer.getFeatures():
    attrs = feat.attributes()
    values.append(attrs[idx])

print max(values)
Joseph
  • 75,746
  • 7
  • 171
  • 282
1

There are also several other approaches available:

Approach 1 : QgsAggregateCalculator with Max

Available aggregates to calculate.

Not all aggregates are available for all field types.

This approach is available since QGIS 2.16.

layer = iface.activeLayer()
max_value = layer.aggregate(QgsAggregateCalculator.Max, "name_of_your_field")[0]
print(max_value)

Approach 2 : QgsVectorLayerUtils

Contains utility methods for working with QgsVectorLayers.

This approach is available since QGIS 3.

layer = iface.activeLayer()
max_value = max(QgsVectorLayerUtils.getValues(layer, "name_of_your_field")[0])
print(max_value)

Approach 3 : QgsVectorLayer class with getFeatures() method

Similar to @Joseph's solution

layer = iface.activeLayer()
max_value = max([feat["name_of_your_field"] for feat in layer.getFeatures()])
print(max_value)
Taras
  • 32,823
  • 4
  • 66
  • 137