# How to compute angle of circular buffer' segments around a point in PostGIS?

I have a point feature and using `ST_Buffer()` command, I created a circular buffer of 200 meters with 3 segments in quarter of the circle around this point:

``````Select ST_Buffer(my_point.geom, 200, 'quad_segs=3') As geom from my_point
``````

Now, I would like to compute angles of each segment w.r.t to the reference point. PostGIS command `ST_Azimuth()` works for computing azimuth for point geometries. Could anyone suggest me how to compute the angles of each segment?

• Why not just divide a full turn (i.e. 360°) by the number of segments? This result does not depend on point location or buffer distance. – Mike T Aug 26 '16 at 5:05
• Thanks for your useful suggestion. My requirement is to make use of point geometry anyhow. In the next step, I would like to break this circular buffer of 12 segments into geometry. – khajlk Aug 26 '16 at 8:20

Below is a simple example in that I did not further refine the result to exclude the duplicate 270 nor did I round the results to the nearest whole number because I don't know what the end goal is. Further refinement to your specific needs wouldn't take much further effort.

Consider this statement which uses the exterior ring of a point at (0,0) with a buffer of 200 meters and calculates the azimuth of each point:

``````SELECT st_azimuth(ST_PointN(my_point, generate_series(1, ST_NPoints(my_point))),st_centroid(my_point))/(2*pi())*360
FROM ST_ExteriorRing(ST_Buffer(ST_GeomFromEWKT('Point(0 0)'), 200, 'quad_segs=3')) as my_point
``````

The result is:

``````270
300
330
360
29.9999999999999
59.9999999999999
89.9999999999998
120
150
180
210
240
270
``````
• Thanks a lot. But if want to calculate the angle of each segment and if there are total 12 segments in my buffer then isn't the angles should be multiple of 30 degrees up to 360? – khajlk Aug 25 '16 at 21:43
• @JibranKhan I dont understand the question. The results above are multiples of 30° up to 360°. This is a simple example in that I did not further refine the result to exclude the duplicate 270 nor did I round the results to the nearest whole number because I don't know what your end goal is. Further refinement to your specific needs wouldn't take much further effort. – kttii Aug 26 '16 at 13:13

Here's an example where I take a single school from my schools table, create a buffer around it, convert it to points, then find the azimuth between each point in the buffer, and the original school:

``````  select
--convert azimuth to degrees
degrees(ST_Azimuth(ST_Transform(sch.geom, 2877), buffpoints.geom))
from dpsdata."Schools_Current" as sch

--subquery to create buffer and convert to points aliased as buffpoints
, (select
(ST_DumpPoints(ST_Buffer(ST_Transform(sch.geom, 2877), 1000))).geom
from dpsdata."Schools_Current" as sch where sch.schnum = '453'
) as buffpoints

--specify the original school from which to calculate the azimuth to
where sch.schnum = '453'
``````

Note I'm transforming the geometry to SRID 2877 to work in Feet for the buffer...

This outputs a table of each azimuth direction in degrees between each buffer vertex and the original point.

• I tried to use SRID: 3044 in the above code and the mentioned one as well. Unfortunately, got the error: "ERROR: Input geometry has unknown (0) SRID" but I already Set the SRID to 3044 beforehand. Perhaps, something is wrong in ST_Transform()?? – khajlk Aug 25 '16 at 21:41
• that error message suggests that you didn't set an srid on import, so the geometries don't have a crs. Try changing any instances of `sch.geom` to `st_setsrid(sch.geom,3044)` in this answer, does that work? If you use a tool like shp2pgsql-gui it's very easy to forget to set the SRID... – Steven Kay Aug 25 '16 at 22:38
• I have already set the SRID to 3044. +1 for your suggestion but it doesn't work either. I may try to remove ST_Transform() but I am curious why it doesn't work! – khajlk Aug 25 '16 at 23:17