I need to calculate the angle between line segments in a long chain of linestring as in the figure below:
The way I approached this is:
I assigned the id to the feature using:
$id
Now each feature is assigned a unique id, I calculated the azimuth of each line segment using:
CASE
WHEN ((yat(-1)-yat(0)) = 0 and (xat(-1) - xat(0)) >0) THEN 90
WHEN ((yat(-1)-yat(0)) = 0 and (xat(-1) - xat(0)) <0) THEN 270
ELSE (atan((xat(-1)-xat(0))/(yat(-1)-yat(0)))) * 180/pi() +
(180 * (((yat(-1)-yat(0)) < 0) + (((xat(-1)-xat(0)) < 0 AND (yat(-1) - yat(0)) > 0)*2)))
END
- Then, I calculated the angle difference between the consecutive if as:
attributes(
get_feature_by_id(
@layer ,
$id + 1
)
)['Azimuth'] - "Azimuth"
The main issue with my approach is the id might not always be assigned to the consecutive line segment. So there could be a simpler way to find the angle between the line segments in a long chain of linestring.
array_foreach (...
, you get the left hand side angle at each but the first and last vertex, see: i.stack.imgur.com/iGpjH.png - about disconnected features: this is a separate question and should be asked separately - and similiar questions have been asked before on this site.