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.
-
3There 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 rl2 days ago
-
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 Coando2 days ago
1 Answer
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):
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)
)
)
)
)
-
Thank you, Babel. Can the above process be applied to many lines overlapping many polygons? It would always be one line overlapping two polygons. 2 days ago
-
1I'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).– Babel2 days ago
-