In QGIS Plugins you'll find a 'merge lines' plugin, which at first sight seems to accomplish what you are after.
cited from description:
Simplifies the topology of a line network by merging adjacent lines
This plugin merges segments of a line network (e.g. river network) in order to simplify its topology. Two merging methods are currently available : length (a segment is merged with its longest neighbor) and alignment (a segment is merged with its best aligned neighbor).
Below you find a geoprocessing script of which I hope that it does what you want. For testing purposes I created a shapefile with a bunch of irregularly intersecting lines and no attributes (network):
The standard dialog when executing the script looks like this (in this case the result is a memory layer):
Running the script produces a 'copy' of the input data with a field 'subnet' distinguishing to which subnet a feature belongs. With a categorized style the result looks like this:
This can be dissolved using the field 'subnet'.
Create a new geoprocessing script, copy the code in the editor, save it and things should work.
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import QColor
#from processing.core.VectorWriter import VectorWriter
rubberBand = 
v = processing.getObject(lIn)
#generate a list of all features in the layer
subNets = 
flist = 
for f in v.getFeatures():
#print "making subnet ---------------------------------------------------"
if len(featureList) > 1:
#print "start finding a subnet"
fTest, fRest = findSubnet([featureList], featureList[1:])
#print "finished finding a subnet"
def findSubnet(featTest, featRest):
found = True
newTestList = 
#print "candidates: ", len(featTest)
#print "search in: ", len(featRest)
for fT in featTest:
for fR in featRest:
if not fT.geometry().disjoint(fR.geometry()):
featTest += newTestList
if newTestList == :
found = False
#print "Found (undis)joining segments"
for fn in newTestList:
if fn in featRest:
#print "removed ", fn
#print "allready removed ", fn
return featTest, featRest
fields = QgsFields()
writer = QgsVectorFileWriter(lOut, None, fields, QGis.WKBLineString, v.crs())
net = 0
for sn in subNets:
for f in sn:
#print net, f
feat = QgsFeature()
net += 1
To create a geoprocessing script do the following (I've got the german gui, so I try to translate in en):
A: Menu 'Processing' -> 'Toolbox' (appears as a dock on the right)
B: Under 'Scripts [...]' -> 'Tools' doubleclick 'create new script'
An Editor with a little toolbar appears, in wich you copy the code above. Herein you can:
C: Save the script. It appears (in this case) in the group 'Networking' or in whatever group you write in the first line of the script
##MyGroup=group. Be aware not to write blanks in the
D: Start the script with the two little gears. A gui appears (cp. above) with the in- and output layers defined in the script line 2 and 3. When saved, alteratively start the script by doubleclicking its name under 'scripts' > 'mygroup' > 'myscriptname' (if saved under