10

I am using QGIS 3.18.

I have, for example, this layer with points or centroids (with x, y coordinates) enter image description here

I want to enumerate the points from right to left (for example) and top to bottom like this:

enter image description here

I tried sorting them using the Sort and Number plugin (using X and Y), I got close but not enough

I even tried to follow and modify the below

Automatically numbering points East-West & North-south on QGIS?

but still didn't get far Is there any other way?

2 Answers 2

16

labeling the points with this expression:

array_find(
    array_reverse( 
        array_sort(array_agg($y))
                 ), $y)+1

+1 because arrays start from zero

enter image description here

2
  • 2
    I like this solution!
    – Babel
    Commented May 29, 2021 at 19:29
  • 2
    Very neat solution! Both solutions are great. Too bad I cannot accept two answers. This solution is simple. However, BERA was fast to answer so I chose his solution! @pigreco
    – wollyka
    Commented May 29, 2021 at 20:48
11

Try this python script:

layer = iface.activeLayer() #Click layer in layer tree
idfield = 'enum' #Change to match the name of your field

coords = [[f.geometry().asPoint().x(), f.geometry().asPoint().y(), f.id()] for f in layer.getFeatures()] #List x coordinate, y coordinate and id of each point
coords.sort(key=lambda k: (k[1],-k[0]), reverse=True) #Sort by y, x
order = [i[2] for i in coords] #Extract only the ids from the sorted list of lists

i = layer.fields().indexFromName(idfield)
attrMap = {id: {i:e} for e,id in enumerate(order,1)}
layer.dataProvider().changeAttributeValues(attrMap)

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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