QGIS Expression Builder – Assigning Specific Features in Expression Builder

expressionqgis

I have a question regarding "Making tiny objects visible for their location when zooming out" in QGIS: Making tiny objects visible for their location when zooming out
or more specifically how to assign a specific feature in expression builder.

I'm having the issue that two features, which are close to each other, will be overlapped when the scale is changed. Therefore I want to change the position for one or two specific features (in both x- and y-direction) when zooming out.

This is two different features at a scale of 1:2000

This is in a scale of 1:15000 and the two features are overlapping each other

My main code for this is as follows

array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 ) )

But it only works for all the features in the layer and I want it to only work for one or two of them. I've tried to assigning this expression for one specific feature within the layer but can not make it work.

First I thought that the expression itself actually was assigned to a specific feature and that you could give different expressions for each feature just by changing the features in the express builder, but they all get the same expression.

So instead I've tried assigning the expression to a specific feature in the code. I've tried assigning it by both id (@id = '13') and by name (@feature = "Diket") separately, but can not make it work. I keep getting the error:

syntax error, unexpected NAME, expecting $end

As you probably notice I'm quite new at both coding in python and working in QGIS and I understand that this probably is basic knowledge, but since I can't make it work I still have to ask.

How would you type the code to assign this displacement to a specific feature?

Here are two different examples of what I've tried:

 @id = '13' (array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 ) ))

and

 @feature = "Diket" (array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 ) ))

Best Answer

You didn't state it in your original question, but I assume that you are using the expression above:

array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 ) )

in the Data defined override for the Offset of the marker.

strong text

You can use an if clause to set the offset only for a particular id:

if( @id = 1, array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 )), array(0,0) )

enter image description here

enter image description here

You can modify the clause to match a particular attribute eg name:

if( attribute(@feature, 'name') = 'Point1', array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 )), array(0,0) )

And if you want to check more than one id, you can use array_contains eg:

if( array_contains(array(1,2),@id),array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 )),array(0,0) )

enter image description here

Related Question