如何用lng和lat获得地球上两点之间的距离或弧度?

时间:2021-10-12 21:17:53

how to get the distance or radian between two point on the earth with lng and lat?

如何用lng和lat获得地球上两点之间的距离或弧度?

2 个解决方案

#1


You probably don't want mapReduce in this case but actually the aggregation framework. Apart from the general first stage query you can run via $geoNear which is more efficient in your purpose.

在这种情况下,您可能不希望使用mapReduce,但实际上是聚合框架。除了一般的第一阶段查询,您可以通过$ geoNear运行,这对您的目的更有效。

db.places.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ -88 , 30 ]
        },
        "distanceField": "dist"
    }},
    { "$match": {
        "loc": {
            "$centerSphere": [ [ -88 , 30 ] , 0.1 ]
        }
    }}
])

Or frankly, because the initial $geoNear stage will "project" an additional field into the document containing the "distance" from the queried "point of origin", then you can just "filter" on that element in a subsequent stage:

或者坦率地说,因为最初的$ geoNear阶段会将一个额外的字段“投射”到包含与查询的“原点”的“距离”的文档中,然后你可以在后续阶段“过滤”该元素:

db.places.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ -88 , 30 ]
        },
        "distanceField": "dist"
    }},
    { "$match": {
        "dist": { "$lte": 0.1 }
    }}
])

Since this is one option that can "produce/project" a value representing the distance in the result then that satisfies your first criteria. The "chaining" nature of the "aggregation framework" allows "additional filtering" or any other operation you need to perform after the filtering of the initial query.

由于这是一个可以“生成/投影”表示结果中距离的值的选项,因此满足您的第一个标准。 “聚合框架”的“链接”特性允许“过滤”或在初始查询过滤后需要执行的任何其他操作。

So $geoWithin works just as well in the aggregation framework under a $match stage as it would in any standard query since it is not "dependant" on an "index" of geospatial origin to be present. It performs better in an initial query with one, but it does not need it.

因此,$ geoWithin在$ match阶段的聚合框架中也可以像在任何标准查询中一样工作,因为它不依赖于地理空间来源的“索引”。它在一个初始查询中表现更好,但它不需要它。

Since your requirement is the "distance" from the point of origin, then the most logical thing to do is to perform an operation that will return such information. Such as this does.

由于您的要求是距离原点的“距离”,因此最合乎逻辑的做法是执行将返回此类信息的操作。比如这样做。

Would love to include all of the relevant links in this response, but as a new responder then two links is all I am allowed for now.

很想在此回复中包含所有相关链接,但作为新响应者,我现在只允许两个链接。


One more relevant note:

一个更相关的说明:

The measurement of "distance" or "radius" in any operation is dependant on how your data is stored. If it is in a "legacy" or "key/pair or plain array" format then the value will be expressed in "radians", otherwise where the data is expressed in GeoJSON format on the "location" then the "distance data" is expressed in "meters" instead.

任何操作中“距离”或“半径”的测量取决于数据的存储方式。如果它是“遗留”或“密钥/对或普通数组”格式,则该值将以“弧度”表示,否则数据在“位置”上以GeoJSON格式表示,则“距离数据”为以“米”表示。

That is an important consideration given the libraries implemented by the MongoDB service and how this interacts with the data as you have it stored. There is of course documentation on this in the official resources should you care to look at that properly. And again, I cannot add those links at this time, unless this response sees some much needed love.

考虑到MongoDB服务实现的库以及它在存储时如何与数据交互,这是一个重要的考虑因素。当然,如果您需要正确查看,请在官方资源中提供相关文档。而且,我不能在这个时候添加这些链接,除非这种反应看到了一些非常需要的爱。

#2


https://github.com/mongodb/mongo/blob/master/src/third_party/s2/s2latlng.cc#L37

GetDistance() return a S1Angle, S1Angle::radians() will return the radians.

GetDistance()返回一个S1Angle,S1Angle :: radians()将返回弧度。

This belong to s2-geometry-library(google code will close,i export it to my github. Java).

