MongoDB geo distance/radius to useful units

旧街凉风 提交于 2019-12-04 13:08:29

问题


I'm using MongoDB geospatial queries -- $near, geoNear, etc. -- and I'd like to know how to turn the dis result of the geoNear command as well as the radius argument for $within queries to/from readable units like miles or kilometers.


回答1:


if your data is in geographic coordinates (i.e. 30.47921 N -121.45724 W) then you need to convert your underlying projection with the units you want (choose a projection that preserves distance) or you need to convert the distance in coordinates to something like miles.

Use the Haversine formula for calculations http://www.movable-type.co.uk/scripts/latlong.html




回答2:


Using geoNear command for a dis value in kilometers add the parameter:

distanceMultiplier: (6371 * Math::PI / 180.0)



回答3:


Note if you're using Mongodb version 2.6.0 there is a bug which always enables the new behavior for spherical coordinate systems to return distance in meters (based on WGS84 coordinates).

The documentation states that giving the $near parameter in legacy coordinates rather than GeoJSON should still return radians. This has been fixed in newer versions.

The bug report: Bug #13540



来源:https://stackoverflow.com/questions/3470902/mongodb-geo-distance-radius-to-useful-units

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!