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).

enter image description here

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 (

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)


If it is not mandatory to deploy a QGIS expressions then you may try the following workflow

Step 1. Apply "Explode lines" for the layer 'Line 1'

Step 2. Use the "Geometry by expression" geoalgorithm, where the result of Step 1 crosses features from the layer 'Line 2'. Also you may use a basic "Intersection".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.