0

I am working in QGIS and I have a point layer whose labels are to be displayed left or right of the point. The formula I tried to make it work is, in label placement, selected Offset from Point mode, and built the following expression in the Quadrant:

CASE WHEN ("fid" / 6316) % 2 = 1 THEN 3 ELSE 5 END

Being 6316 the total amount of points in the layer. And 3 and 5, according to the Expression Builder, are:

[0=Above Left|1=Above|2=Above Right|
3=Left|4=Over|5=Right|
6=Below Left|7=Below|8=Below Right]

If I am correct, in this case, if the fid number is odd, it will be placed on the left-hand side, and if the fid number is even, it will be placed on the right-hand side.

At the same time, I wanted them to have 20 mm offset each side, so I built this very similar expression on the Offset X,Y:

CASE WHEN ("fid" / 6316) % 2 = 1 THEN '-20,0' ELSE '20,0' END

However, all the labels display on the same side. I have, of course, made sure that some of the fid numbers are odd and some are even, but it seems I'm missing something pretty obvious here.

1 Answer 1

1

It's always good to check the output in the calculator.

% is the modulus operator, which will work directly on the fid, without the need to divide by 6316.

enter image description here

Dividing by 6316 will probably in most cases give you a non-integer eg (1 / 6316) % 2 = 0.000158328. Which does not equal 1, and hence will end up as the ELSE clause!

enter image description here

What you want is just: CASE WHEN "fid" % 2 = 1 THEN 3 ELSE 5 END

One thing to note is that depending on your data, IDs can be negative, in which case odd ones will have a modulus of -1. So consider using abs("fid").

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.