这属于s2-geometry-library(谷歌代码将关闭,我将其导出到我的github.Java)。

#1


You probably don't want mapReduce in this case but actually the aggregation framework. Apart from the general first stage query you can run via $geoNear which is more efficient in your purpose.

在这种情况下,您可能不希望使用mapReduce,但实际上是聚合框架。除了一般的第一阶段查询,您可以通过$ geoNear运行,这对您的目的更有效。

db.places.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ -88 , 30 ]
        },
        "distanceField": "dist"
    }},
    { "$match": {
        "loc": {
            "$centerSphere": [ [ -88 , 30 ] , 0.1 ]
        }
    }}
])

Or frankly, because the initial $geoNear stage will "project" an additional field into the document containing the "distance" from the queried "point of origin", then you can just "filter" on that element in a subsequent stage:

或者坦率地说,因为最初的$ geoNear阶段会将一个额外的字段“投射”到包含与查询的“原点”的“距离”的文档中,然后你可以在后续阶段“过滤”该元素:

db.places.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [ -88 , 30 ]
        },
        "distanceField": "dist"
    }},
    { "$match": {
        "dist": { "$lte": 0.1 }
    }}
])

Since this is one option that can "produce/project" a value representing the distance in the result then that satisfies your first criteria. The "chaining" nature of the "aggregation framework" allows "additional filtering" or any other operation you need to perform after the filtering of the initial query.

由于这是一个可以“生成/投影”表示结果中距离的值的选项,因此满足您的第一个标准。 “聚合框架”的“链接”特性允许“过滤”或在初始查询过滤后需要执行的任何其他操作。

So $geoWithin works just as well in the aggregation framework under a $match stage as it would in any standard query since it is not "dependant" on an "index" of geospatial origin to be present. It performs better in an initial query with one, but it does not need it.

因此,$ geoWithin在$ match阶段的聚合框架中也可以像在任何标准查询中一样工作,因为它不依赖于地理空间来源的“索引”。它在一个初始查询中表现更好,但它不需要它。

Since your requirement is the "distance" from the point of origin, then the most logical thing to do is to perform an operation that will return such information. Such as this does.

由于您的要求是距离原点的“距离”,因此最合乎逻辑的做法是执行将返回此类信息的操作。比如这样做。

Would love to include all of the relevant links in this response, but as a new responder then two links is all I am allowed for now.

很想在此回复中包含所有相关链接,但作为新响应者,我现在只允许两个链接。


One more relevant note:

一个更相关的说明:

The measurement of "distance" or "radius" in any operation is dependant on how your data is stored. If it is in a "legacy" or "key/pair or plain array" format then the value will be expressed in "radians", otherwise where the data is expressed in GeoJSON format on the "location" then the "distance data" is expressed in "meters" instead.

任何操作中“距离”或“半径”的测量取决于数据的存储方式。如果它是“遗留”或“密钥/对或普通数组”格式,则该值将以“弧度”表示,否则数据在“位置”上以GeoJSON格式表示,则“距离数据”为以“米”表示。

That is an important consideration given the libraries implemented by the MongoDB service and how this interacts with the data as you have it stored. There is of course documentation on this in the official resources should you care to look at that properly. And again, I cannot add those links at this time, unless this response sees some much needed love.

考虑到MongoDB服务实现的库以及它在存储时如何与数据交互,这是一个重要的考虑因素。当然,如果您需要正确查看,请在官方资源中提供相关文档。而且,我不能在这个时候添加这些链接,除非这种反应看到了一些非常需要的爱。

#2


https://github.com/mongodb/mongo/blob/master/src/third_party/s2/s2latlng.cc#L37

GetDistance() return a S1Angle, S1Angle::radians() will return the radians.

GetDistance()返回一个S1Angle,S1Angle :: radians()将返回弧度。

This belong to s2-geometry-library(google code will close,i export it to my github. Java).

这属于s2-geometry-library(谷歌代码将关闭,我将其导出到我的github.Java)。