I'm working with map of plants in garden. This garden is divided into about 40 sections. Plants of one species have their number for example every Scots pine is 12 and every Common oak is 15 in field "Inventory". I want to number every plant of each species on each section in column "RecNum", for example:
This tabel is "hand-made", how to do it automatically in field calculator?
Answer
QGIS3 has a tool Add autoincremental field for this but it seems you need qgis-2.18
solution.
Someone may offer Python code. Let me suggest a Virtual Layer
(or SQLite) workflow:
Preparation:
- Open the attribute table and add an unique id field (e.g.
fid
). Save and close the table.
Virtual Layer:
Layer | Add Layer | Add/Edit Virtual Layer
- Import your layer (let me call it
your_layer
) - In the Query window, copy and paste a syntax below.
- Click on Test and if there is no error, click on OK
Query syntax is:
SELECT T1.*,
(
SELECT Count(*)+1
FROM your_layer AS T2
WHERE T2.Inventory = T1.Inventory AND T2.fid < T1.fid
AND T2.section = T1.section
) AS RecNum
FROM your_layer AS T1
Output:
No comments:
Post a Comment