I am trying to autofill values of fields in a form when adding features to a database. When the point is added there are several fields that relate to polygons that the point sit within, such as within a county shape or a postcode district.
I would like to be able to set these fields automatically with the name of the intersecting polygon by running a spatial filter with the layers that contain the polygons and the point that is being added.
I have tried to do this by using the Relative Relation option in the fields widget when adding the point, but this still requires a click on the canvas in the correct polygon to fill the box. There are 3 fields that would need to be updated this way and if several points are being added it is a lot of extra mouse clicks that it would be preferable to avoid. This method could be error prone if the user clicks the wrong polygon. Also, if I looked at the info for an existing point, these fields would be empty and would need to be set again.
I thought that I may be able to set it using Value Relation, but I cannot figure out how to make this work. I assume that I would need to the geometry of the shape that I would be testing against the point, which I would not know beforehand.
I tried to script something to add as a Python Init function (after reading this article) , but I could not get the code to work properly in the Python Console. I would get the value for every point within the layer and I cannot see how to identify the point that I am attempting to add.
Is there a better way of doing this? Or is there some code that I could use that gets the coordinates of the point that is being added to do the intersection test?