I want to sum up the attribute values from a line layer along another line layer in QGIS in a direction:

enter image description here

The values from the orange line should be summed up along the red line, which is divided into segments between the points. The orange and red lines intersect in the blue points. The values written in red should be the result in each segment of the line.

How can I sum up the values from the orange lines along the red line?

Here you can see the dataset

  • 1
    Do they have some attribute of the order they should be summed?
    – BERA
    Apr 20, 2023 at 11:43
  • @BERA there's no attribute of the order. The beginning should be on the left side and the end on the right
    – fabian96
    Apr 20, 2023 at 11:49
  • 1
    Can you share the data? The red line is one feature or five?
    – BERA
    Apr 21, 2023 at 16:31
  • 2
    ln5.sync.com/dl/270b69880/6tb55h7n-q26ydw6p-h6qfcp5x-gq799ijh here you can see the dataset. It is one line layer and the red line is where the attribute "Hauptleitung" has the value of one, so the red line consists of 5 features
    – fabian96
    Apr 24, 2023 at 11:44

1 Answer 1


Solution: the basic idea

Create the shortest path from each start/end of the side branch (blue houses in the screenshot below) to the last point on the main line (dark blue square) using the network/shortest path tool. Each segment of the main line then has a different number of overlapping lines: aggregate/sum the attribute values of these accumulated, overlapping lines separately for each segment.

For step-by-step implementation in QGIS, see below. Result with your dataset:

enter image description here

How to implement it in QGIS

Screenshot, showing the state after Step 5: the thickness of the lines represents the number of overlapping lines; the labels show the corresponding values that you sum up in Step 6: enter image description here

See at the bottom for details about the general principles of the solution and the problem of your dataset that you should clean before starting.

  1. Use Select by expression with the expression Hauptleitung = 1 and create a very small buffer (like 0.1 m) around the selected features only - we need this buffer layer in step 6

  2. Merge selected features so that Hauptleitung is one single feature.

  3. Create the start points for all Zuleitungen lines: Select Zuleitungen (with Hauptleitung <> 1 or Hauptleitung is NULL) and use Menu Processing > Toolbox > Geometry by expression. Check the box Selected features only and create a new point layer with the expression start_point ($geometry)

  4. Menu Processing > Toolbox > Shortest Path (layer to point). As Vector layer with start points, select the points from Step 3. For the end-point, manually select the last point on the main pipe. To be sure to get a route for all points, set a small topology tolerance (e.g. 0.1).

  5. Run Explode lines on the result of Step 4.

  6. Run Menu Processing > Toolbox > Aggregate. For Group by expression, use the following expression, where Buffered is the name of the buffer layer from Step 1. For the attribute Heizlast, select Aggregate Function to sum and run the tool:

    overlay_within('Buffered', $id)[0]

Details about the idea

For each of the branch lines (supply pipes, your "Zuleitungen"), create the start point (the green house icons in your screenshot, blue dots in my screenshot). From each of these points, create the shortest path along the pipes network to the end-point. This end-point should be at the end of the main pipe (your "Hauptleitung"): in your screenshot, somewhere on the red line on the right border of the image. Use the network "shortest path" tool for that. So each supply pipe after the other leads to the main pipe. Like this, along the main pipe, more and more supply lines are accumulated from left to right (towards the end-point).

Explode the resulting lines to get each segment of the main pipe between two supply pipes as a single line. Each of these segments consists of a different number of overlapping lines. Now aggregate these overlapping lines (see screenshot below), whereby you sum up the connected attribute values Heizlast to get the cumulative sum of all supply pipes: the result you're looking for.

Supply pipes dotted, main pipe in solid, thick black line; red: accumulated values of the supply line; blue: start points (step 3 below); the end-point for the shortest path tool (step 4) is on the main pipe to the right of the red 130: enter image description here

Problem: get clean data

At least in an earlier version, your data I downloaded was inconsistent and messy. For the kind of task you want to undertake, it is critical to have clean data - data resulting from CAD might not be suitable for that. Be sure to have lines snapped to each other and avoid overlapping geometries. It seems data was updated meanwhile and works fine with this solution.

E.g. the feature with id=1 contained the whole length of Hauptleitung, whereas the other sections of Hauptleitung contained just the lines between two neighbouring Zuleitungen. Also, the point where Zuleitung with fid=8 met Hautpleitung did not correspond to the start/end-point of the corresponding Hauptleistungen.

Recommendation - what you need (maybe even as separate layers):

  1. One single part line representing the main pipe. You can then explode this to get the individual segments of the main pipe.
  2. Individual lines for supply pipes, with end-points snapped to vertices of the main pipe.
  • 1
    Thank you very much for your answer and the ideas behind the solution. Works perfectly!
    – fabian96
    Apr 28, 2023 at 10:55
  • the solution works very fine with straight lines ("Hauptleitung"), but when there are curves the puffer has some problems and the result has wrong results. Do you know how to deal with curvy lines?
    – fabian96
    May 24, 2023 at 10:45
  • Not sure what exactly goes wrong with curved lines? Maybe it's just that you don't have clead data (see last point in the solution)? Maybe also try to reduce buffer size. Can't you simplify the curved lines to topologically correct straight lines (just for the sake of this task), run the solution and than join the results back to the initial, curved layer?
    – Babel
    May 24, 2023 at 13:00
  • The problem is that there are more features between the points when there are curves in the main pipe. I have the same result in every segment of the curve between two points, but I only want one result. The results in the main pipe should only be between the points where the "Zuleitungen" are and not when there is a curve in the main pipe. it's very difficult to describe, I hope you get what I mean.
    – fabian96
    May 25, 2023 at 7:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.