I have a large raster stream network (excerpt below) in QGIS that has been thinned using r.thin (light blue squares) and then vectorised using r.to.vect (pink lines). However, because some of the streams are close together, there are extra unwanted lines (in yellow) that are created as part of the vectorisation process.

enter image description here

Is there a process for removing these yellow lines without affecting the rest of the network?

Ideally there might be a process that only involves using the vector layer itself. However, if that's not possible, it could involve the use of one of the underlying raster layers that was used to derive the stream network (eg hydrologically controlled DEM or D8 flow accumulation raster).

The thinning process removes some cells from the raster that are part of the drainage channels (think of a T-intersection for example - the head of the T is removed, making the intersection a Y). So using the DEM to determine line direction is not accurate, because the "downhill" end of the line may be higher than the "uphill" end.

  • 1
    You can find them using line to polygon, any polygons formed in a drainage network are suspect but removing them automatically isn't so easy. If you do come up with an easy way of automating removing these I would be very interested. Feb 9, 2021 at 4:15
  • I suspect it is going to require multiple stages. The loops in the screenshot are one type of imperfection, but there are likely to be other types. Polygonize is definitely a useful first step to identify loops in the data, and hence vertices that need further analysis. I have an conceptual approach for a first stage, but it would need converting into PyQGIS. Feb 11, 2021 at 21:30
  • 2
    There will be situations, such as braided streams and islands in the middle of a stream, where yellow lines such as yours are desired. It seems to me that any automated procedure would need to take such "correct" yellow lines into account. Interesting!
    – Stu Smith
    Mar 22, 2021 at 2:53
  • 1
    How should an algorithm decide between any of the candidate yellow lines and the ones you have selected as "correct"? Any of the four segments (top set) or three segments (bottom set) could be correct. I think the solution is actually one step back from this: a DEM that has been hydrologically corrected (breaches and depression fills) such that there is only one flow path and you would never arrive at this situation. Alternatively a potential approach would be to represent the output as a directed graph in order to find nodes with more than two associated edges directed away from the node. Jun 1, 2023 at 0:34
  • 1
    Sorry I should have read that! A networkx approach is what I was thinking if you couldn't regenerate the streams from a corrected DEM. Jun 1, 2023 at 1:14

2 Answers 2


I'm documenting the steps below in case this is approach useful to someone. Unfortunately there is a fundamental assumption around the data in the question that makes this approach fail for me. See edit above.

The approach below may deal with the examples in the screenshot, which probably make up the majority of the problems in a thinned stream model. However, there are other types of loops that can appear that may need a different approach.

One approach for the first stage might be

  • identify loops in the data (Polygonize)
  • extract the list of vertices that make up those loops (Extract Vertices)
    • Add Raster Values to Points
    • possibly Remove Duplicate Vertices(?)
  • identify all lines that use one or more of those vertices. All other lines should already be OK. (Join Attributes by Location (Summary) - Intersects (don't use Contains), summary Min/Max
    • this will extract lines of interest to a separate layer
  • use one of the underlying layers (hydrologically controlled DEM) to make all lines in that layer point "downhill"
    • this can be done via Expression Builder in the Attributes (F6):
      • if the hydrologically controlled DEM layer id (not name) is DEM, then the following will select features pointing the "wrong" way. This could be done earlier
      • raster_value( 'DEM',1,make_point( $x_at(0),$y_at(0)))-raster_value( 'DEM',1,make_point( $x_at(-1),$y_at(-1)))<0
      • Reverse Line Direction (will create a new layer, with just the reversed lines)
  • starting from the highest vertex (using the hydrologically controlled DEM):
    • identify vertices that have more than one line leaving
    • for each line leaving that vertex, count the number of lines entering the vertex at the other end of that line
    • if the count is 2, mark that line for deletion
    • this step probably needs to be done via PyQGIS
      • This is a directed graph so could use NetworkX to build the graph, and any vertex with an out_degree of 2 should be removed (because stream networks should only have one outlet per vertex)

To remove unwanted connections, simply identify lines shorter than a certain threshold and delete them. Depending on how the data is structured, first convert your lines to single part features: Select all lines of the layer, toggle edit mode, Merge selected features. Then run Multipart to single parts.

  1. Use Select by expression with the expression $length <=100 to select lines shorter/equal to 100 (adapt this value to your needs).

  2. Delete selected lines - or extracty/paste them to a new layer to keep the geometries in case there are some left you still need.

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.