2

Is there a way to clip a line that overlaps two polygons in QGIS? The line needs to be resized to either the poly/line intersection on the inside of the polygons or the poly/line intersection on the outside of the polygons.

enter image description here

Anthony
  • 127
  • 7
  • 3
    There are many ways to do this with expressions or with tools, or manually. Is this s one of or something to be done in mass? Does the line only ever cross two spacedout polygons? – Al rl May 14 '22 at 19:44
  • Would be great if this could be done in mass or many lines to many polygons. The situation would be same with one line overlapping two polygons. – Anthony May 14 '22 at 21:26

1 Answers1

5

Use QGIS expressions with Geometry Generator or Geometry by expression (see here for details) with this expression

A) To get the line from inside the polygons (below for outuside polygons):

with_variable(
    'closest',
    array_foreach (
        overlay_intersects(
            'polygon',
            $geometry
        ),
        intersection (
            @element, 
            $geometry
        )
    )
    ,
    line_substring (
        $geometry,
        line_locate_point(
            $geometry, 
            closest_point (
                @closest[0],
                line_interpolate_point (
                    $geometry,
                    length ($geometry)/2
                )
            )
        ),
        line_locate_point(
            $geometry, 
            closest_point (
                @closest[1],
                line_interpolate_point (
                    $geometry,
                    length ($geometry)/2
                )
            )
        )
    )
)

Screenshot: initial line (thin black) and expression to create line from inside the polygons (bold black); in red: lines from outsides of polygon (see next expression): enter image description here

B) To get lines from outsides of polygons:

with_variable(
    'closest',
    array_foreach (
        overlay_intersects(
            'polygon',
            $geometry
        ),
        intersection (
            @element, 
            $geometry
        )
    )
    ,
    line_substring (
        $geometry ,
        line_locate_point(
            $geometry, 
            closest_point (
                collect_geometries (@closest),
                start_point ($geometry)
            )
        ),
        line_locate_point(
            $geometry, 
            closest_point (
                collect_geometries (@closest),
                end_point ($geometry)
            )
        )
    )
)
Babel
  • 71,072
  • 14
  • 78
  • 208
  • Thank you, Babel. Can the above process be applied to many lines overlapping many polygons? It would always be one line overlapping two polygons. – Anthony May 14 '22 at 21:30
  • 1
    I'm not really sure what you mean, but have a look at the screenshot: there are two lines and four polygons, each line overlapping two polygons. So I guess this is what you want (what you asked for). – Babel May 14 '22 at 21:37
  • It worked! Thank you for taking the time to answer my question. – Anthony May 15 '22 at 04:35