Monday, 8 October 2018

postgis - How to get the rotation of a square Polygon?


I've got a layer made of perfect square polygons. I would like to obtain the rotation of each polygon:


enter image description here


In this case, the rotation would be 45 degrees.


I suppose I would have to compare it with a 0 rotation polygon, which has all of its sides parallel to the X axis or the Y axis. Example:


enter image description here



Answer




I agree with @user30184... and here is the code:


WITH poly AS (SELECT ST_MakePolygon(ST_GeomFromText('LINESTRING(75.15 29.53,77 29,77.6 29.5, 75.15 29.53)')) as geom)

SELECT degrees(ST_Azimuth(ST_PointN(ST_ExteriorRing(geom),1),ST_PointN(ST_ExteriorRing(geom),3))) as degAz
FROM poly;

I would also add the obvious, that this solution would work decently for a somewhat square or rectangular polygon as in the example but not for more complex polygons.


In that case, perhaps this code could be used for more complex polygons which gets the azimuth from the first point to the center of the polygon:


WITH poly AS (SELECT ST_MakePolygon(ST_GeomFromText('LINESTRING(75.15 29.53,77 29,77.6 29.5, 75.15 29.53)')) as geom)


SELECT degrees(ST_Azimuth(st_centroid(geom),ST_PointN(ST_ExteriorRing(geom),1))) as degAz
FROM poly;

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...