2

I am trying to find the python equivalent of enabling the checkbox for controlling the feature rendering order based on an attribute of the features of a layer (see below screenshot).

enter image description here

I want to base the rendering order on 'height' as shown in the screenshot.

My code below reads:

lyr_features = sorted(lyr_features, key = lambda i: i['height'])
colours = self.linear_gradient(len(lyr_features))
for i, feat in enumerate(lyr_features):
    myMin = feat['height']
    myMax = feat['height'] + 0.5
    myLabel = '{}'.format(feat['height'])
    myColour = QtGui.QColor(str(colours[i]))
    mySymbol = QgsSymbol.defaultSymbol(lyr.geometryType())
    mySymbol.setColor(myColour)
    mySymbol.setOpacity(myOpacity)
    myRange = QgsRendererRange(myMin, myMax, mySymbol, myLabel)
    myRangeList.append(myRange)

myRenderer = QgsGraduatedSymbolRenderer('', myRangeList)
myRenderer.setMode(QgsGraduatedSymbolRenderer.Pretty)
myStyle = QgsStyle().defaultStyle()
defaultColorRampNames = myStyle.colorRampNames()
ramp = myStyle.colorRamp(defaultColorRampNames[25])
myRenderer.setSourceColorRamp(ramp)
myRenderer.setClassAttribute(myTargetField)

lyr.setRenderer(myRenderer)
QgsProject.instance().addMapLayer(lyr)
lyr.triggerRepaint()

This is okay, but I then need to manually go in and enable the feature rendering control. Maybe something like this:

myRenderer.orderByEnabled(True)

This doesn't work but maybe along the lines of this?

  • That is not the correct method. You need to establish the order and then setOrderByEnabled – Fran Raga May 14 at 8:37
1

I add an example of code.

myRenderer.setOrderBy(QgsFeatureRequest.OrderBy([QgsFeatureRequest.OrderByClause('Value', False)]))
myRenderer.setOrderByEnabled(True)

QGIS Api OrderByClause

OrderByClause (const QString &expression, bool ascending=true)

Code reference : QGIS Github

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.