2

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 ) ))
MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
asked Sep 10, 2023 at 11:56
3
  • If you add your failing code into the question, it will make it easier for someone to help you. Commented Sep 10, 2023 at 22:36
  • I've tried so many different ones but I've now added two examples in the main text. Commented Sep 11, 2023 at 6:47
  • 1
    In my answer to this question gis.stackexchange.com/questions/381045/… I suggest a way that could be an alternative Commented Sep 12, 2023 at 15:36

1 Answer 1

4

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

answered Sep 11, 2023 at 12:29
5
  • Thank you, I appreciate your time and help! First nothing happened with the codes that you posted, but I could make the features start moving after I'd rearranged the arrays like this: if( @id=13 ,array(0,0), array ( scale_linear ( @map_scale , 3000, 30000 , 0 , -5 ) , scale_linear ( @map_scale , 3000, 30000 , 0 , 5 )) ) The only problem though is that all of the features still moves and are therefore still overlapping, so it seems that I'm still not assigning the code to a specific feature. I've tried with all three suggestions. Can you see what I'm doing wrong? Commented Sep 11, 2023 at 13:41
  • Which version of QGIS are you using? Commented Sep 11, 2023 at 21:14
  • I had some issues getting it working using an existing layer - not sure why - so I created a new layer from scratch for the examples in the screenshot above. Might be worth trying with a new layer. The examples above are using QGIS 3.30.1 Commented Sep 11, 2023 at 22:05
  • I'm using QGIS 3.32.2 and I've managed to move the symbols with your code now. Though, I'm having issues with the symbols and the scale limiting expression, but that is posted in new questions. Thank you for your help Tom! Commented Sep 12, 2023 at 12:09
  • 1
    No worries - remember to accept the answer if it solved your question Commented Sep 12, 2023 at 12:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.