2

At the moment I have split my imported shapefile into the parts with the function 'qgis: explode' (pic1 & pic2):

pic1 & pic2

Next, I want to the polygon main angle of each "exploded" element.

In ArcMap 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?

0

3 Answers 3

3

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.

1

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!

3
  • 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. Commented Jan 24, 2018 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
    Commented Jan 25, 2018 at 7:56
  • It is now possible using main_angle function.
    – EranGeo
    Commented Jul 16, 2020 at 6:05
1

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) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.