QGIS – Calculate the Rotation of Square Polygons in QGIS

atlaspolygonqgis

In QGIS, I have a polygon layer with square polygons in it. The polyogons are of same size but different orientation. I want to create a set of maps using the atlas feature. My aim is to rotate the map accordingly to the orientation of the polyogns.

The "item properties" offer the possibility to insert an expression for the map rotation. If I had a rotation-field in the property table of the poygons layer I'd be able to automate map rotation accordingly, I suppose.
So my question is: is it possible to calculate the rotation of quadratic polyon features relative to the N-S (or W-E) axis?

Here I found a similar question; however, not being familiar with PostGIS, I want to accomplish this in QGIS.

Best Answer

You can use $x_at(n) to get the n-th x coordinate. Then you can either use atan2 with the coordinate differences:

(180/pi())* atan2($y_at(1)-$y_at(0), $x_at(1)-$x_at(0)) 

or azimuth by making points:

180/pi() * azimuth(make_point($x_at(0), $y_at(0)), 
                   make_point($x_at(1),$y_at(1))) 

The 180/pi() gets you degrees. I think the only difference between these two is the angle for zero, and maybe the direction (clockwise or anti-clockwise...).

Related Question