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.

Comments

Anonymous said…
ehm..btw...where i must to put that script on my mapscript... data"the_geom(select.....? thanks before,..
Magnus Runesson said…
Anonymous: That depends on your eelopment environment. I suggest to write an PL/PGSQL stored procedure. See Postgres documentation for more information.
Anonymous said…
thanks for your respons, can do you help,..i use mapserver ms4w and postgis to store my point lon lat as the_geom, i success to drawing feature point on map, but i don;t know how to drawing circle as coverage my point on the map..., can i use your script to my map file?
Magnus Runesson said…
Unfortunately I have no lager experience with mapserver so cannot help you.
Anonymous said…
owh its okay... i has solved my problem..and i can draw circle on map now...but thanks your respons before...ng

Popular posts from this blog

Create your own CA with TinyCA2 (part 1)

Create your own CA with TinyCA2 (part 2)