I generated a grid with 300m spacing but the ids generated from top to bottom in sequential oder. How could I get in horizontal from left to right sequence.
- 21
- 1
1 Answers
You can use the following expression that works for all kind of polygon-grids created wiht Menu vector / Resarch tools / Create grid: rectangle, diamond or hexagon (fishnet) and indifferent of cell-size (see documentation for grid-creation):
Just copy and paste this expression to where you want the grid-id to appear: be it as a text-label or as a separate field in your attributes or updating the id-field (for explanation, see below):
round (
(
maximum ( y ( centroid ( $geometry ) ) ) -
y ( centroid ( $geometry ) )
) / (
range (
y ( centroid ( $geometry ) )
) / (
count_distinct (
( y ( centroid ( $geometry ) ) ) ,
x ( centroid ( $geometry ) )
) - 1
)
)
) * (
count_distinct (
x ( centroid ( $geometry ) )
)
) + (
round (
(
x ( centroid ( $geometry ) )
- minimum ( x(centroid($geometry ) ) )
)
/ (
range ( x ( centroid ( $geometry ) ) )
/ (
count_distinct ( x ( centroid ( $geometry ) ) )
-1
)
)
)
+ 1
)
Explanation: the expression calculates the horizontal (x-)distance (in coordinates) from every cell-centroid to the leftermost (minimum) centroid, let's call it the x-offset. This x-offset is divided by the horizontal distance between cells, getting a coninuous counting of column-numbers. Horizontal distance between cells is calculated as the range (total distance from highest to lowest centroid x-coordinat value) of x-coord- values, divided by the number of columns (using count_distinct expression: how many different x-values are there?). There must be subtracted 1 (if you have e.g. 27 columns, there are only 26 distances from centroid to centroid of neighboring cell). Now you have a numbering starting with 0, if you wish the first cell to start with 1, include a +1 in the expression. So we have the first part of the expression, let's call it as a variable "column_no". This first part looks like that:
round (
( x ( centroid ( $geometry ) ) - minimum ( x ( centroid ( $geometry ) ) ) )
/ (
range ( x ( centroid ( $geometry ) ) )
/ (
count_distinct ( x ( centroid ( $geometry ) ) )
-1
)
)
)
+ 1
The result of "column_no" looks something like that:

Now with a very similar approach you get the number of the rows (lines). I added a round()-expression to get integer-values. Due to the shape of diamond and hexagon, the rows go up and down - that's why I had to introduce a "group-by"-paramenter in the count_distinct-expression to group the cells that are exactly vertically above eacht other (i.e. that have the same x-value for their centroids) - only these should count as a new row, not the "half-rows" in-between. So the whole expression to get the value of the row-number (let's call it "row_no") is as follows:
round (
( maximum ( y ( centroid ( $geometry ) ) )
-
y ( centroid ( $geometry ) )
) /
( range ( y ( centroid ( $geometry ) ) )
/
( count_distinct (
( y ( centroid ( $geometry ) ) ) , x ( centroid ( $geometry ) )
) -1 )
)
)
The result of "row_no" looks something like that:
Otherwise (without the group-by-paramenter), you would have this result:
No combining the two calculations, you get something like in the next picture, with "column-no" in red, "row_no" in black:
Now it's easy to calculate a continuous counting, let's call "cell_id", starting on the upper left to the right and after the last cell of the first line starting again on the left of the next line. You just need the number of columns, let's call it "no_of_columns" with this expression:
( count_distinct ( x ( centroid ( $geometry ) ) ) )
Now we can bring all together with this formula (paste the expressions form the steps explained above and you get the whole expression): cell_id = "row_no" * "no_of_columns" + "column_no"
- 71,072
- 14
- 78
- 208




asctodescif you want 1 at the top left corner) – JGH May 15 '20 at 19:00