I looking for solution for getting a numbering an attribute call "NAME" by clicking on polygon. I have dataset with polygon and I want to get number like 1,2,3,4... etc. I done this with simple expression in calculator, but I want to select which polygon is going to be next in numbering by clicking on it. Can I do that with some plugin on Python code?
You can achieve this using a layer
Right click on the layer, go to
actions then add a new action of type
feature, and enter the following code.
Make sure to use the proper field name instead of
pt_id at the line
fld = layer_obj.fields().lookupField('pt_id');
from qgis.core import QgsSettings s = QgsSettings(); pt_id = s.value("last_id") or 0; layer_id = '[%@layer_id%]'; layer_obj = QgsProject().instance().mapLayer(layer_id); fid = [%$id%]; fld = layer_obj.fields().lookupField('pt_id'); layer_obj.changeAttributeValue(fid, fld, pt_id+1); s.setValue("last_id",pt_id+1);
Basically, this code reads the last assigned value (or 0 for the 1st click), then adds 1 to it and save this new value in the targeted feature.
To use it, put the layer in edit mode, select the
info tool, right click on the feature of interest and click on
set id. You may have to pan the map or refresh the attribute table to see the changes.
PS: if you click twice on the same feature, the previous value will be overwritten.