gpt4 book ai didi

postgresql - 按距离查询和使用 postgres 中的 earthdistance 模块有什么区别?

转载 作者:行者123 更新时间:2023-11-29 13:48:03 24 4
gpt4 key购买 nike

在 postgres 中,我知道有两种基于距离进行查询的方法。

第一个是使用特定算法“按距离查询”(如此处所示http://daynebatten.com/2015/09/latitude-longitude-distance-sql/)。

SELECT zcta.*, 
3958.755864232 * 2 *
ASIN(SQRT(POWER(SIN((41.318301 - zcta.latitude) *
PI() / 180 / 2), 2) + COS(41.318301 * PI() / 180) *
COS(zcta.latitude * PI() / 180) *
POWER(SIN((-83.6174935 - zcta.longitude) *
PI() / 180 / 2), 2))) AS distance,
MOD(CAST((ATAN2( ((zcta.longitude - -83.6174935) / 57.2957795),
((zcta.latitude - 41.318301) / 57.2957795)) *
57.2957795) + 360 AS decimal), 360) AS bearing
FROM "zcta"
WHERE (zcta.latitude BETWEEN 40.59464208444576
AND 42.04195991555424
AND zcta.longitude BETWEEN -84.58101890178294
AND -82.65396809821705
AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((41.318301 - zcta.latitude) *
PI() / 180 / 2), 2) + COS(41.318301 * PI() / 180) * COS(zcta.latitude *
PI() / 180) * POWER(SIN((-83.6174935 - zcta.longitude) *
PI() / 180 / 2), 2))))
BETWEEN 0.0
AND 50)
ORDER BY distance ASC

第二个是用于地理空间查询的地球距离模块 ( https://www.postgresql.org/docs/8.3/static/earthdistance.html)。

select * 
from zcta
where earth_box(ll_to_earth(41.318301, -83.6174935), 63067.2) @>
ll_to_earth(zcta.latitude, zcta.longitude)

这里有什么区别?哪个更好用?哪个更准确?它们各自是如何工作的?

最佳答案

回答有点晚,但我的补充。第一个是你自己的解决方案,而后者是一个名为 earthdistancePostgresql extensionmodule .要在 psql 中安装:

CREATE EXTENSION IF NOT EXISTS earthdistance;

您可能需要先对 cube 执行相同的操作,因为它们共享资源。或者……

CREATE EXTENSION IF NOT EXISTS earthdistance CASCADE;

这是 documentation .

关于postgresql - 按距离查询和使用 postgres 中的 earthdistance 模块有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45271277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com