# Produce line from components, length and angle

I have a CSV file, or point data file which includes information about line vectors which I have previously calculated. This file is outlined as such:

``````x, y, dx, dy, length, angle(east_from_north)
1,1,1,1,1,1
2,2,2,2,2,2
...
n,n,n,n,n,n
``````

Where `x, y` are the co-ordinates of each point; `dX, dY` are the Cartesian components of the line; `length` is the length/magnitude, and `angle(east_from_north)` is the bearing of the line (east 0degrees, north 90degrees).

I also have the the original set of `x, y` data from which I calculated the `dX, dY` components, `length` and `angle` using some python trig, if that would help this process. For example:

``````x1, y1, x2, y2
1,1,1,1
2,2,2,2
...
n,n,n,n
``````

I would like to produce lines from each point using the `x, y` values as the originating point with the `length` and `angle` creating the length and direction of the line.

I have used a QGIS plugin, `Vector Field Renderer` to produce lines with arrowheads depicting the 'magnitude' and direction using this information which has been useful for one part of my analysis; however this does not transform the point data into polylines, only produces a line symbol -- for example: Effectively I would like to reproduce this output, but as a polyline file for which I can do further analysis on. The line does not need to have arrow heads.

Edit: Solutions involving QGIS, GRASS GIS, GDAL, ArcGIS or Python/ ArcPy would be greatly received. Though, as I am currently learning (and a bear of little brain) a well-commented/ walkthrough-esque solution would be amazing!

• A solution in Python with PyQGIS is presented in How to create points based on the distance and bearing from a survey point? – gene May 10 '14 at 15:33
• Thank you, however this seems to involve creating the final points, and does not plot the vector between the two as a polyline within a shapefile. Or have I missed something? – MACooperr May 10 '14 at 15:39
• It is easy to create the line between two points with PyQGIS – gene May 10 '14 at 16:08
• look at the solution below – gene May 10 '14 at 17:18

With the solution given in How to create points based on the distance and bearing from a survey point? and memory layers.

Part of a my class to create memory layers from the Python console (How to create points in a specified distance along the line in QGIS？):

``````class Crea_layer(object):
def __init__(self,name,type):
self.type=type
self.name = name
self.layer =  QgsVectorLayer(self.type, self.name , "memory")
self.pr =self.layer.dataProvider()
def create_point(self,geometry):
# add point to the layer
self.seg = QgsFeature()
self.seg.setGeometry(QgsGeometry.fromPoint(geometry))
self.layer.updateExtents()
def create_line(self, point1,point2):
self.seg = QgsFeature()
self.seg.setGeometry(QgsGeometry.fromPolyline([point1,point2]))
self.layer.updateExtents()
@property
def show_layer(self):
``````

processing:

``````   # direction cosines function
import math
def cosdir_azim(azim):
cosa = math.sin(az)
cosb = math.cos(az)
return cosa,cosb

original_point = QgsPoint(50,50)
azimuth = 120
length = 50
cosa, cosb = cosdir_azim(azimuth)
end_point = QgsPoint(original_point.x()+(length*cosa), original_point.y()+(length*cosb))
# create the points layer
pt = Crea_layer("pt_120_50", "Point")
pt.create_point(original_point)
pt.create_point(end_point)
pt.show_layer
``````

Result: Create the line layer joining the points:

``````   li = Crea_layer("l_120_50", "LineString")
li.create_line(original_point,end_point)
li.show_layer
``````

Result: 