I am storing rectangle in my DB in order to automatize the creation of technical maps of a networks. I am creating these maps in QGIS using the print composer. Many of these rectangles are oriented (they do not follow a strict W/E orientation).

I have a field in my table to store the orientation value, with the idea that I would be able to give this value in the print composer to generate an atlas with all the maps at once, properly oriented.

I however do not manage to find the proper formula in PostGIS to fill the value of an orientation that works in QGIS. The left side of the map is always the smaller side of the rectangle, and I do not want to have the North arrow pointing further South than East or West (i.e. the orientation in the QGIS composer has to be between -90° and 90°).

I found two queries that seems to do the job, but nonetheless the angle is systematically reversed (I get 45° when I need -45°), they also don't take into account the "North arrow never pointing down" parameter.

Here they are :

SELECT ((degrees(ST_Azimuth(ST_PointN(ST_ExteriorRing(geom),1),ST_PointN(ST_ExteriorRing(geom),2))) + 180)::numeric % 360 ) - 180 
FROM table;

SELECT CASE WHEN degrees(ST_Azimuth(ST_PointN(ST_ExteriorRing(geom),1),ST_PointN(ST_ExteriorRing(geom),2))) < -180 THEN degrees(ST_Azimuth(ST_PointN(ST_ExteriorRing(geom),1),ST_PointN(ST_ExteriorRing(geom),2))) + 360
ELSE degrees(ST_Azimuth(ST_PointN(ST_ExteriorRing(geom),1),ST_PointN(ST_ExteriorRing(geom),2))) END
FROM table;
  • 2
    Does 60 come out as -60? If so, you solution is simply to negate the angle. – John Powell Jul 4 '19 at 10:26
  • Hi, yes it works, but it doesn't solve the fact that I want it to be between -90 and 90. – GuiOm Clair Jul 4 '19 at 12:12
  • This is basically the same problem as dealing with atan2 function, in reverse. – John Powell Jul 4 '19 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.