5

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

0

2 Answers 2

2

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

1

There is a way to directly get those segments of a line that are crossed by another line, using QGIS expressions with Geometry generator.

The trick is to split up the blue line into segments and than test for each segment if it crosses the red line. Only if this is the case, the corresponding segment should be rendered.

Use this expression:

collect_geometries(
    array_filter (
        array_foreach (     
            array_filter( 
                array_foreach(
                    generate_series (0, num_geometries (segments_to_lines ($geometry))),
                    geometry_n (segments_to_lines($geometry), @element)
                ), 
                @element <> 'NULL'
            ),
            if ( 
                crosses (
                    @element, 
                    collect_geometries (
                        overlay_crosses ('red',$geometry)
                    )
                ),
                @element,
                ''
            )
        ), 
        length(@element) > 0
    )
)

Segments of the blue linestring (one feature) that are crossed by red lines are highlighted in yellow: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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