I have a points layer in QGIS defined by a csv file in the following format:


(lat and long are coordinates in WGS84)

How can I draw all of the possible lines between all of the points that are in the same group? So the above data would yield a triangle with vertices at A, B and C, a line with end points A and D, and another line with end points E and F.

I have tried a number of solutions suggested on these boards (PointsToPaths plugin, Points2One plugin, Connect Points plugin, GRASS v.to.lines, SAGA Convert lines to points) but all of these only draw the minimum number of lines to connect the points. This works fine when only two points need to be connected, but fails when there are more than two points. In the example above, group 1 comes out as a line from A to B to C, without the direct line from A to C. Sometimes there are up to eight points in the group, which should yield 28 lines.


1 Answer 1


I have achieved this by modifying the code linked to by JGH.

    from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points in a group defined by an attribute in original point layer

POINTID = 'Mint'

srclayer = iface.activeLayer()
crs = srclayer.crs().toWkt()

# Create the output layer
outLayer = QgsVectorLayer('Linestring?crs='+ crs, 'connector_lines' , 'memory')
outdp = outLayer.dataProvider()

#add the ID field to new layer
outdp.addAttributes([QgsField(LINEID, QVariant.Int)])

#get the field index
srcLineIDfldidx = srclayer.dataProvider().fieldNameIndex(LINEID)
srcPtIDfldidx = srclayer.dataProvider().fieldNameIndex(POINTID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()

    #statement for selecting the points in same group
    exp = QgsExpression(LINEID + ' = ' + str(feat1.attributes()[srcLineIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value

    #loop through the points in same group
    for feat2 in feat2lst:
        geom2 = feat2.geometry()

        #create the line
        connector = QgsFeature(outLayer.pendingFields())

        #record the line ID
        res = connector.setAttribute(outLineIDfldidx, feat1.attributes()[srcLineIDfldidx])

        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel

print "Done .. "

This creates a directed network (where A-B is considered different from B-A), and includes self-loops (A-A). To remove these, use Vector -> Geoprocessing Tools -> Dissolve, using the field 'group'. This leaves one feature in the new layer for each group in the original layer, connecting all points to each other.

Your Answer

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

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