[Answer]-Advice on how to structure a Location model with Django and PostgreSQL (postGIS)

1👍

I dealt with a similar problem recently. I found that PostGIS and other spatial solutions were overkill for finding all of the points in an approximate radius.

I ended up using the SAS distance algorithm in the database. After filtering with a square, I used the raw command to let the database crunch the numbers for me. Postgres and most other databases other than SQLite support trig functions, so you can filter on the database’s computed distance. This is the formula in miles, but you can change the constant.

Dist = 3949.99 * arcos(sin(Y1) * sin(Y2) + cos(Y1) * cos(Y2) * cos(X1 - X2));

http://support.sas.com/kb/3/973.html

Edit: To expand, I was querying all zip codes (~44K) in the US within a radius. It turns out I could query all of the zip codes within 500 miles in fractions of a second that were acceptably fast for my purposes. The geospatial libraries allow you to find all of the points within a complex polygon much more quickly, but I don’t expect you’ll see a noticeable performance improvement for your application.

Leave a comment