There is a way to automatically assign a river_id
field to your points so that you don't have to do that manually. As it adresses a completely different aspect as in my first answer, I post it as a second answer. The only thing you have to do manually is creating a line that defines where two rivers meet (step 4).
- On your points layer, add a
symbol layer / Geometry generator / line
and paste this expression (replace 'vertices' in line 8 with the name of your points layer). The overlay_nearest
function is available since QGIS 3.16 (see refFunctions plugin for older versions):
collect_geometries (
array_foreach(
generate_series( 0,1),
make_line (
$geometry,
array_get (
overlay_nearest(
'vertices',
$geometry,
limit:=20
),
to_int(@element)
)
)
)
)
This generates a line connecting each point with its nearest neighboring point. The trick is now to succesively increase the value 1
in line 3 (in the expression generate_series( 0,1)
): this will additionally create a line to the nearest 2 neighbors, than next 3 etc. Increase the value as long until all points are connected together. In my case, I had to increase to value to 4 (see screenshots - the solution continues afterwards):
Value with 1: many gaps in the connecting red line:
*Value with 2: most point connected, but still a gap left at the left, where there is the pink colored polygon - here, points are not yet connected: *
Value with 3: still not connected in the same place as before (see arrow), but some further connections added in other places
Value with 4: finally, all points are connected :-)
Value increased to 19: too many connections, a real mess :-(
- When you found the ideal setting (in my case: 4), copy the expression, run Menu
Processing / Toolbox /Geometry by epxression
and paste the expression there. This creates the same lines as before - but this time as actual geometries (new layer) - before, we had it only for visualization purpose:
- Now apply a buffer to the line generated in step 2:
Menu Vector / Geoprocessing Tools / Buffer
- the buffer size should not be too large, it's OK to set it to a distance considerably smaller than the distance between your points. Be sure to check the box Dissolve result
What appears here as a thick blue line is in fact a polygon, the buffer created in step 3:
- Select the buffer, toggle editing and select the icon
Split features
, than make a line that crosses the buffer where the two rivers meet. Activate snapping to snap the line to the vertices where the rivers meet. When your line has the right shape, right click to finish it and the buffer will be split.
The small red line that splits the buffer, highlighted in yellow:
Result: your buffer now consists of two features - highlighted (selected) the one on the right:
- Now go back to your vertices layer and create a new field with filed calculator. Paste this expression (the
overlay_within
function is available since QGIS 3.16, see above). The expression looks for each point within which one of the buffer-features it is located and creates the belonging id as a new attribute in the point layer:
array_first (
overlay_within(
'buffer',
$id
)
)
The expression to create the river_id
field:
- Now you can use select by expression to select all the points that belong to the same river, than run
points to path
(as described in the other solution) for the selected features only. You could also use this field to run it in batch process or to create a model.
The points (vertices) are labeled with the field river_id
created in step 5. Using select by expression
, you can select all points belonging to the same river - here with the expression "river_id" = 1
:
And this is how my result looks like after applying points to path
and than lines to polygons
, using a categorized renderer for the colors (color ramp blues
):
section : sequence
) as in your example, I was able to create your desired result, see: i.sstatic.net/sPlfD.png - so the problem is only that you have the same combination ofsection : sequence
several times (for each river) and you're not able automatically keep the different rivers apart? Did I understand that right?Geometry by Expression
tool in QGIS.