I have two line layers in QGIS 3.16: line 1 (blue), line 2 (red). The white points are the vertexes (nodes) of the two lines. They have no common vertex, are not connected. How can I get the segment from line 1 that is crossed by line 2 using QGIS expressions? I would like to generate a line by geometry generator of the highlighted yellow segment (see screenshot).
I tried this as a geometry generated symbol layer on line 1: I first segmentize line 1 to get the segments, than I cross it with the line_2 on the other layer. I get a boolean output (true/false) and I am also able to create a crossing point where the two lines intersect. However, I tried in vain to address directly the segment that is crossed.
crosses ( segments_to_lines ( $geometry), geometry ( get_feature( 'line_2', 'fid', 1 ) ) )
In this simple case, I could get the segment with the expression (that's how I generated the yellow line):
geometry_n (segments_to_lines ( $geometry), 3). However, I have to set the segment-number (3 in this case) manually, but I want to create this number autormatically as I have a lot of different cases and can't do it manually. How can this be done?
For the background of this question, see: Re-locating points randomly at certain distance in QGIS (Step 5)