4

I need to calculate the angle between line segments in a long chain of linestring as in the figure below: long chain of linestring

The way I approached this is:

  1. I assigned the id to the feature using:$id

  2. 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
  1. 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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
unknown123
  • 449
  • 1
  • 8
  • 2
    Check this : https://gis.stackexchange.com/questions/397497/calculating-interior-angles-of-polygons-or-lines-in-qgis – Taras Dec 24 '22 at 12:08
  • 2
    What do you want to do with the resulting angles? – BERA Dec 24 '22 at 15:00
  • Use these angles for modeling purposes in project – unknown123 Dec 25 '22 at 16:01
  • The solution in the above link is not valid to linestring. While using 'point_n(geometry, index)' the index value 1 and 2 are only possible. Only two vertices in one line feature. – unknown123 Dec 26 '22 at 13:02
  • OK, seems you have an exploded line: every segment is an own feature (line). Merge all lines, then the solution above should work. Otherwise, provide more information as it is unclear what exactly you need ("modeling purposes" is not really helpful) and how your data is structured: providing sample data might help. – Babel Dec 26 '22 at 13:11
  • Demo Data Link: https://drive.google.com/file/d/1k7em9PEr5iI92sTDAds8S08KppOvT2H5/view?usp=share_link I am interested in the angle between the lines. There are some disconnected features, is there any way to handle them? – unknown123 Dec 26 '22 at 15:15
  • Note: The attached is multilinestring. – unknown123 Dec 26 '22 at 15:24
  • 1
    Now you just have to merge the lines that are connected to get a linestring. If you label this linestring with the second expression in the answer linked above (the one starting with array_foreach (..., you get the left hand side angle at each but the first and last vertex, see: https://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. – Babel Dec 26 '22 at 15:45

1 Answers1

0

A simpler method might be using "Vector Geometry>Points along geometry", ~if~ the line segments are all same length, set this length to "Distance". Use offset 0 and end offset 0. The output will be points with distance along line, and angle between subsequent points. Can't tell from diagram if segment length max-min == 0 -edit- and even if the line segments are not the same length, set distance low e.g. 1m, and with a chain of points each with an angle to subsequent point, filter the output to remove redundant data.

CJA888
  • 13
  • 6