# Calculate Polygon Main Angle in QGIS with Python

At the moment I have split my imported shapefile into the parts with the function 'qgis: explode' (pic1 & pic2): Next I want to the polygon main angle of each "exploded" element.

In ArcGIS there is the function "Calculate Polygon Main Angle (Cartography)" which solves this problem, but I didn't find a suitable function for it in QGIS. "Calculate Polygon Main Angle (Cartography)" creates a new column in the. dbf file and fills it with the angles of each element.

Is there an equivalent function in QGIS?

• For help with Python you need to include a code snippet to show what you have tried and where you are stuck. – PolyGeo Jan 24 '18 at 10:39

There is no pre-existing function, to my knowledge.

Meaning you'll have to implement that yourself using pyqgis or maybe even creative use of the field calculator.

Note that the Arcgis tool uses a more elaborate approach as it is meant for complicated polygons. In your case, where each polygon seems to be just a buffered line, you could simplify this a lot. The logic could be:

For each poly

1. Get the bounding box
2. Get the long side of the bbox
3. Get the angle of it
4. Done

Bonus: You could easily calculate the certainty of the angle by calculating the ratio of long / short side of the bbox.

If you need help with implementation, follow the advice in PolyGeo's comment and ask as precise questions as possible when you're stuck.

EDIT: There actually is a tool in QGIS for this!

Apply the Oriented minimum bounding box processing tool. It will create exactly what it says it will, and bonus: It already gives you the angle as an attribute field on the resulting geometries!

Remember this is not the same approach that the ArcGIS tool uses that you reference, as explained above!

• but you would need an oriented bounding box, don't you? a "normal" bounding box is parallel to the axes. If you take the lines instead of buffers would'nt that be easier, because you can extract both start and end point to get a straight line. Then you could calculate the angle between that line and the y-axis or to whatever. – Andreas Müller Jan 24 '18 at 16:33
• Obviously using the lines would be easier, if they are available. I assume they are not, because the questioni s about polygons. Bounding boxes are not necessarily parallel to the axes, and that would indeed make little sense here. But I found a premade function in QGIS for this, so it's even easier! Updating my answer. – Senshi Jan 25 '18 at 7:56
• It is now possible using main_angle function. – EranGeo Jul 16 '20 at 6:05

QGIS has a `main_angle(geometry)` function since version 3.16: https://www.qgis.org/en/site/forusers/visualchangelog316/index.html#add-main-angle-function-to-return-the-estimated-main-angle-of-a-geometry

To calculate it you can use the expression `main_angle(\$geometry)` for a new field.

Be aware that it might return unexpected results or require further processing. See the GIF in the changelog (https://www.qgis.org/en/_images/87367553-aae7d680-c5be-11ea-923a-e81200f2b90c.gif) for an example.

As written in the ESRI help context:

"The dominant angle of a polygon is the angle of longest collection of segments that have similar orientation. This angle will be stored in the specified field in decimal degrees from true north.",

you could:

1. Split the polygon geometry into segments,
2. calculate the length of the segments and choose the longest (you could use a length histogram to get an theshold for this selection)
3. orient the segment into and defined direction (smallest X always WEST, smallest Y always SOUTH),
4. calculate the angle for each segment,
5. and build the MEAN value of the angle set (simple approach)
6. or cluster the angle set by angle and length and choose the MEAN angle of the dominant cluster (more tricky) .