I have line and polygon layers. I want to make a list of points' coordinates (ends or begginings of lines), which are touching the polygons. The coordinates which I need are selected as yellow.
With the code I have right now I get coordinates of the whole line. How to change it? Here is the part of the code and visualisation of my result:
lineFeatures = lines.getFeatures() lakes= for lineFeat in lineFeatures: lineGeom=lineFeat.geometry() polygonFeatures=polygons.getFeatures() for polygonFeat in polygonFeatures: polyGeom=polygonFeat.geometry() if lineGeom.touches(polyGeom): lakes.append(lineGeom.asPolyline()) lakes.append(lineGeom.asPolyline())
The points in the pictures are there just for visualization. In my project, I have only lines and polygons.
I am working with QGIS and python.
From one of the answers, I found a solution. The code below do exactly what I wanted.
lakes= lineFeatures = [feat for feat in lines.getFeatures()] polyFeatures = [feat for feat in polygons.getFeatures()] for lineFeat in lineFeatures: for polyFeat in polyFeatures: if polyFeat.geometry().intersects(lineFeat.geometry()): geom = polyFeat.geometry().intersection(lineFeat.geometry()) line_intersection = geom.asPolyline() if line_intersection: lakes.append(line_intersection) lakes.append(line_intersection)