I have to iterate over a point and line layer to draw the perpendicular line from each point to the nearest line. The QGIS geoalgorithm "DISTANCE TO NEAREST HUB" isn't helpful because the nearest hub isn't perpendicular.
I found this code, but how can I call the points in the Point Layer and the lines in the Line layer generally? How can I can iterate over them with a loop to find/create the perpendicular line in between?
# basic example with PyQGIS
# the end points of the line
line_start = QgsPoint(50,50)
line_end = QgsPoint(100,150)
# the line
line = QgsGeometry.fromPolyline([line_start,line_end])
# the point
point = QgsPoint(30,120)
def intersect_point_to_line(point, line_start, line_end):
''' Calc minimum distance from a point and a line segment and intersection'''
# sqrDist of the line (PyQGIS function = magnitude (length) of a line **2)
magnitude2 = line_start.sqrDist(line_end)
# minimum distance
u = ((point.x() - line_start.x()) * (line_end.x() - line_start.x()) + (point.y() - line_start.y()) * (line_end.y() - line_start.y()))/(magnitude2)
# intersection point on the line
ix = line_start.x() + u * (line_end.x() - line_start.x())
iy = line_start.y() + u * (line_end.y() - line_start.y())
return QgsPoint(ix,iy)
line = QgsGeometry.fromPolyline([point,intersect_point_to_line(point, line_start, line_end)])
Best Answer
You need a reference to the layers. I do that with QgsMapCanvas class in the next code. PyQGIS also has classes to find 'Closest Segments'. For this reason you can avoid to use your 'intersect_point_to_line' function. I used 'closestSegmentWithContext' of QgsGeometry instead.
I employed above code with next layers; where you can have two possible situations.
After running the code, at the Python Console of QGIS were printed two Line Strings in WKT format. By using QuickWKT plugin of QGIS I got:
You can observe that, in one case, second closest segment is not perpendicular.