Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distance_by method is not working correctly #125

Open
pblogs opened this issue Jul 13, 2017 · 3 comments
Open

distance_by method is not working correctly #125

pblogs opened this issue Jul 13, 2017 · 3 comments

Comments

@pblogs
Copy link

pblogs commented Jul 13, 2017

So, I'm using geokit-rails and I would like to sort by distance, specifically showing the nearest meals first and descending from there. e.g. 5km, 10km, 15km.

However, for some reason the distance_by method isn't working at all, and the distances are all over the place.

This is how we're using it:

all_venues = Venue.where( 'listed = ? AND packages_count > ?', true, 0 ).by_distance(origin: search_location).search(params[:search]) || Venue.where(listed: true).all

Are there any work around methods to use? Or how can I sort from nearest to furthest with geokit-rails?

Thanks!

@locolab
Copy link

locolab commented Jul 19, 2017

+1

@mnoack
Copy link
Member

mnoack commented Jul 22, 2017

@pblogs @locolab - Whilst I don't have the time to resolve this issue, I'm more than able to review a PR which resolves the issue (along with a test of the currently failing case).

I'm sure with a little debugging (debugger, binding.pry) you'll be able to discover where the code is going wrong.

@pezholio
Copy link

Can confirm this is an issue for me as well. The SQL that is generated in for my use case (in postgres) is:

Place.by_distance(origin: [51.672607, 0.651835]).to_sql
#=> SELECT "places".* FROM "places" WHERE (places.latitude IS NOT NULL AND places.longitude IS NOT NULL)  ORDER BY "places"."name" ASC, (ACOS(least(1,COS(0.9018571252390695)*COS(0.01137666692973724)*COS(RADIANS(places.latitude))*COS(RADIANS(places.longitude))+        COS(0.9018571252390695)*SIN(0.01137666692973724)*COS(RADIANS(places.latitude))*SIN(RADIANS(places.longitude))+        SIN(0.9018571252390695)*SIN(RADIANS(places.latitude))))*3963.1899999999996)         ASC

Looking under the hood, Postgres ignores the distance based ordering. If I add it to the select query as a virtual column, like so:

SELECT "places".*, (ACOS(least(1,COS(0.9018571252390695)*COS(0.01137666692973724)*COS(RADIANS(places.latitude))*COS(RADIANS(places.longitude))+        COS(0.9018571252390695)*SIN(0.01137666692973724)*COS(RADIANS(places.latitude))*SIN(RADIANS(places.longitude))+        SIN(0.9018571252390695)*SIN(RADIANS(places.latitude))))*3963.1899999999996) AS distance FROM "places" WHERE (places.latitude IS NOT NULL AND places.longitude IS NOT NULL) ORDER BY distance

I get the expected result. I'm using Postgres 9.6.5 and Rails 4.2 BTW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants