2

Is there a way to rotate all polygons in layer_a (green polygons) to align with the longest side of the polygon in layer_b (red polygon) without having to manually enter a rotation angle? In most cases there would be multiple polygons in both layers - the idea would be for all polygons in layer_a to align with longest side of all the polygons in layer_b

enter image description here

enter image description here

I understand there is a similar question

But the solution is tedious for the amount of polygons I'm planning to adjust.

Anthony
  • 127
  • 7

1 Answers1

4

Using QGIS 3.28, this can be achieved with the Geometry by expression tool.

with_variable(
    'current_rotation',
    main_angle($geometry),
rotate( 
    $geometry,
    main_angle(
        overlay_intersects(
            'target poly',
            $geometry
        )[0]
    ) - @current_rotation       
)

)

The rotation angle for each feature is found by using the main_angle function on the 'target polygon' that intersects with that feature. The main_angle of the original feature is subtracted from the rotation angle to deal with varying orientations.

From the help panel of the expression editor:

function main_angle

Returns the angle of the long axis (clockwise, in degrees from North) of the oriented minimal bounding rectangle, which completely covers the geometry.

I applied this expression on the red polygon layer. The black outlines are the 'target polygon' layer.

Result:

enter image description here

Red polygons are the original geometry, blue polygons are rotated to the main axis of the polygon in 'target_poly' layer (black outline).

Matt
  • 16,843
  • 3
  • 21
  • 52