I have a way which might work just using QGIS vector operations and expressions. It finds the angle of the longest segment of polygon features.
In outline:
- Convert the N polygon features to N linear features
- Explode the N linear features to M simple line segments
- The M simple line segments have an ID variable from the polygons. Select line segments with length not equal to the maximum length grouped by that ID and delete. That leaves just the longest simple line segment from each polygon.
- Add an
angle
attribute to the remaining simple line segments using the atan2
function.
- Join the polygon layer to the line segment layer on the ID attribute.
Now your polygon has a joined "angle" attribute which is the angle of the longest side (in radians).
That's the algorithm. The implementation follows:
You'll need "Polygons to lines" from the QGIS toolbox and then "Explode lines" from the toolbox for the first two steps.
To get the longest simple line segments grouped by "id" variable, do "Select by expression" and use: $length != maximum($length, "id")
- your id
variable might have another name, it needs to be unique per polygon. Apply that and you should have most of the simple line segments selected. Turn on editing for that layer and cut those features.
Create the angle column by adding a new attribute called "angle", and updating it with the following formula:
atan2(y_max($geometry) - y_min($geometry),x_max($geometry)-x_min($geometry))
Then switch editing off for that exploded layer and save.
Finally, use the polygon layer properties to create a Join from the polygons to the line segments, matching on the id
attribute.
Look now at the attribute table of your polygons, and it should have the angle of the longest line segment from the joined line segment layer.
Note this is not dynamic so if you change the polygons it will not update properly, you'll have to start again. I suspect this workflow could be wrapped into a flow diagram....