Thursday 16 June 2016

qgis - Create Intersecting lines and remove dangles



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?


enter image description here



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))

enter image description here


segment =QgsGeometry.fromPolyline([seg_end,result])

enter image description here


2) find the intersection and compute the resulting line


inter = segment.intersection(line2) # a point, in green

enter image description here



result =QgsGeometry.fromPolyline([seg_start,inter])

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...