1

I have a polygon shapefile on which I need to eliminate features belonging to the same group (identified by a variable/attribute XG) on the base of the values taken by another variable YA. In particular, given some features with same XG, I would like to keep the one with the highest YA value.
There are many groups for which XG identifies only one feature.

Below is a sample structure of my attribute table in case I did not explain my self well.
What I have:

XG YA  
5 100  
5 130  
5 210  
2 0.4  
2 0.1  
1 0.6

What I would like to get using Python console in QGIS:

XG YA  
5 210  
2 0.4  
1 0.6
2

in this case I would recommend to use a virtual layer. It is not pyqgis but much simpler. May be it is not the answer you expected...the definition query would be (where mylayer is the name of your layer):

select geometry, xg, max(ya) from mylayer group by xg

you can also put this into a simple pyqgis script:

vlayer = QgsVectorLayer( "?query=select geometry, xg, max(ya) from mylayer group by xg;", "vlayer", "virtual" )
QgsProject.instance().addMapLayer(vlayer)

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.