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?

  • 1
    I would think you would have success with PostGIS as the back-end, and using a trigger that would calculate the value with the spatial query you're using... Apr 4, 2016 at 14:44
  • 1
    We have Oracle as our database so I could try and do it with triggers in Oracle, but I wanted to see if I could do it using QGIS.
    – Nicola
    Apr 4, 2016 at 14:47
  • I had a similar question about how to simply fill in the time a feature was modified in QGIS, the answer was to use a trigger in the DB... Apr 4, 2016 at 14:53
  • Ok I'll look into triggers in the database then. Thanks
    – Nicola
    Apr 4, 2016 at 14:54

1 Answer 1


After lots of trial and error I found a way of doing the spatial filter and then passing the value to a custom form, with a script running alongside.

The form takes dialog, the layerid and a featureid. I used featureid to identify the geometry of the point I wanted to test against polygons, obtained the geometry of the polygons to test against and if there was a match passed the value into the field in the form.

    layer = QgsVectorLayer(uri.uri(), "TABLE_NAME", "oracle")
    feats = layer.getFeatures()
    for feat in feats:
        if featureid.geometry().within(feat.geometry()):

except Exception as e:
    msgBox = QMessageBox()
    msgBox.setText("Error: " + str(e))

This saved having to create triggers and kept the logic contained in QGIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.