pmah
pmah

Reputation: 167

Store a Circle in Geodjango + Postgres

Looking to store a circle in a geodjango field so I can use the geodjango query __contains to find out if a point is in the circle (similar to what can be done with a PolygonField).

Currently have it stored as a Decimal radius and GeoDjango Point Field, but need a way to query a list of locations in the DB such that these varying circles (point field and radii) contain my search point (long/lat).

Hope it makes sense.

Upvotes: 3

Views: 1309

Answers (1)

Mike T
Mike T

Reputation: 43622

Technically speaking, PostGIS supports CurvePolygon and CircularString geometry types, which can be used to store curved geometries. For example, a 2-unit radius around x=10, y=10 that has been approximated by a 64-point buffered polygon is:

SELECT ST_AsText(ST_LineToCurve(ST_Buffer(ST_MakePoint(10, 10), 2, 16)));
                   st_astext
------------------------------------------------
 CURVEPOLYGON(CIRCULARSTRING(12 10,8 10,12 10))
(1 row)

However, this approach is not typically done, as there is very limited support for this geometry type (i.e., ST_AsSVG, and others won't work). These geometry types will likely cause plenty of grief, and I'd recommend not doing this.

Typically, all geometries are stored as a well supported type: POINT, LINESTRING or POLYGON (with optional MULTI- prefix). With these types, use the ST_DWithin function (e.g., GeoDjango calls this __dwithin, see also this question) to query if another geometry is within a specified distance. For example, if you have a point location, you can see if other geometries are within a certain distance (i.e., radius) from the point.

Upvotes: 2

Related Questions