Skip to content

$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

Closed
wants to merge 3 commits into from

Conversation

rps
Copy link

@rps rps commented Nov 20, 2013

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.

rps added 3 commits November 19, 2013 16:37
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
@tychoish
Copy link
Contributor

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,
sam

@rps
Copy link
Author

rps commented Dec 9, 2013

Hi Sam,

I've created a test case below.

Consider a collection 'test' with the following:
{ "_id" : ObjectId("528bf49b5aa6e559c6b56ba5"), "lonlat" : [ 30.2453, 51.3007 ] }
{ "_id" : ObjectId("528bf4a75aa6e559c6b56ba6"), "lonlat" : [ 30.1637, 51.283 ] }
{ "_id" : ObjectId("528bf4b45aa6e559c6b56ba7"), "lonlat" : [ 30.0821, 51.2652 ] }

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:

db.test.find({'lonlat': { $near: [30.0821, 51.2652], $maxDistance: 0.00303106845 }})
where maxDistance is in radians (6mi/3959mi)

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:

db.test.find({'lonlat': { $near: [30.0821, 51.2652], $maxDistance: 0.08683371484 }})
results in a correct set containing the middle point.

I have encountered this issue using a data set with significantly more precision and am happy to send over with queries as well.

@tychoish
Copy link
Contributor

Just to confirm, you're using a 2d index and not a 2dsphere index?

@rps
Copy link
Author

rps commented Dec 10, 2013

Correct.

db.test.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "somedb.test",
"name" : "id"
},
{
"v" : 1,
"key" : {
"lonlat" : "2d"
},
"ns" : "somedb.test",
"name" : "lonlat_2d"
}
]

@tychoish
Copy link
Contributor

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?

@rps
Copy link
Author

rps commented Dec 11, 2013

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.

@tychoish
Copy link
Contributor

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,
sam

@tychoish tychoish closed this Dec 11, 2013
mongo-cr-bot pushed a commit that referenced this pull request Jun 29, 2022
Co-authored-by: jason-price-mongodb <[email protected]>

Co-authored-by: jason-price-mongodb <[email protected]>
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

Successfully merging this pull request may close these issues.

2 participants