Use Geometry by expression (Menu Processing / Toolbox / Geometry by expression
; see here for more details) to create a new line layer with all line features shorter/equal 20 m are merged with the neighboring feature of the same line.
Run the tool:
- Select your line layer as
Input layer
Line
as Output geometry type
- Insert this expression as
Geometry expression
:
if (
$length <= 20,
union (
$geometry,
eval (
'overlay_nearest (''line'', $geometry, limit:=4, filter:=id= ' || id || ' and $id <> ' || $id || ' )[0]'
)
),
$geometry
)
Note:
- The expression works for newer QGIS versions, see here.
- Your line features must have a common identifier (here called
id
) that groups features belonging to the same line together so that it is clear to which of the neighboring features the short ones should be connected. This id
is not to be confused with $id
- the unique identifier for each feature (line segment of 100 or less meters).
- Replace
line
in line 6 with the name of your layer in two single qutotes ''
(not double quotes "
).
- To delete overlapping lines in the output, use Select by expression with this expression
overlay_within( @layer)
and delete the selected feature(s).
3 lines with their id, labeled above the line: 1 (orange, left to the middle), 2 (blue, from top to the middle) and 3 (pink, from right to the middle). Each feature (individual line) is labeled below the line with the individual $id
(here called fid
) and the length. Small green lines indicate the start- and end-points after the workflow. Merged lines (here lines <=70) are from line 1: fid 21+22; from line 2: fid 16+17; from line 3: fid 5+6:

Orange, blue and pink lines are created with a slightly modified expression for visualization purpose with Geometry generator.The short segments (here: <=90) are connected to the nearest neighboring segment with the same id
, but different $id
. White circles with black dot are start- and end-points of the colored lines:
