-
Notifications
You must be signed in to change notification settings - Fork 1.7k
$maxDistance appears to require degrees rather than radians #1393
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
Conversation
Querying $near with $maxDistance requires a conversion from radians to degrees for correct results. Happy to provide sample data/queries to verify.
For geographic coordinates, accurate results were not obtained unless the radian input was multiplied by 180/pi.
Was able to create two 2d indexes
Thanks for this contribution. I'm following up with the people who do work on the geospatial capability in MongoDB, although as far as I know we return all distances in radians, although I may be missing a fact. Given that, I'm a bit confused where you observed this behavior? Do you have an example query or illustrative data points that might help me/us understand what's going here? Thanks, |
Hi Sam, I've created a test case below. Consider a collection 'test' with the following: The top and bottom point are separated from the middle point by ~5.72-5.77 miles. In my queries below I use 6 miles, as these coordinates lack some precision. Were I to use radians, my query might look like this:
The result should have both the input and middle point, but only contains the input point (the same result as if maxDistance = 0) Multiplying the radian value (6/3959) by (180/pi) converts it to degrees. My query in degrees:
I have encountered this issue using a data set with significantly more precision and am happy to send over with queries as well. |
Just to confirm, you're using a |
Correct.
|
I believe that the issue your observing is that 2d sphere in conjunction with the $near operator returns results using planar geometry, and the resulting calculation error explains the limited response set. To correct for this problem you can use a 2dsphere index which uses spherical geometry to calculate distance for points on the sphere. If you're already using 2d indexes you may want the $nearSphere query operator for use with 2d indexes, which attempts to use spherical geometry despite the planar nature of the index. I was able to confirm that $nearSphere produces the expected result on your existing data set. The following documents might help clarify the issue
How does this affect the observed behavior on your data? |
For unrelated reasons, I ultimately used a geoNear operator with maxDistance in meters, which does not face the same problem. That said, I submitted the pull request because I believe that the $near operator requires the use of degrees rather than radians. This is important for those who, for whatever reason, are using 2d indexing rather than 2d sphere. The documentation specifically mentions support for this: 'The $near operator requires a geospatial index: ... a 2d index for legacy coordinate pairs.' To avoid future confusion, I think it's important for the documentation to note that using maxDistance with $near with legacy coordinate pairs on a 2d index requires an input of degrees rather than radians. |
I've confirmed that maxDistance does in fact return using radians as designed with the engineer responsible for geospatial queries and indexes. I think that the geoNear commands provides a more intuitive interface for the kind of query that you're trying to implement. Sorry for the confusion, and thanks for your work on this. Cheers, |
Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
Changed documentation to reflect degree requirement for accurate querying of geographic coordinate data. Also changed index documentation to reflect that multiple indexes of a given 2d type can be made.