I'm trying to handle proximity search for a basic store locater in Django. Rather than haul PostGIS around with my app just so I can use GeoDjango's distance filter, I'd like to use the Spherical Law of Cosines distance formula in a model query. I'd like all of the calculations to be done in the database in one query, for efficiency.
An example MySQL query from The Internet implementing the Spherical Law of Cosines like this:
SELECT id, (
3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) *
cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) *
sin( radians( lat ) ) )
)
AS distance FROM stores HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;
The query needs to reference the Zipcode ForeignKey for each store's lat/lng values. How can I make all of this work in a Django model query?
haversine
formula; it's thespherical law of cosines
formula; see (for example)http://www.movable-type.co.uk/scripts/latlong.html
and look at the respective Wikipedia articles. (b) I do trust that you'll replace the hard-coded user coordinates with variables :-) (c) The gentle reader should be warned that your unit of distance is somewhat archaic (something to do with 1000 * (length of Roman legion's standard pace), I believe) :-) – Jethro