7

Problem: In QGIS I would like to automatically "connect" several lines to a node that is close to them by extending the lines. The objective is that the coordinates of the vertex of the lines coincide with the coordinates of the existing node. I need to apply this to several cases.

Research: I have tried "connect nodes to lines" from the plug-in "networks" but that only connects the line that is the closest to the node, not to all of them. Also running it multiple times does not help.

Application: The lines are actually power lines and the node represents a secondary substation. I am bulding power system model. For this I need to know from which node to which node the lines go. In reality, the lines are connected to the same busbar in the substation but in the GIS model there is a gaps between the lines and the substation. For further analysis and calculations, I will export the data and need the lines properly connected to the nodes.

enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
Julian
  • 71
  • 1

1 Answers1

7

Use the following expression with Geoemtry Generator (visualizaiotn only) or Geometry by Expression (for actual geometries) on the line layer. See here for the difference between both options.

Option 1: Connect lines to the closes point

This connects the lines to the closest point. Change nodes in line 5 with the name of your point layer:

with_variable(
    'node',
    overlay_nearest ('nodes', $geometry)[0],
    make_line (
        closest_point (
            $geometry,
            @node
        ),
        @node
    )
)

Red lines created by the expression: enter image description here

Option 2: Connect lines to the a specifically defined point

If you have several points, but want to define for each line to which one it should be connected, create an attribute connect_to on your line layer that contains the value of the attribute id of the point feature you want to connect to. Like this, you can control for each line to which point it should be connected. Use this expression and replace 'nodes', 'id' and "connect_to" on line 4 with your layer/fieldnames:

with_variable(
    'node',
    geometry(
        get_feature('nodes','id',"connect_to")
    ),
    make_line (
        closest_point (
            $geometry,
            @node
        ),
        @node
    )
)

Define in the attribute table the node to which the lines should be connected; the layer nodes must have an attribute id: enter image description here

Variant: Restrict extended lines to connect to end of existing lines

In some cases, the extend lines might not connect to the start-/end-point of the existing lines:

enter image description here

To avoid this, replace $geometry in line 6 (option 1) respective line 8 (option 2) with

union (start_point($geometry),end_point($geometry)):

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208