I have 3 cases shown in the image.
In all of them i want to intersect the lines and remove dangles using python in Qgis python console so afterwards i can make a plugin for this work in qgis.In case 1 i want to extend the vertical line to the horizontal line.In case two i want to extend both of the lines so that they intersect each other.In case three i want to remove the extra portion (remove dangles).
I am able to get the coordinates in the variable.
Can anybody tell me how to do this using python code?
Answer
You need to think in terms of analytic geometry or vector geometry:
I illustrate the approach with the first example (same with the others) with PyQGIS here but you can also use Shapely.
You need to create a segment in the direction of line1 and calculate the point of intersection with line2.
1) Find the azimuth of line1 (How do I find vector line bearing in QGIS or GRASS?) and project a point in this direction using direction cosines (How to create points in a specified distance along the line in QGIS?)
import math
def azimuth(point1, point2):
return point1.azimuth(point2) #in degrees
def cosdir_azim(azim):
azim = math.radians(azim)
cosa = math.sin(azim)
cosb = math.cos(azim)
return cosa,cosb
seg_start, seg_end = line1.asPolyline()
cosa, cosb = cosdir_azim(azimuth(seg_start, seg_end))
lenght = a_distance
result = QgsPoint(seg_end.x()+(a_distance*cosa), seg_end.y()+(a_distancer*cosb))
segment =QgsGeometry.fromPolyline([seg_end,result])
2) find the intersection and compute the resulting line
inter = segment.intersection(line2) # a point, in green
result =QgsGeometry.fromPolyline([seg_start,inter])
No comments:
Post a Comment