Skip to main content
6 of 6
added 31 characters in body
Matt
  • 17.9k
  • 4
  • 23
  • 59

You can use either of the following expressions - Either as a Geometry Generator symbol layer (on Layer B) or by using the Geometry by Expression geoprocessing tool if you wish to create a new layer with the results (used also on Layer B).

The first expression works for situations where there is only one intersection point per feature on Layer B. The second expression works when there are multiple intersection points per feature on Layer B.

Single intersection point per feature

-- find the intersection of the line layers and assign it to a variable
with_variable('i',
    intersection(
        -- a small extension is applied to Layer A to ensure there is a true intersection with Layer B.
        -- the extension is applied at both ends so that the direction of the line does not need to be known
        aggregate('Layer A', 'collect', extend(@geometry, 1, 1)),
        @geometry
    ),
    
    -- get the distance of the intersection along Layer B's geometry
    with_variable('dist_along', 
        line_locate_point(@geometry, @i),

        -- generate the line that spans 100 m either side of the intersection
        line_substring(@geometry, @dist_along - 100, @dist_along + 100)
    )
)

enter image description here

Multiple intersection points per feature

⚠️ Be aware: due to the nature of Geometry Expressions, this will create a MultiLineString with duplicated geometries. You will need to run Multipart to Singleparts and then Delete Duplicate Geometries on the output to clean it up.

-- find the intersection of the line layers and assign it to a variable
with_variable('i',
    intersection(
        -- a small extension is applied to Layer A to ensure there is a true intersection with Layer B.
        -- the extension is applied at both ends so that the direction of the line does not need to be known
        aggregate('Layer A', 'collect', extend(@geometry, 1, 1)),
        @geometry
    ),
    
    -- collect the array of geometries into a MultiLineString
    collect_geometries(
        -- loop through each intersection point per Layer B feature
        array_foreach(
            -- convert the MultiPoint intersection geometry into an array of geometries 
            geometries_to_array(@i),
            
            -- find the distance of the intersection along the Layer B feature
            with_variable('dist_along', 
                line_locate_point(@geometry, @element),         
                
                -- generate a line that follows Layer B spanning 100 m either side of the intersection
                line_substring(@geometry, @dist_along - 100, @dist_along + 100)
                
            
            )
        )
    )
    
)

enter image description here

Note, when using either a Geometry Generator symbol layer or the Geometry by Expression tool, don't forget to set the output geometry type to LineString / MultiLineString or Line respectively.

Matt
  • 17.9k
  • 4
  • 23
  • 59