首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有半径的Postgresql接地距离- earth_box

具有半径的Postgresql接地距离- earth_box
EN

Stack Overflow用户
提问于 2014-08-29 23:23:38
回答 2查看 6.7K关注 0票数 8

请你解释一下earth_box函数的这种行为.或者我做错了什么?

所用数据

代码语言:javascript
复制
40.749276, -73.985643 = Empire State Building - is in my table
40.689266, -74.044512 = Statue of Liberty - is my current position in select - 8324m far from Empire State Building

我的桌子

代码语言:javascript
复制
=> select id, latitude, longitude, title from requests;
 id | latitude  | longitude  |         title
----+-----------+------------+-----------------------
  1 | 40.749276 | -73.985643 | Empire State Building

帝国大厦到自由女神像的距离

代码语言:javascript
复制
=> SELECT id, latitude, longitude, title, earth_distance(ll_to_earth(40.689266, -74.044512), ll_to_earth(latitude, longitude)) as distance_from_current_location FROM requests ORDER BY distance_from_current_location ASC;
 id | latitude  | longitude  |         title         | distance_from_current_location
----+-----------+------------+-----------------------+--------------------------------
  1 | 40.749276 | -73.985643 | Empire State Building |               8324.42998846164

我目前的位置是自由雕像,距离帝国大厦8000多米,但选择返回行与id 1,即使半径只有5558米!你能解释一下我的这种行为或出了什么问题吗?

代码语言:javascript
复制
=> SELECT id,latitude,longitude,title FROM requests WHERE earth_box(ll_to_earth(40.689266, -74.044512), 5558) @> ll_to_earth(requests.latitude, requests.longitude);
 id | latitude  | longitude  |         title
----+-----------+------------+-----------------------
  1 | 40.749276 | -73.985643 | Empire State Building

扩展和postgresql的版本

代码语言:javascript
复制
=> \dx
                                     List of installed extensions
      Name      | Version |   Schema   |                         Description
 ---------------+---------+------------+--------------------------------------------------------------  cube          | 1.0     | public     | data type for multidimensional
 cubes  earthdistance | 1.0     | public     | calculate great-circle
 distances on the surface of the Earth  plpgsql       | 1.0     |
 pg_catalog | PL/pgSQL procedural language

 => select version();
                                                                version
 --------------------------------------------------------------------------------------------------------------------------------------  PostgreSQL 9.4beta2 on x86_64-apple-darwin13.3.0, compiled by Apple
 LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn), 64-bit

谢谢诺伊

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-03 09:53:53

这里的问题是,earth_box得到的规约是迈尔斯。8324.42998846164米在5.172560986623845法规英里单元转换器附近

解决方案:将半径转化为成文法单位

earth_box(ll_to_earth(40.689266, -74.044512), 5558/1.609) //doesn't return results

earth_box(ll_to_earth(40.689266, -74.044512), 9000/1.609) //does.

票数 10
EN

Stack Overflow用户

发布于 2020-06-14 00:12:05

根据文档,默认情况下,半径以米表示。

我刚刚进行了测试,从文档中可以看出,您在WHERE子句语句中同时需要earth_box和earth_distance。

因此,您需要同时使用earth_box和earth_distance来获得正确的结果。

也是在文档中的earth_box函数描述中,它说:

此框中的某些点比指定的大圆距离更远,因此应该在查询中包括使用earth_distance的第二次检查。

因此,下面将返回结果

代码语言:javascript
复制
SELECT  earth_distance(ll_to_earth(40.749276, -73.985643), ll_to_earth(40.689266,-74.044512)) distance
FROM (SELECT 1) test
WHERE
    (earth_box(ll_to_earth(40.749276, -73.985643), 9000) @> ll_to_earth(40.689266,-74.044512))
              AND earth_distance(ll_to_earth(40.749276, -73.985643), ll_to_earth(40.689266,-74.044512)) <= 9000
     order by distance desc

但这并不是因为实际距离大约是8324.429988461638米

代码语言:javascript
复制
SELECT  earth_distance(ll_to_earth(40.749276, -73.985643), ll_to_earth(40.689266,-74.044512)) distance
FROM (SELECT 1) test
WHERE
    (earth_box(ll_to_earth(40.749276, -73.985643), 6000) @> ll_to_earth(40.689266,-74.044512))
              AND earth_distance(ll_to_earth(40.749276, -73.985643), ll_to_earth(40.689266,-74.044512)) <= 6000
     order by distance desc
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25577461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档