< All
...
Export

13 Semi-automatic numbering of objects

In QGIS you can number the field “treenumber” consecutively in the field calculator, starting from a certain number and with leading zeros, by combining the functions rownumber() and lpad().

Here is an example of what this could look like:

Step 1: Consecutive numbering starting from a certain number

To start the consecutive numbering with a specific starting number, you can use rownumber() and add the starting number. For example:

@row_number + 100

This adds 100 to each line number so that the numbering starts at 101.

Step 2: Leading zeros for correct sorting

You can use the lpad() function to prepend zeros to ensure that the values are correctly aligned for sorting. The lpad() function has the following format:

lpad(string, Länge, Zeichen)
  • string: The value you want to format (e.g. @row_number + 100).
  • Length: The total length of the result, including the zeros.
  • Character: The character you want to use for padding (e.g. 0).

Step 3: Combination of both functions

The combination of the functions for consecutive numbering, which starts at 101, and a two-digit output with leading zeros looks like this:

lpad(@row_number + 100, 5, '0')

This means:

  • The numbering starts at 101.
  • The result has a length of 5 characters.
  • Zeros are inserted before the number if it is shorter than 5 characters.

Example:

If @row_number has the value 1, lpad(@row_number + 100, 5, '0') gives the value 00101.

Complete example in the field calculator:

If you want to update the “treenumber” field, you can enter this expression in the field calculator:

lpad(@row_number + 100, 5, '0')

This generates consecutive numbering from 101, which is always 5 digits long (e.g. 00101, 00102, 00103, etc.).