4

I am executing the following raw query with MongoDB:

qry = {"position" : SON([("$near", [52.497309,13.39385]), ("$maxDistance", distance/111.12 )])}
locations = Locations.objects(__raw__=qry)

The position in the database is set to [52.473266, 13.45494].

I get a result once I set the distance to 7.3 or higher, so it seems the two locations must at least be 7.3 kilometer away from each other.

When I calculate the distance of those two geo locations with Google Maps (for example going by car) it's telling me it's only 5.2 kilometer away from each other.

I tested it with loads of different locations and there is always a big difference in the distance calculation of google and Mongodb

Am i missing anything or can somebody explain please where this difference is coming from?

I already checked this answer but it's not working for me...

Community
  • 1
  • 1
Jonas
  • 302
  • 2
  • 13
  • Hi, i'm executing this call from within django/tastypie so SON is working for me and I cannot use .getIndexes(). maxDistance with the legacy coordinate pairs is using radians. ...."If you query for legacy coordinate pairs, specify $maxDistance in radians." So to calculate a distance in km I need to convert it to radians with the division of 111.12 – Jonas Oct 21 '13 at 14:32

1 Answers1

5

MongoDB assumes that coordinates are in (long, lat) format. If you compute distances by hand using Great-circle distance you'll see what is going on:

> from math import acos, sin, cos, radians
>
> long_x, lat_x = [radians(y) for y in [52.473266, 13.45494]]
> long_y, lat_y = [radians(y) for y in [52.497309, 13.39385]]
>
> acos(sin(lat_x) * sin(lat_y) + cos(lat_x) * cos(lat_y) * cos(long_x - long_y)) * 6371.0
7.27362435031

Google takes coordinates in (lat, long) format so if you provide the same input Google interpretation will be like below:

> acos(sin(long_x) * sin(long_y) + cos(long_x) * cos(long_y) * cos(lat_x - lat_y)) * 6371.0
4.92535867182
zero323
  • 322,348
  • 103
  • 959
  • 935