19

I want to automatically split all lines in a QGIS layer at their vertices (like this).

I've done a Google search and found this article, which is quite close but the workflow is complicated. Because I'll have to train my co-workers (who are unfamiliar with GIS) to use this tool, I want something as simple as a plugin or something that doesn't require GRASS.

Since it's been a long time from 2010 - the time of the previous answer, I wonder if there is any better way to achieve the same result?

Taras
  • 32,823
  • 4
  • 66
  • 137
Cao Minh Tu
  • 1,994
  • 4
  • 19
  • 33
  • You can use GRASS tools from QGIS nowdays.Maybe Vector->Geometry Tools->Single parts to multipart could help. Sextante toolbox should have "Explode line layer" And there is plugin "split feature" – simpleuser001 Mar 20 '13 at 07:43
  • 1
    I think that the answer you referred to is (still) the correct procedure. The vector tool "Single Part to Multipart" will not do what you want. It splits a multi-linestring at the nodes, not at each vertex. – Micha Mar 20 '13 at 09:52
  • @simplexio: with Sextante toolbox, I received an error message as in Bernd V. answer and I can't find the 'split feature' plugin anywhere on the net. – Cao Minh Tu Mar 21 '13 at 01:39
  • @Micha: that procedure may be correct but it will be too complex for my fellow co-workers - who are unfamiliar with GIS, so I'll consider it as the last resort, when all else failed :) – Cao Minh Tu Mar 21 '13 at 01:41
  • @Cao Minh Tu: i tested 'Split feature' again on Linux Master and on Win7 1.8 with a newly created line layer, and there it works. Please create a test layer from scratch and apply it there. Maybe there are errors in your original layers which have to be resolved first !? – Bernd V. Mar 22 '13 at 11:27
  • @BerndV.: 'Split Feature' works well when the source layer is a shapefile but returns an empty layer when the source is in spatialite (as in my previous case). Your answer marked as accepted. Thanks a lot! – Cao Minh Tu Mar 23 '13 at 04:33

3 Answers3

34

Try the "Explode lines" from the Processing Toolbox (Ctrl+Alt+T) will split all lines at their vertices to separate lines.

Menu Processing -> Toolbox. Type "explode" into the search field to find the function. Select your line layer from the dropdown and start. This will produce a new layer.

Selecting one or more lines from the line layer will produce a splitted layer with only these features, discarding the rest.

Note: As QGIS is moving fast, all infos below are outdated already.

I think Explode lines would be the the noob-solution through the SEXTANTE toolbox. QGIS Geoalgorithms > Vector geometry tools > Explode lines. Unfortunately, it does not seem to work at the moment, i get an error message only.

Traceback (most recent call last):
  File "path/.qgis//python/plugins\sextante\core\GeoAlgorithm.py", line 145, in execute
    self.processAlgorithm(progress)
  File "path/.qgis//python/plugins\sextante\algs\Explode.py", line 58, in processAlgorithm
    features = QGisLayers.features(layer)
NameError: global name 'layer' is not defined

A working solution is the Split Feature-Plugin from the "contributed" repository, which does the job for me.

In general I prefer sextante functions over separate plugins, cause the workflow with sextante is so much faster and produces no junk-files of intermediate steps (provided it works).

EDIT: Unfortunately, a lot of plugins were not moved to the official plugin repository yet, but are still only available through the "contributed" plugin repository: http://pyqgis.org/repo/contributed, which was removed from the default repositories list with version 1.8.

There you can download them individually and unzip them in your .qgis/python/plugins folder OR you add this address to your plugins repository list: Plugins -> Fetch python plugins -> Tab "Repositories" -> Add ... -> enter a name and the url.

Taras
  • 32,823
  • 4
  • 66
  • 137
Bernd V.
  • 3,179
  • 25
  • 49
  • Can you tell me where to find the 'Split Feature' plugin - I can't find it in the official plugin repository? With SEXTANTE Explode lines, I received the same error message as yours - there's a bug report here: http://hub.qgis.org/issues/7157 – Cao Minh Tu Mar 21 '13 at 01:15
  • @Cao Minh Tu: sorry, completely forgot that i added the old repository to my list, where this plugin resides exclusivly. see my edits. – Bernd V. Mar 21 '13 at 18:18
  • I tried the Split Feature plugin, there's no error message but the output layer contained nothing. – Cao Minh Tu Mar 22 '13 at 08:14
  • There is only a 404 error at http://pyqgis.org/repo/contributed - has the URL changed? – til_b Sep 29 '14 at 10:05
  • @til_be see my edit – Bernd V. Oct 01 '14 at 09:37
  • this one removing z values from the exported shape file. – Tamas Kosa Mar 13 '19 at 09:14
  • @TamasKosa Never worked with z-values in shapes. I think they are still a weak point in QGIS. Did you try to use another format like SpatiaLite or Geopackage for the output? Maybe its worth a bug report in https://issues.qgis.org/issues/ ? – Bernd V. Mar 13 '19 at 22:13
  • @BerndV. the GRASS solution below, keeps the Z value. We've tested it, hope it helps to others as well. – Tamas Kosa Mar 14 '19 at 17:05
5
  1. Open/Create GRASS mapset (so you can run the tools on your data)
  2. Open GRASS tools / Import the shapefile you want to split into the GRASS database
  3. Run v.split.vert (set maximum number of vertices = 2)
1
lines = QgsProject.instance().mapLayersByName('layer_name')[0]

segment_list = []

for feature in lines.getFeatures():
    vertices = feature.geometry().asPolyline() # returns list of vertices comprising polyline
    for point in range(len(vertices)-1):
        segment = QgsFeature()
        segment.setGeometry(QgsGeometry.fromPolyline([QgsPoint(vertices[point]), QgsPoint(vertices [point+1])]))
        segment.setAttributes(feature.attributes())
        segment_list.append(segment.geometry())

print(segment_list)
Taras
  • 32,823
  • 4
  • 66
  • 137
Steve
  • 361
  • 1
  • 5