Wrong distance calculation with MongoDB

后端 未结 1 1266
南旧
南旧 2020-12-19 17:33

I am executing the following raw query with MongoDB:

qry = {\"position\" : SON([(\"$near\", [52.497309,13.39385]), (\"$maxDistance\", distance/111.12 )])}
lo         


        
相关标签:
1条回答
  • 2020-12-19 18:20

    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
    
    0 讨论(0)
提交回复
热议问题