5

In QGIS 3, I have split a line dataset (road dataset where each road is an individual segment i.e. split at intersections) into segments using the "Split lines by maximum length" tool with the maximum length set to 100m. There are several segments (~1000) that are less than 20m and I would like to merge with a neighbouring line segment.

For example, the yellow segment below is less than 20m, while the neighbouring segments are 100m. The points represent the line segment endpoints.

enter image description here

What is the best way to merge lines less than 20m with the neighbouring line segment? I accept that this will increase my max length by 20m in some areas.

0

1 Answer 1

2

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: enter image description here

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:

enter image description here

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.