# Calculate the rotation of square polygons in QGIS

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.

• You could write a python function to get the four coordinate points and compute the angle. Care to offer some sample data? Apr 10 '17 at 16:41
• Unfortunately, I am not familiar with python at all. I am trying to do this with the azimuth function in the field calculator. However, I do not really understand how to tell the programm to always use the first two nodes of each polygon and calculate the azimuth... Apr 10 '17 at 16:51
• `(180/pi())* atan2(\$y_at(1)-\$y_at(0), \$x_at(1)-\$x_at(0)) ` should get you an angle for the first two points. Apr 10 '17 at 17:03
• Or this for azimuth: `180/pi() * azimuth(make_point(\$x_at(0), \$y_at(0)), make_point(\$x_at(1),\$y_at(1))) ` (which is probably just 90 degrees away from the previous example) Apr 10 '17 at 17:08

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

• there's also a degrees() function to convert radians to degrees Apr 10 '17 at 21:21
• How would you do calculate polygon orientation if they are within 4 to 15 points ? If think first would be determinating polygon box then bow orientation. But my programming skills are terrible... Mar 3 '20 at 20:43
• @Laurent try asking as a new question if you really want to know. Mar 3 '20 at 23:34