I'm making a map of trees in QGIS (3.10). Each point represents a tree and the attribute table has a column that should contain the tree species. For now this column is empty, but I would like to fill this column. The trees are planted in a pattern, for example cashew tree - baobab tree - banana tree.

How can I assign the tree species to the table according to the pattern? So every first tree is a cashew, every second tree is a baobab and every 3rd tree is a banana. The fid of the table is in the right order, so I could build an expression with fid+0 = cashew, fid+1 = baobab and fid+2 is banana. Would that be possible?


Open the field calculator, select the function editor and make the content look like:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def tree(value1, feature, parent):

Press "Save and load functions" then in the Expression tab, type


If you want the easiest possible solutiuon, look at Erik's below, but it is less flexible if you want to add more types.

|improve this answer|||||
  • Thank you, it worked. I was also able to extend it, to include more trees in the pattern. – DerekUU Nov 19 '19 at 14:57

If you data is as organised as you say, creating the "tree column" with following expression for the field calculator should work:

WHEN $id%3=0 THEN 'Cashew'
WHEN $id%3=1 THEN 'Baobab'
ELSE 'Banana'

Short explanation: This checks whether the feature's ID is divisible by 3, and if not, what rest (% is the modulo-operator) is left, then assigns the fitting tree-type. As ID starts with 0, Cashew is assigned to all IDs with no rest.

|improve this answer|||||
  • I think you mistyped the second condition, it does not require CASE – Taras Nov 20 '19 at 13:18
  • @Taras I knew there was something fishy, thanks for pointing it out. – Erik Nov 20 '19 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.