Save line geometry as points geometry using PyQGIS
Try out with 'intersects' and 'intersection' methods. Next code produces only two points.
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
feats_line = [ feat for feat in layers[0].getFeatures() ]
feats_poly = [ feat for feat in layers[1].getFeatures() ]
for feat_l in feats_line:
for feat_p in feats_poly:
if feat_p.geometry().intersects(feat_l.geometry()):
geom = feat_p.geometry().intersection(feat_l.geometry())
print geom.asPolyline()[0]
print geom.asPolyline()[1]
I tested it with next simple hypothetical situation:
and it works. Points printed at the Python Console were:
(386524,4.45324e+06)
(410342,4.44987e+06)
Editing Note:
With 4 lines:
running this code:
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
feats_line = [ feat for feat in layers[0].getFeatures() ]
feats_poly = [ feat for feat in layers[1].getFeatures() ]
my_list_points = []
for feat_l in feats_line:
for feat_p in feats_poly:
if feat_p.geometry().intersects(feat_l.geometry()):
geom = feat_p.geometry().intersection(feat_l.geometry())
my_list_points.append(geom.asPolyline()[0])
my_list_points.append(geom.asPolyline()[1])
print my_list_points
produces a list with 8 points:
[(387941,4.45518e+06), (410116,4.45158e+06), (385087,4.45128e+06), (411027,4.44466e+06), (383102,4.44575e+06), (408687,4.43823e+06), (392714,4.4348e+06), (405952,4.43338e+06)]