# Extend line to specific point

I have a line layer which is cut at the level of the point layer,

I want it to be connected at the nearest point while saving the forme ( just adding the missing part the that line without moving it) as showing in the next picture

My idea is to calculate the distance between each extremity of the line with the points and take the nearest point then draw a line between them..But the drowing come out bizzarly.

I was wondering ,Is there a simple way of doing this or maybe could anyone help me with the following code?

``````from qgis.core import *
from qgis.gui import *
import processing
layer = iface.activeLayer()
feat1=layer.selectedFeatures()

for lyrr4 in QgsMapLayerRegistry.instance().mapLayers().values():
if lyrr4.name().startswith( 'Boitier_B1'):
ch = lyrr4
pr= ch.dataProvider()
chs= ch.getFeatures()
for f in feat1:
min1=10000.55
min2=10000.55
attrs = f.attributes()

idc = f["ID"]
x1=f["x1"]
y1=f["y1"]
x2=f["x2"]
y2=f["y2"]
print "id_cab"
print idc
print "x1"
print x1
print "y1"
print x1
print"x2"
print x2
print "y2"
print y2
if  "%.2f" % x1 != "%.4f" %xc and "%.2f" %x2 != "%.4f" %xc:
point1 = QgsPoint(x1,y1)
point2 = QgsPoint(x2,y2)
for ch in chs:
id = ch["ID"]
xc= ch["xcoord"]
yc= ch["ycoord"]
print "id_ch"
print id
print "xc"
print xc
print "yc"
print yc
pointc = QgsPoint(xc,yc)
geom = f.geometry()
distance = QgsDistanceArea()
m1 = distance.measureLine(pointc, point1)
m2 = distance.measureLine(pointc, point2)
print "m1"
print m1
print "m2"
print m2
fet = QgsFeature()
if m1<min1:
min1=m1
pts=pointc
if m2<min2:
min2=m2
idmin2=id
pts1=pointc
print "min1"
print min1
print "min2"
print min2
if min1<5:
fet.setGeometry(QgsGeometry.fromPolyline([pts, point1]))
fet.setAttributes(attrs)
outFeat = QgsFeature()
pr = layer.dataProvider()
outGeom  = fet.geometry()
outGeom = outGeom.combine(geom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
layer.deleteFeature(f.id())

if min2<5:
fet.setGeometry(QgsGeometry.fromPolyline([pts, point2]))
fet.setAttributes(attrs)
outFeat = QgsFeature()
pr = layer.dataProvider()
outGeom  = fet.geometry()
outGeom = outGeom.combine(geom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)