25

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?

9 Answers 9

14

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.

12

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).
1
  • 1
    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, 2012 at 19:44
10

I assume you are wanting to join two line segments into one line. enter image description here

This is how I did it ...

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

This should result in one line. enter image description here

1
  • All fine. But how about a dataset of 22,000 lines? I cannot go about doing this manually...
    – FaCoffee
    Jan 17, 2017 at 9:50
6

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

https://plugins.qgis.org/plugins/joinmultiplelines/ https://github.com/dgoedkoop/joinmultiplelines

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

1
  • Been working great in 3.8
    – user94812
    Oct 17, 2019 at 20:35
4

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.

2

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
2

I just did this with QGIS 3.4.0, using the plugin Join Multiple Lines mentioned by tnagel above (installs under Vector menu > Join Multiple Lines). Worked well to join about 60 trail line fragments into about 20 trail lines. The more fragments you have and the larger the area they cover, the less practical this is, as you have to watch the results for problems (tnagel may have had one of the problems I describe).

You select the lines you want to join, click the menu item and it joins them (you can select as many lines as you want, but...), even if the ends are a mile apart, you get a straight line from the end of one to the end of the other connecting them, so make sure your ends are close enough so you will be happy with the results (I used the tip in another post to mark the ends of my lines with markers, so they were easy to see (Properties > Symbology > add a marker line and make the symbol large enough to see easily > select the radio button "on last vertex only" (and if you want, add another marker line with a different symbol and select "on first vertex only")).

Also, lines can apparently be directional. So you have to watch (for example) that the southernmost end of line A did not join with the southernmost end of line B below it (another long straight line where you do not want it).

I found when that happened I could "undo" and use "Reverse line" in the Advanced Processing Toolbar to flip the direction of a line from one end to the other (does not change where the line is, just flips the start of the line with the end of the line), then join the lines again.

A good thing about this is your changes/edits are saved to your existing layer so no need to save the results as a new shapefile (make a backup first).

I am constantly amazed at the work done by QGIS's core and plugin developers. Shows what smart constructive humans can do when they decide to work together. Thank you!

0

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

0

In my case the lines were in different layers and with different attributes. I merged the layers with mmqgis and then merged the lines in the next step with the technique of snapping and merging lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.