# How to merge lines with slightly different endpoints in QGIS?

In QGIS, I have two lines. The endpoints are near each other, but not quite aligned. I'd like to turn the two lines into one, with the two endpoints merged into midpoints.

The closest I've been able to come is with "Merge selected features." That gives me one feature, but it's comprised of two disjoint lines -- there's no line between the endpoints.

I'm looking for something that behaves similarly to Illustrator's Join Paths feature (Object > Paths > Join). Does QGIS do that?

-

Quick answer: no! There's no tool like that to do that operation directly on the layer (the "Join Two Lines" plugin requires intersection).

You could do it for a very simple layer by turning the lines to points (extract nodes) then joining with points2one (line output) but this would be MUCH slower than just editing by hand:

1. Make sure you have snapping (Settings|Snapping options ...) turned on for your line layer (5 pixels works well for me).
2. Create a new line from one endpoint to the other.
3. Select all three lines (the two original lines + the new one) and merge them (Edit|Merge selected features or toolbar button).
-
Thanks. I only had a few lines to merge, so this was quick & easy. (@Michael Markieta's answer seemed better suited to complex situations.) –  Evan Jan 24 '12 at 19:44

You can accomplish this with a combination of QGIS and GRASS.

1. Import your vector layer into a GRASS mapset ( Grass | File > Import Vector Data )
2. Open your mapset in QGIS ( QGIS | Plugins > GRASS > Open mapset )
3. Add vector layer from your GRASS mapset to your QGIS project ( QGIS | Plugins > GRASS > Add GRASS vector layer )
4. Use v.clean.snap ( QGIS | Plugins > GRASS > Open GRASS tools > Modules List > v.clean.snap )

Adjust the v.clean.snap 'threshold' to attain proper vertex snapping throughout study area.

-

I assume you are wanting to join two line segments into one line.

This is how I did it ...

• Enable editing.
• Turn on snapping ( Settings -> Snapping Options ... )
• Select the node node tool.
• Double click close to the end of one of the lines to add a node.
• Drag the node at the end to snap onto the the end of the other line
• Select both lines
• Merge their attributes ( you cannot make therm one line if they have differing attributes)
• Merge the two features

This should result in one line.

-

If you mean that your single feature consists out of a MULTILINESTRING instead of a single LINESTRING then you can easily fix that if you are using a Postgis database:

update your_table set your_geo_column = ST_LineMerge(your_geo_column);


If you only want the 1 row to do this you should add WHERE id = your_row_id of course.

-

Also someone could try:

1. save .shp as .dxf
2. open .dxf with DraftSight
3. join polyline parts
4. reopen .dxf with qgis and save as .shp
5. from Vector menu -> Geometry tools -> Lines to polygons
-

Using OpenJUMP (Plus edition):

1. File > Open file
2. Plugins > Topology > Network topology cleaning ...
3. Right click on the new layer (fixed): Save dataset as ....

This is the result:

-

There is this QGIS plugin which says it does exactly what you want.

(However, I was not able to use it correctly in QGIS 2.6)

-