3

I want to connect the green polygon and the blue line. The buffer tool is not suitable because it makes the whole shape bigger. I'd like to make longer the length of the green one to attach to the line.

Is there any way to attach the vertex or polygon to the nearest line? I thought snap tool could help that, but the output was the same as the input geometries. I also tried the v.clean tool with dissolved layer, it was the same as the result of the snap tool.

enter image description here

1
  • 1
    Do you want to do this automatically, or manually? How do you plan to handle "corner situations", like with the lower polygon? – Erik Jan 18 at 7:35
3

You polygons must have a unique identifier in the attributes, in my case the field named fid. I also presume the line layer has the name line. If you have other names, change it in the following expressions accordingly.

Be aware: the expression overlay_nearest is available since QGIS 3.16, see visual changelog. For older versions, use the refFunctions plugin instead.

  1. For every vertex of the polygon layer, find the nearest point on the line layer using this expression with Menu Processing / Toolbox / Gemonetry by expression, set the vertices as input and output geometry type as points: closest_point (array_first (overlay_nearest( 'line', $geometry)), $geometry). As output, you get the red dots (see screenshot).

  2. Select all features from the vertices layer, copy and paste them to the point layer changed_geometries - the output from step 1 (red dots).

  3. Run Menu Processing / Toolbox / Minimum bounding geometry set changed_geometries (output from step 2) as Input layer and select fid as Field (optional, set if features should be grouped by class). As geometry type, select Minimum Oriented Rectangle. If you run the tool, you get the blue polygons as output:

enter image description here

Variants:

You can use the point layer output from step 1 (skip step 2/copying vertices), than in step 3 select Convex hull instead of Minimum Oriented Rectangle, see result:

enter image description here

And this is the output you get including step 2, but selecting Convex hull in step 3:

enter image description here

2

A completely different solution with yet another resulting polygon shape consists of "stretching" the polygons: just extend the length (longer side) of the rectangle until its intersection with the lines layer. This is more intuitive, but not necessarily easier to realize. You can do it as follows.

Remark: your polygon should have an uniqe identifier in the attributes. I have a field fid, if you have a field with another name, change this accordingly in step 3 and 6.

  1. Convert the polygons to line (boundary): Menu Vector / Geometry Tools / Polygons to lines

  2. Creat separate features for all four sides: Menu Processing / Toolbox / Explode lines

  3. Find the two length (longer) sides of the polygon: these are the lines we want to extend. Create a new attribute extend with field calculator on the output of step 2 using this expression: if (length ($geometry)> median( length ($geometry), "fid"), true, false)

  4. Select all features with attribute field estend = true (in the attribute table or with select by expression from the toolbar).

  5. Extend the selected length sides of the polygon and find their interections with the lines layer: create a point where both lines cross. Use Menu Processing / Toolbox / Geometry by expression, check the mark next to Selected features only, output geometry type: point and paste this expression:

intersection (
    aggregate ( 
        'line',
        'collect',
        $geometry
    ) ,
    extend (
        $geometry, 
        100,
        100
    )
)
  1. Now create a polygon from the point output of step 5, using Menu Processing / Toolbox / Minimum bounding geometry and select fid as Field (optional, set if features should be grouped by class). Select convex hull as geometry type.

See the output:

enter image description here

4
  • Thank you for detailed answer! I tried what you wrote, but I have a question. Actually the blue line in picture above consists of several line, not one, so if I extend the line like this method, it will make two or more intersections. Is there a way to stop detecting if one intersection detected? – jin Jan 26 at 5:15
  • Simply reduce the length the line is extended to avoid additional intersections. To get just the closest point, see the other solution. – Babel Jan 26 at 10:53
  • I saw the other answer you wrote. That was great, but that makes polygon changing a bit, so I think this method is better than that. The reason why I can't reducing the length of the line is some points may not be connected. I would like to make only one intersection as the line and polygon are connected. Anyway, thank you so much for your answer. – jin Feb 1 at 2:42
  • I see. Is it possible to provide a sample of your data? I adapted my solution to some dummy data I created, but best would be to see the exact setting you have - it would probably be easier to find a solution that works with your data than again experimenting with some random lines and polygons I create. – Babel Feb 1 at 19:53

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.