1

I created a grid of hexagons and kept only the hexagons in a circle. In effect many hexagon-ids are missing and the remaining ones start at 1409.

enter image description here

I want them to be numbered consecutively in a logic way starting with 1 (top to bottom, left to right); i. e. 1409 should be 1, 1410 should be 2 ... 1495 should be 9, 1496 should be 10 ...

The attempt to use the expression @row_number in the field calculator as suggested by this post: Filling column with consecutive numbers in QGIS used the Feature ID + 1 for the new value, as can be seen in the column 'test'.

enter image description here

From the list of functions (https://docs.qgis.org/testing/en/docs/user_manual/expressions/functions_list.html#functions-list) I understand that it should take the row-number instead, which would result in the exact sequence I desire.

Babel
  • 71,072
  • 14
  • 78
  • 208
SAM
  • 21
  • 5
  • Not an answer but there is a sort and number plugin which you could use instead. – John Dec 30 '22 at 18:13
  • After deleting the features, did you toggle edit mode to save the changes? If not, no wonder @row_number returns the same value – Babel Dec 30 '22 at 19:02
  • @John Thank you very much, I use the "sort and number"-plugin as a workaraound. Still not the neatest solution, but it does the job. – SAM Dec 30 '22 at 19:49
  • Babel: I exported the selected hexagons as a seperate shape-file, so saving should not be a problem, I also closed and reopened QGIS many times. Also the row-numbers started with 1, so it is not that the original rows have been used, as the original hexagon 1 has been deleted. Instead I received a consecutive series of numbers 1 to 5966, just as I wanted, only their placing was weird with 1 somewhere in the middle at the bottom. At the original grid, the 1 was in the upper left corner, which I cut off. With a rectangular grid as the original one, the expression @row_number works fine btw. – SAM Dec 30 '22 at 20:01
  • 1
    I can't reproduce the behivour you describe, when using @row_number, I get an increasing no. starting with 1, see screenshot: https://i.stack.imgur.com/cFFNp.jpg Can you provide/share the exported shapefile? – Babel Dec 30 '22 at 21:01
  • @Babel When I recreated a file for you, I had the decisive idea. It seems to work just fine when I save the original grid as a shape-file. When I use it as a temporary scratch-layer and afterwards save only the hexagons selected by my circle as a shape-file, it produces the errors I described.

    Maybe a beginner's mistake. But it is good to know this for the future. Thank you anyways for your efforts!

    – SAM Dec 30 '22 at 21:53
  • @SAM You should add what solved your problem as an answer and then accept it, so that other people can find it. – Babel Dec 30 '22 at 22:03
  • I deleted your edit, as it is the same as the answer. Due to the policy of this site, mark your answer as accepted (hit the checkmark) to signalize to other users that this answer solved your problem. See: https://gis.stackexchange.com/help/someone-answers – Babel Dec 31 '22 at 11:01
  • 2
    @Babel I will do this, but as it is my own answer I have to wait 2 days to be able to do so. – SAM Jan 01 '23 at 00:15

1 Answers1

1

It turns out to work just fine, if I save every step as a shape-file. Originally I always used the original grid as a temporary scratch layer and saved only the selected hexagons which form a circle. This created the error. So just make sure to save every step as a shape-file, it is worth the effort. Many thanks to Babel https://gis.stackexchange.com/users/88814/babel for forcing me to rethink the process.

SAM
  • 21
  • 5