简体   繁体   English

试图计算 PostgreSQL 中两个经纬度点之间的距离 - PostgreSQL earth_box(ll_to_earth) 返回值的单位是什么?

[英]Trying to calculate distance between two lat-lng points in PostgreSQL - what is the unit of PostgreSQL earth_box(ll_to_earth) return value?

I have a query like so:我有一个这样的查询:

SELECT * FROM chat_rooms 
WHERE earth_box(ll_to_earth(5, 5), 111000) @> ll_to_earth(lat, lng);

Essentially, I have lat = lng = 5 in the query, and in my chat_rooms table I have two entries, one with lat=lng=5 , the other with lat=lng=4 .本质上,我在查询中有lat = lng = 5 ,在我的chat_rooms表中有两个条目,一个是lat=lng=5 ,另一个是lat=lng=4 Using an online calculator, the distance between these two entries is 157 miles = 252.667km (ie entry lat=lng=4 should be returned when radius >= 157 miles. However, the query only returns the entry with lat=lng=4 when the radius is specified as ~111,000+. My question is, what unit is the radius in, and am I conducting this query correctly?使用在线计算器,这两个条目之间的距离为 157 英里 = 252.667 公里(即半径 >= 157 英里时应返回条目lat=lng=4 。但是,查询仅返回lat=lng=4时的条目半径指定为 ~111,000+。我的问题是,半径是什么单位,我是否正确执行此查询?

As per the doc , by default, the radius is expressed in meters.根据doc ,默认情况下,半径以米表示。

The most important flaw is that the distance between 4;4 and 5;5 is 157 km, not miles.最重要的缺陷是 4;4 和 5;5 之间的距离是 157 公里,而不是英里。 Even though, 111,000m is not the same as 157,000m, but the doc for earth_box says虽然,111000米是不一样的157000米,但对于文档earth_box

Some points in this box are further than the specified great circle distance from the location, so a second check using earth_distance should be included in the query.此框中的某些点距该位置的距离比指定的大圆距离更远,因此应在查询中包含使用 earth_distance 的第二次检查。

so your query should be similar to所以你的查询应该类似于

SELECT * FROM chat_rooms 
WHERE earth_box(ll_to_earth(5, 5), 111000) @> ll_to_earth(lat, lng)
AND earth_distance(ll_to_earth(5,5), ll_to_earth(lat, lng)) <= 111000

in which case entry at 5;5 won't be returned, but will be if you use 158000 instead.在这种情况下,不会返回 5;5 处的条目,但如果您使用 158000 则会返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM