3

Is there a possibility to add continuous numbers in a seperated row that based on their location? Like f.e. from north to south, or left to right. i used row_number, but the numbers seem to be coincidental...example pic

Marek
  • 525
  • 1
  • 8
  • https://gis.stackexchange.com/questions/296758/sorting-grid-cells-in-fishnet-grid-by-location-and-number-using-qgis/296762#296762 – BERA Jan 19 '23 at 08:58

3 Answers3

9

You can create a Virtual Layer and use row_number function, ordered by for example each features centroid y coordinate:

select *, row_number() over(order by st_y(centroid(geometry))) as newid
from ak_riks

ID increasing from south to north: enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161
7

To order the features (e.g.) west to east, you can use this expression on a new field in the layer:

array_find(                         -- find the position of the current feature in the array (this becomes the calculated value)
    aggregate(                      -- create an array of all the feature ids in the layer
        layer:='your_layer_name',   -- the name of your layer 
        aggregate:='array_agg', 
        expression:=$id, 
        order_by:=x($geometry)      -- order the points west to east
    ),
    $id                             -- the current feature id
) + 1                               -- start the 'position id' at 1 instead of 0

It will number the features 1, ..., Nfeatures with their position.

enter image description here

Matt
  • 16,843
  • 3
  • 21
  • 52
  • Yes indeed, you can add 1 to the value returned by array_find, see my edit. – Matt Jan 19 '23 at 10:48
  • I am not sure if I did something wrong but my results are identical to $id...

    array_find ( aggregate (layer:='layername in my project', aggregate:='array_agg', expression:=$id, order_by:=x($geometry) ), $id) + 1

    – Marek Jan 19 '23 at 14:26
7

You can use this Field Calculator expression for numbering north to south for instance:

array_find(
           array_agg(y($geometry), -- list the Y coordinates
           order_by:=-1*y($geometry)), -- order by descending Y coordinates
       y($geometry)) -- finds the current feature Y coordinate index in the array

enter image description here

enter image description here

Kasper
  • 3,192
  • 1
  • 2
  • 19