The distance function should be this:
distance_function = (
(6371
* func.acos(func.cos(func.radians(bindparam('origin_lat')))
* func.cos(func.radians(places_table.c.latitude))
* func.cos(func.radians(places_table.c.longitude) -
Hi,
I've like to include the distance of a particular row (object) from a given
point in a SQLAlchemy query using the haversine formula, and SORT on the
distance, similar to this example:
http://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula
But can't figure
On Tue, Oct 16, 2012 at 07:06:00AM -0700, Michael Wilson wrote:
Hi,
I've like to include the distance of a particular row (object) from a given
point in a SQLAlchemy query using the haversine formula, and SORT on the
distance, similar to this example:
Thanks!
Your example worked perfectly.
But to simplify the question, I left out a wrinkle. My tables look roughly
like this:
mapper(Thing, things_table,properties={
'place_id' : things_table.c.place_id,
..
'place' : relation(Place,
it's not going to be simple as relationship() doesn't render HAVING, it would
have to be embedded in a subquery and then set into the relationship as a
secondary table or something similar - this is assuming the lat/long columns
in question are on places_table. There are also special recipes
I got it to work. Here's what I did:
distance_function = (
3959
* func.acos(func.cos(func.radians(*bindparam*('origin_lat'
* func.cos(func.radians(places_table.c.latitude))
* func.cos(func.radians(places_table.c.longitude) - func.radians(*