You can use overlay_nearest()
function that has an optional filter condition. The filter is a bit tricky, but you can use a variable @value
based on the attribute value
and than introduce it into the filter condition by concatenating it as a string and than convert it back to a function with eval()
.
Red arrowheads show the direction in whicht points are connected: 7 is linked to 5 and 5 to 7; 11 is linked to 7, too ("outgoing"), but 11 has no "incoming" connection:
You can use the expression with Geometry Generator (for visualization) or Geometry by Expression (for actual lines) - see here for differences.
The expression looks like this: you can freely change the percentage in line 3. Be sure to introduce the name of your point layer in line 11:
with_variable(
'percentage',
50, -- change this value
make_line (
$geometry,
with_variable (
'value',
value,
eval(
'overlay_nearest (
''points'', -- change to the name of your points layer
$geometry,
limit:=11,
filter:="value">=' || (@value/100*(100-@percentage)) || ' and "value" <=' || ( @value * (100+@percentage)/100) || ')'
)
)[1]
)
)
For the radial line (a Geometry Generator symbol layer):
with_variable(
'buffer_dist', -- store result of distance calculation as a variable
array_mean( -- lengths mean
array_foreach(
overlay_nearest( -- get nearest points
@layer,
$geometry,
limit:=4
),
length(
make_line(
$geometry, @element
)
)
)
),
make_line(
$geometry,
project(
$geometry,
@buffer_dist, -- insert the calculated value as a variable
radians(270)
)
)
)
For the label position (Geometry Generator placement):
with_variable(
'buffer_dist',
array_mean( -- lengths mean
array_foreach(
overlay_nearest( -- get nearest points
@layer,
$geometry,
limit:=4
),
length(
make_line(
$geometry, @element
)
)
)
),
project(
$geometry,
@buffer_dist / 2,
radians(270)
)
)
The label expression:
array_mean( -- lengths mean
array_foreach(
overlay_nearest( -- get nearest points
@layer,
$geometry,
limit:=4
),
length(
make_line(
$geometry, @element
)
)
)
)
Result:
Best Answer
You can use this expression: