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.

  • 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 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 at 21:30
  • 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 at 2:53

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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