# Multiple operations within Field Calculator using the same IF statement

I have 2 layers (`Point_Orien_Vert` - red points and `Point_Centroides` - green points), both are Point Geometry.

I need first to check if the distance between one feature in `Point_Orien_Vert` and one feature in `Point_Centroide` is smaller than 180 m.

The code below shows how to calculate this distance and it works inside a IF statement.

``````distance(
@geometry,
closest_point(
geometry(
get_feature_by_id(
'POINT_Centroides',
array_to_string(
overlay_nearest(
'POINT_Centroides', "fid"
)
)
)
)
, @geometry
)
)
``````

If the result is true, I need to calculate in `Point_Orien_Vert` layer the difference between the value stored in "Dip_Direct" and the value "Azimute".

Note that a feature from `Point_Centroide` will be checked more than an once time from points in `Point_Orien_vert` because the distance.

I do not know how to write this second code using self-referencing geometry to do this operation.

• The closest point of a point geometry, is the point itself. Also, the distance between geometries, is the distance between their closest points.

• "fid" attribute may or may not have the id of the feature as its value. If you delete the feature where "fid" = 1, the second feature "fid" value remains as 2, but its new id is 1.

• Anyways, you don't need its id nor its "fid" value, you need the feature itself. Use the @feature expression in the overlay_nearest function.

• Don't convert the array to string, get its first item. Even if you were getting the id, this is a number not a string.

• Store the feature in a variable for later use in the if clause.

``````with_variable( 'closestfeature',
overlay_nearest(
layer:='POINT_Centroides',
expression:=@feature)[0], --slice index 0 to get the first array item
if(
distance(@geometry, geometry(@closestfeature)) < 180, --condition
"Dip_Direct" - attribute(@closestfeature, 'Azimute'), --expression for True
NULL) --expression for False
)
``````
• +1 - A bit shorter: instead of `if` multiply the condition's result (0 or 1) with the difference of `Dip_Direct - Azimute` like this: `(distance (geometry (@closestfeature), \$geometry) < 180) * (Dip_Direct - attribute (@closestfeature, 'Azimute'))` Commented Nov 8, 2023 at 7:17
• @Gabriel De Luca thank you very much to help me with this solution and to teach me a bit more about Field Calculator. It works perfectly for my purpose. Commented Nov 10, 2023 at 14:09
• @Babel thank you very much too because you improve the solution and it fits perfectly. Thanks to teach me more about Field Calculator. Commented Nov 10, 2023 at 14:12