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

2 Answers 2

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

4
  • 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
    Commented Apr 3, 2019 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
    Commented Apr 3, 2019 at 11:48
  • Thank you a lot! I installed 3.4.6(LTR) on a MAC OS and it worked.
    – nat
    Commented Apr 3, 2019 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) Commented Feb 9 at 11:29
0

select *, ROW_NUMBER() OVER(ORDER BY ST_MinY(geometry) asc, ST_MinX(geometry) asc) as newID FROM Grid, my work does not work

New contributor
Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented 14 hours ago

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.