[postgis-users] How to add circle to Postgis database?

Brad Ediger brad at bradediger.com
Wed Mar 7 11:05:58 PST 2007


On Mar 7, 2007, at 12:59 PM, Brent Wood wrote:

>
> Maciej Skorczewski wrote:
>> hi i get this data and don't know hove add this to postgis database
>> what type of geometry it is (MULTILINESTRING,POLYGON)?
>>
>>
>>
>> center-X: -87.69581735217052,
>> center-Y: 43.75183840008278
>> Radius: 0.007124880185134723
>
> Postgis doesn't (yet) support circle datatypes, just points, lines  
> & polygons
> as defined in the OGC SFS specification.
>
> One way to approximate a circle is to define a polygon with  
> vertices which
> correspond to your circle perimiter.
>
> To do this you can create a point at the centre location with your  
> XY values &
> then buffer this point (see the buffer function in the docs) with  
> lots of
> vertices using the radius.
>
> There is unfortunately a nasty trap here, as the point will need to be
> reprojected to a suitable equidistant or equal area projection to  
> generate a
> true circle, and the radius will need to be converted to the new  
> projections
> map units. This is because the distance represented by 1 degree of  
> longitude
> changes with latitude, decreasing away from the equator to 0 at the  
> poles.
>
>
> So it can sort of be done, but is not straightforward.
>
>
> The SQL is along the lines of
>
> select transform(buffer(transform(point,<new SRID>),<new distance>). 
> 4326) as
> circle;
>
> where 4326 is the SRID (probably) for your base lat long data.
>

Here's the function I usually use, which handles the reprojection for  
you. It might be overly complicated but it works for me.

(SRID 27582 is a Lambert II planar projection, as measured in meters.)

CREATE OR REPLACE FUNCTION circle(geometry, numeric) RETURNS geometry
     AS 'SELECT Transform(SetSRID(Buffer(Transform(SetSRID($1, 4326),  
27582), $2), 27582), 4326);'
   LANGUAGE sql;

Used as follows:

-- select circle with 1-km radius around the given point
SELECT circle(MakePoint(lng,lat), 1000);

HTH,
Brad

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2421 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20070307/8d255b76/attachment.bin>


More information about the postgis-users mailing list