The endpoint is displaced from the origin by 800 meters, of course. The displacement in the direction of the x-coordinate is proportional to the sine of the angle (east of north) and the displacement in the direction of the y-coordinate is proportional to the cosine of the angle.
Thus, from sin(15 degrees) = sin(0.261799) = 0.258819 and cos(15 degrees) = 0.965926 we obtain
x-displacement = 800 sin(15 degrees) = 800 * 0.258819 = 207.055
y-displacement = 800 cos(15 degrees) = 800* 0.965926 = 772.741.
Therefore the endpoint coordinates are (400460.99 + 207.055, 135836.76 + 772.741) = (400668.05, 136609.49).
If you want to take advantage of the former code you can do that:
from math import sqrt
import itertools
layer = iface.activeLayer()
features = layer.getFeatures()
lines = [feature.geometry().asPolyline() for feature in features]
k = 0
for points in lines:
n = len(points)
list = range(n)
print "line" + str(k) + ", " + str(n) + " points"
length_segments = [sqrt(points[i].sqrDist(points[j]))
for i,j in itertools.combinations(list, 2)
if (j - i) == 1]
sum = 0
for length in length_segments:
i = length_segments.index(length)
print "segment = %d, length = %.2f" % (i, length)
sum += length
print "sum = ", sum
k += 1
This code calculates each segment length (corroborated with "Measure Line Tool") in all lines; as you can see for the line vector layer of below image:
Result at the Python Console was:
line0, 4 points
segment = 0, length = 21735.11
segment = 1, length = 20208.88
segment = 2, length = 38158.48
sum = 80102.4682351
line1, 7 points
segment = 0, length = 18831.03
segment = 1, length = 50481.12
segment = 2, length = 30119.61
segment = 3, length = 26176.50
segment = 4, length = 47111.79
segment = 5, length = 21697.58
sum = 194417.62525
line2, 5 points
segment = 0, length = 23266.31
segment = 1, length = 21186.16
segment = 2, length = 23595.18
segment = 3, length = 25817.39
sum = 93865.0436374
The total length of each line, as sum of all its individual segments, it is also calculated.
Best Answer
This function will create a new point based on:
distance
(meters),azimuth
(degrees) and the starting pointstart_QgsPoint
which is qgis.core.QgsPoint class. Then you can connect the coordinates into a line. This only works with projected coordinate systems.