Saturday, 24 August 2019

field calculator - Number each value in each section due to other value


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:


example of result I want to get


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:



  1. Open the attribute table and add an unique id field (e.g. fid). Save and close the table.


Virtual Layer:



  1. Layer | Add Layer | Add/Edit Virtual Layer

  2. Import your layer (let me call it your_layer)

  3. In the Query window, copy and paste a syntax below.

  4. 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:


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...