9

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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
wollyka
  • 447
  • 3
  • 10

2 Answers2

13

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

pigreco
  • 4,460
  • 1
  • 15
  • 32
  • 2
    I like this solution! – Babel May 29 '21 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 May 29 '21 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

BERA
  • 72,339
  • 13
  • 72
  • 161