I would like to number my grid cells from Northwest to Southeast. I tested a formula in the field calculator found in this post: Vector grid id generation.

But the problem is that I would like to have the number "9" instead of the number "10". So I want a series of continuous numbers. I can't do it manually because I have many of layers. Do you know how to solve this problem?

enter image description here

1 Answer 1


A minor change from this post leads to this:

Go the the menu layer / add layer / add-edit virtual layer and enter the following query.

select *, 
  ROW_NUMBER() OVER(ORDER BY  ST_MinY(geometry) desc, ST_MinX(geometry) asc) as newID

Replace the layer name (Grid) with the real name of your layer. If you don't want to have the old id field and the new newID field, replace * by the list of field name from Grid you want.

The query gets all rows from the Grid layer, then orders each polygon by Y (from northern to southern) and X (West to East) and generates a new sequential row number.

enter image description here

  • Thanks you for your help JGH. But I get an error : "HINT: No function matches the given name and argument types. You might need to add explicit type casts."
    – Demonshine
    Commented Jul 16, 2021 at 18:03
  • @Demonshine Make sure you have used the proper layer name, or the proper geometry field name (if yours comes from a DB it might have a different name)
    – JGH
    Commented Jul 16, 2021 at 18:10
  • Thanks, it works! Is it possible to do the same thing on hundreds of files at once? Maybe using the modelbuilder?
    – Demonshine
    Commented Jul 16, 2021 at 18:52

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.