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

0

3 Answers 3

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

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

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

2
  • Yes indeed, you can add 1 to the value returned by array_find, see my edit.
    – Matt
    Jan 19, 2023 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, 2023 at 14:26

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.