Circles in PostGIS
OGC and PostGIS has no data type to represent a circle. Instead one use ST_Buffer(geom, distance) to get a geometry that covers the area within the distance to geom. So if geom is a point you get a circle. But it does not return an exact circle. The circle is approximated represented by a polygon using eight points. One can increase the precission by adding a third argument to ST_buffer that is the number of points in the polygon returned. The unit for distance depends on what SRID is used.
But what if you have a geometry that you know is a circle created with the method above? There is no exact method as far as I know, but her comes a good approximation.
First we want the origo of the circle. ST_Centroid(geom) comes to handy. It returns the centeroid point of the geometry, in this case the origo of the circle.
To get the radius we pick out the first point in the polygon returned by ST_Buffer with PointN(ExteriorRing(geom),1).
We need to call ExteriorRing to get the polygon of geom since ST_Buffer does return type is a geometry and not a polygon. Then we calculate the distance betwen origo and the polygons first point with ST_distance_sphere. ST_distance_sphere always returns distance in meters.
All together:
circle=ST_Buffer(GeomFromEWKT('SRID=3021;POINT(1537619 6347866)'),
10000, 16)
origo=ST_Centroid(geom)
radius=ST_distance_sphere(PointN(ExteriorRing(geom),1), origo)
I use the swedish projection RT-90 which uses distances in meters to create the circle.
But what if you have a geometry that you know is a circle created with the method above? There is no exact method as far as I know, but her comes a good approximation.
First we want the origo of the circle. ST_Centroid(geom) comes to handy. It returns the centeroid point of the geometry, in this case the origo of the circle.
To get the radius we pick out the first point in the polygon returned by ST_Buffer with PointN(ExteriorRing(geom),1).
We need to call ExteriorRing to get the polygon of geom since ST_Buffer does return type is a geometry and not a polygon. Then we calculate the distance betwen origo and the polygons first point with ST_distance_sphere. ST_distance_sphere always returns distance in meters.
All together:
circle=ST_Buffer(GeomFromEWKT('SRID=3021;POINT(1537619 6347866)'),
10000, 16)
origo=ST_Centroid(geom)
radius=ST_distance_sphere(PointN(ExteriorRing(geom),1), origo)
I use the swedish projection RT-90 which uses distances in meters to create the circle.
Comments