1

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

Demonshine
  • 149
  • 8

1 Answers1

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
FROM Grid

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

JGH
  • 41,794
  • 3
  • 43
  • 89
  • 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 Jul 16 '21 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 Jul 16 '21 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 Jul 16 '21 at 18:52