I'm going to make this one short and sweet, I was asked can QGIS (well qqfield/mergin, but the underlying question is can QGIS) limit the digitization of new features using specific spatial rules. As with most questions asking if QGIS can do something, the answer was yes.
I'm going to display this using a Within check, but you can use any type of spatial check, as long as it returns a true/false (boolean) value, meaning intersects will work, but intersection won't (it returns a geometry, not a boolean value).
I'll create a new layer with one field, whose validation is going to be a check if the point I'm creating is inside a lake (using Natural Earth's lakes layer). Since I want the data to be useful, I'll also use an expression to fill in the field with the name of the lake.
The validation is a relatively simple one, we open the layer properties and go to the Attributes Form tab and using one of the fields, in this case I only have one, but you can use whatever relevant field or just one that doesn't need any other value validation.
In the Constraints section we check the Enforce expression constraint and create a simple constraint using the expression:
overlay_within('ne_10m_lakes')
That's it, that's all you need to make sure users can only add point inside lakes. You can use other overlay_* functions for other spatial constraints as they all check against an entire layer and are very simple to use.
I can also add a default value using a second parameter for the overlay_within function, the second parameter is expression and I can use it to get a field or perform any expression on the returned results (usually resulting in returning an array.
Here I used:
overlay_within( 'ne_10m_lakes',"name")[0]
Which returned the first result of the array of the "name" field of features containing my point.
Comments
Post a Comment