I created the yellow area as a shapefile by hand. Then I calculated the perimeter. I also created another shapefile (blue) manually and calculated the length, see image 1.

The next step was to subtract the perimeter of the yellow area with the length of the blue line and add the remaining green length (total green length - blue line) to calculate the length of the purple line, which was created manually, see image 2.

Does anyone know if there is another way to calculate the purple length? I tried using Vector > Intersection to automatically generate the yellow area. Unfortunately, this did not work (the layer was empty). After that, I used Vector > Union. But I have no access to the yellow area, see image 3.

I am at a loss at the moment.

enter image description here

I discarded the approach with Union and Intersection and instead applied split the line for both shapefiles. Then examined the attribute tables and created a query using a Virtual Layer.

SplitContourLine returns five values where only one is relevant (row 1), see image 4.

I solved the problem by taking out all 0 and greater 5000 values, see image 5.

Now I want to access this inner line without using this condition c.ContourLen < 5000 to be more flexible when using this method for other examples.

enter image description here

  • Sharing your data could improve chances to get an answer. Otherwise, one first has to create own test-data.
    – Babel
    Commented Apr 26, 2021 at 9:30
  • dropbox.com/sh/vnl0wxcw4zhdegn/AAAhvgEq6NbIfjc7ro87jIOza?dl=0 I am currently using "split with lines". See SplitLine and SplitContourLine. Now I am experimenting how to calculate the purple line based on the partial information
    – Madeira
    Commented Apr 26, 2021 at 9:44
  • If you already have the splitted lines, you can simply delete the segments you don't need, calculate the length of the remaining ones and add them. Or do I miss something?
    – Babel
    Commented Apr 26, 2021 at 10:38
  • Select the purple line manually, than calculate the length with field calculator, just for the selected segment.
    – Babel
    Commented Apr 26, 2021 at 10:50
  • Sorry, still don't understand the problem: why can't you select the segment manually and calculate it's length? Maybe I overlook something?
    – Babel
    Commented Apr 26, 2021 at 10:53

2 Answers 2


If you have one line layer for the contour and one straight line called 'distance', split the contour layer with the 'distance' line. Then you have a new 'contour_split' layer with three segments: one between the intersecting points and each before and after the points.

Then create line intersection points that create a layer called 'points'. Now the segment you're interested in is the one that touches both points (the other two segments will touch only one of the points).

To achieve this, use "Select by expression" on the 'contour_split' layer with this expression - the overlay_touches() function is available since QGIS 3.16:

array_length(overlay_touches('points', $id, limit:=2)) = 2

Screenshot: the black contour line (divided into three segments based on the crossing points) is crossed by the red 'distance' line. Select by expression returns the segment you're interested in (highlighted in yellow): enter image description here


It seems what you want is length calculation of connected line. My answer would be to use network anlysis toolbox. Now it is really strong in QGIS and it will do both : calculate the length + create a line feature.

  1. Get all your geometry part in one layer.
  2. For segment that are crossing each other connect vertices to make the layer "routable" (use magnet or
  3. Use shortest path point to point algorithm. Use your routable layer (or even a selection of your routable layer) and define start and end point. Let the other option blank and execute.

It would work for your example and could be adaptable for projects like that. You can select only some features to be used for network, it will force the route calculation to go the way you want.

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.