# Drawing line to the nearest point following a attribute hierarchy

With a point layer and a attribute table formed by the field "HIERACHY", where there are 3 values (3,2,1). The meaning of the values is from highest to lowest relevance in descending order: 1 highest relevance, 2 intermediate relevance and 3 lowest relevance:

I would like to draw the lines to the nearest point following the hierarchy order. I show a simulation of what it should look like:

Use this expression:

``````with_variable(
'curr',
hierarchy,
make_line (
\$geometry,
eval(
'overlay_nearest(
''point'',
\$geometry,
filter:=hierarchy-1='  || @curr  ||
')'
)[0]
)
)
``````

• It is possible to adapt the structure of this same expression to work with string fields (Z to A) and have the possibility to sort in ascending or descending order? Commented May 23, 2023 at 6:44

For your follow up question about connecting the points based on an alphabetic instead a numerical hierarchy, there are at least two principal ways to do that, depending if you have only one character (`'A'`) or a text string consisting of several characters (`'Anna'`).

### Alphabetical order consisting of just one letter

The easiest is if you have just one letter (`'A'`, `'B'` etc.). Convert this letter to it's ASCII code using `ascii()` function, then you have a number and you can proceed as in the other solution. Simply replace `hierarchy` on lines 3 and 10 with `ascii(hierarchy)` and you're done.

### Alphabetical order from text strings with several characters

If you have whole text string attributes (`'Anna'`, `'Berta'` etc.) for the points an want to connect them alphabetically, it's a bit more complicated and you have to use another approach as follows below.

Screenshot: each point is connected to the closest point with the next value alphabetically; so `Berta` is alway connected to the closest `Cleopatra`, even if a `Diana` or `Anna` is closer:

1. Create a new attribute that for each feature creates the next alphabetical entry. To do so, aggregate all `hierarchy` values in an array (`array_agg()`), reduce it to unique values (`array_distinct()`) and sort it alphabetically (`array_sort()`). Then identify the position (index) of the current `hierarchy` value in this array (`array_find()`) and get the next (index+1) value in the array (`array_get()`). Use this expression with field calculator to create the field named `next`:

`````` with_variable(
'distinct',
array_sort (array_distinct (array_agg (hierarchy))),
array_get (@distinct, array_find (@distinct, hierarchy) +1)
)
``````
2. Draw the lines with the following expression. Be sure to include all single quotes with two `''` and three `'''` repetitions exactly as in the expression to work correctly:

`````` with_variable(
'next',
next,
eval('
make_line(
\$geometry,
overlay_nearest(
''point'',
\$geometry,
filter:= hierarchy = ''' || @next || '''
)[0]
)
')
)
``````
• Always learning and expanding knowledge of complete and clarifying answers. And also surprised by the possibilities of QGIS. I suppose there are limitations but it seems that with genius and creativity, any idea is possible to materialise Commented May 24, 2023 at 10:08
• Could you share a little explanation of how to get the Arrow effect? It seems that in the expression you add the function "line_substring(@ll,0,length(@ll)*0.98)" to modify the size of the Head arrow depending on the length. It is quite interesting to show this effect Commented May 24, 2023 at 17:20
• As a detailed description would be too long here, I posted this as a separate question/answer - see here: gis.stackexchange.com/q/460323/88814 Commented May 24, 2023 at 20:28