# Finding number of lines touching intersection point in polyline layer using QGIS

I am trying to calculate the number of lines touching an intersection point to calculate the number of 3-way, 4-way (etc) intersections within my polyline layer. E. g. when 4 lines are connected to an intersection point, I expect the result to be '4' (etc.).

So what I imagine is the attribute table for the intersection point layer that displays for each point the "type of intersection" or "number of touching lines", so I can afterwards calculate the total number of different intersections.

What I have tried so far is the "split with lines" tool to make sure, that each line is split by each intersection point.

I added a new attribute to the intersection point layer ('type') and used the following code in the field calculator:

``````aggregate('line_layer', 'count', \$id, intersects(\$geometry, geometry(@parent)))
``````

Unfortunately, the results are either '0', '2', or '4', but only randomly correct.

Do you know a workflow using Python or the available tools in QGIS?

I am using QGIS 3.32.3-Lima.

UPDATE I

Using the expression from the answer below I am getting better results, but there are still some miscalculations. As you see in the newly added image, at the "3-way-intersections" (3 lines touching an intersection point) the result is "4". Also, on the lower left side, there is an intersection with 5 connecting lines (result=2) and so on. The labels in the image show the results. Am I missing something?

UPDATE II

I am still working on the issue. My exact workflow looks like this:

1. Create the line layer
2. Create line intersections (Vector --> analysis tools --> line intersections)
3. Split with lines (line layer is split at intersection points)
4. field calculator (active layer is the "Split" line-layer): create "type" integer field using:

array_length( overlay_touches( 'line_layer', \$geometry ) )

Remaining problems:

1. wrong calculations (see results in the image "0" or "2 / 3")
2. duplicated results (in your sample image there is only one result displayed at one intersection; In my case, the intersections are duplicated after calculation). Am I missing a step in between to avoid duplication?

• Could you share your data? I think there's some issue with it that I did not take into account for my solution (see my edited answer including a screenshot). Commented Nov 13, 2023 at 9:39
• By the way: What are "2-way intersections"? :) Commented Nov 13, 2023 at 9:59
• What does "Create the line layer" mean in your UPDATE II process description? Do you create a line layer by drawing manually in editing mode? Or do you use a specific processing algorithm to generate the line layer (e.g. out of a larger network) to be used for the next steps? Commented Nov 27, 2023 at 12:50
• In any case: Try "Dissolve" on your created line layer after step 1. Does this lead to a better or even correct result? Commented Nov 27, 2023 at 12:53
• Thank you so much! It helped! Now it works perfectly :) Commented Dec 19, 2023 at 8:03

To count the touched split lines for each point, `overlay_touches()` can be used in combination with `array_length()`.

The expression for creating the `"type"` integer field then looks like this:

``````array_length(
overlay_touches(
'line_layer',
\$geometry
)
)
``````

Here's a screenshot of an example result with correct type counts as expected (no "2" results).

To test my workflow with exactly the network data shown in the screenshot, do the following:

7. Use Field calculator to add the `type` field with the expression as described above, but with `'line_layer'` replaced by your split layer name
8. Select `type` field as label and you should see only numbers >= 3, probably with 3 and 4 quite often, no or few higher numbers, depending on your network structure