TransWikia.com

Attaching vertex or polygon to line in QGIS

Geographic Information Systems Asked on August 9, 2021

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

2 Answers

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

Correct answer by Babel on August 9, 2021

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

Answered by Babel on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP