7

When creating a grid (e.g. dx=15,dy=20) in GQIS 3 the first cell begins from the upper left corner and continues to the bottom of the grid as shown in the picture below.

enter image description here

Is there any way to define the way that the id cells are located? Is there any plugin in QGIS? In fact, I want to begin from the lower left corner and continue to the east (as MapInfo does) as shown below.

enter image description here

nat
  • 289
  • 3
  • 13

1 Answers1

11

You can create a new layer with the re-ordered ID

Run the grid tool as you currently do.

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) asc, ST_MinX(geometry) asc) as newID
FROM Grid

you can replace the layer name (Grid) with the true name. 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 order each polygon by Y and X and generate a new sequential row number.

enter image description here

JGH
  • 41,794
  • 3
  • 43
  • 89
  • I tried this in QGIS3 but it gives the following error: "Query execution error on CREATE TEMP VIEW _tview AS select * , ROW_NUMBER() OVER(ORDER BY ST_MinY(geometry) asc, ST_MinX(geometry) asc) as newID FROM Grid: 1 - near "(": syntax error". – nat Apr 03 '19 at 11:44
  • 1
    @nat it works under 3.4.5 (LTR), which has spatialite 4.3 (help menu, about). The window function was introduced in spatialite 3.25 – JGH Apr 03 '19 at 11:48
  • Thank you a lot! I installed 3.4.6(LTR) on a MAC OS and it worked. – nat Apr 03 '19 at 13:19
  • I've tried this and I get mixed results - sometimes it appears the resulting grid ignores the second sorting order expression (i.e. ST_MinX) and only orders by the first. This occurs even if you try to alter the second condition (i.e., changing ST_MinX(geom)asc to ST_MaxX(geom)desc, or the desc/asc's flipped) – Needs_Caffeine Feb 09 '24 at 11:29