0

I'm using QField to collect geologic data in the Outback.

I'm creating point data, with various fields for rock type etc.

Occasionally I take samples of the rocks, and want a field that auto-increments the sample number. The problem I have is that most entries do not require a sample, so a simple '$rownumber' query is not suitable.

What I'm picturing is a field for "sample_taken?" 'Y'/'N', and if 'Y' then an auto-incremented number is inserted continuing from the last sample number I used. For example:

enter image description here

Thanks in advance!

2

You can try using something like the expression below as a default value for your Sample Number field in QGIS (see docs for info on default values)

But do not enable 'Apply default value on update' as you don't want your sample numbers to change if you go back and modify some other feature in the record.

I assume here you want to retain the text format with zero-padding of the numbers.

CASE WHEN "sample_taken" = 'Y'
THEN 
lpad(maximum(to_int(coalesce("sample_no",0)))+1,4,'0')
ELSE 
NULL
END

However you may not see this work properly in QField as there seems to be a limitation on default values being created on insert when they rely on a value from another field in the feature being digitised (see issue #914 on GithHub)


If you encounter the above issue, one workaround for your case could be to collect your sample records on another layer.

Either create a separate file altogether, or duplicate the original layer reference in the layer panel (i.e., two references to the same file), with the original filtered to "sample_taken" = 'N' (Non-sample layer) and the other filtered to "sample_taken" = 'Y' (Sample layer).

Then set up the default value for Sample Number, but only for the Sample layer, using just the third line in the above expression (so there is no reference to "sample_taken").

When you are digitising in the field and you know you'll collect a sample, you need to switch to editing the Sample layer and then input your data; the Sample Number should then autoincrement only within its own layer, without looking at records from the Non-sample layer (i.e. records where "sample_taken" = 'N')

| improve this answer | |

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.