Skip to main content
wrap long code line
Source Link
JGH
  • 43.1k
  • 3
  • 46
  • 93
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),
                     QgsField(POINTID1, QVariant.Int),
                     QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(),
                                                        geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),
                     QgsField(POINTID1, QVariant.Int),
                     QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(),
                                                        geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
Try to force python syntax
Source Link
JGH
  • 43.1k
  • 3
  • 46
  • 93
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "
Source Link
JGH
  • 43.1k
  • 3
  • 46
  • 93

You can get this result using a python script that loops twice through the point layer, fetch the two points and construct a line. The solution below removes duplicates (that it, line A -> B is consider the same as B -> A).

The output has the same CRS as the input. Make sure to edit the constant with the proper point ID column name

Point connector output

from PyQt4.QtCore import QVariant

#Purpose: using the selected point layer, create a new scratch layer
#         containing a line between each pair of points and record the point ID

#***********Constants
#
SOURCEID = 'ID'

LINEID = 'lineID'
POINTID1 = 'pointID1'
POINTID2 = 'pointID2'
#
#***********

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

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

#add the two point ID field
outdp.addAttributes([QgsField(LINEID, QVariant.Int),QgsField(POINTID1, QVariant.Int),QgsField(POINTID2, QVariant.Int)])
outLayer.updateFields()


#get the field index
srcIDfldidx = srclayer.dataProvider().fieldNameIndex(SOURCEID)
outLineIDfldidx = outdp.fieldNameIndex(LINEID)
outID1fldidx = outdp.fieldNameIndex(POINTID1)
outID2fldidx = outdp.fieldNameIndex(POINTID2)

lineIDcnt = 0

#loop through the points (1st point)
for feat1 in srclayer.getFeatures():
    geom1 = feat1.geometry()
    
    #statement for selecting the 2nd points with a greater ID to avoid duplicates
    # to keep them, change the expresion to ' <> '
    exp = QgsExpression(SOURCEID + ' > ' + str(feat1.attributes()[srcIDfldidx]))
    request = QgsFeatureRequest(exp)
    #Select only the points having the specified value
    feat2lst=srclayer.getFeatures(request)
    
    #loop through the points (2nd point)
    for feat2 in feat2lst:
        lineIDcnt +=  1
        geom2 = feat2.geometry()
        
        #create the line
        connector = QgsFeature(outLayer.pendingFields())
        connector.setGeometry(QgsGeometry.fromPolyline([geom1.asPoint(), geom2.asPoint()]))
        
        #record the IDs
        res = connector.setAttribute(outLineIDfldidx, lineIDcnt)
        res = connector.setAttribute(outID1fldidx, feat1[srcIDfldidx])
        res = connector.setAttribute(outID2fldidx, feat2[srcIDfldidx])
        
        #add the feature to the layer
        res = outdp.addFeatures([connector])

# Add the layer to the Layers panel
QgsMapLayerRegistry.instance().addMapLayer(outLayer)

print "Done .. "