Postgres SQL(亚马逊红移)-如何计算两个纬度和经度之间的距离?

Postgres SQL(亚马逊红移)-如何计算两个纬度和经度之间的距离?,sql,amazon-web-services,amazon-redshift,Sql,Amazon Web Services,Amazon Redshift,在Netezza中,我习惯于使用INZA软件包和ST_Distance函数。有人知道红移的类似方式吗?我使用它进行大量计算和连接。您必须在红移之外计算它 您可以尝试以下操作: GeoDis(基于redis) 博士后地理信息系统(博士后) 由于redshift实现了postgres接口,所以您可以利用postgres的功能,将它们合并到单个postgres数据源中,您可以在单个查询中进行连接 i、 例如:postgres实例(称之为“master”),安装了postgis,通过fdw连接到re

在Netezza中,我习惯于使用INZA软件包和ST_Distance函数。有人知道红移的类似方式吗?我使用它进行大量计算和连接。

您必须在红移之外计算它

您可以尝试以下操作:

  • GeoDis(基于redis)
  • 博士后地理信息系统(博士后)
由于redshift实现了postgres接口,所以您可以利用postgres的功能,将它们合并到单个postgres数据源中,您可以在单个查询中进行连接


i、 例如:postgres实例(称之为“master”),安装了postgis,通过fdw连接到redshift。通过这种方式,您可以对红移数据使用地理位置查询。

您必须在红移之外计算它

您可以尝试以下操作:

  • GeoDis(基于redis)
  • 博士后地理信息系统(博士后)
由于redshift实现了postgres接口,所以您可以利用postgres的功能,将它们合并到单个postgres数据源中,您可以在单个查询中进行连接


i、 例如:postgres实例(称之为“master”),安装了postgis,通过fdw连接到redshift。通过这种方式,您可以对红移数据使用地理位置查询。

alexanderlz建议的postgres_fdw模块不适用于红移,因为最低要求是postgres 8.1(只读)。红移目前使用Postgres 8.0.2。您可以使用它来获得类似的功能。

alexanderlz建议的postgres_fdw模块不适用于Redshift,因为最低要求是postgres 8.1(只读)。红移目前使用Postgres 8.0.2。你可以用它来获得类似的功能。

看看这个

------- DISTANCE FUNCTION ---------
CREATE FUNCTION DISTANCE (orig_lat float, orig_long float, dest_lat float, dest_long float)
  RETURNS float
STABLE  
AS $$
  import math
  r = 3963.1676          
  phi_orig = math.radians(orig_lat)
  phi_dest = math.radians(dest_lat)
  delta_lat = math.radians(dest_lat - orig_lat)
  delta_long = math.radians(dest_long - orig_long)
  a = math.sin(delta_lat/2) * math.sin(delta_lat/2) + math.cos(phi_orig) \
      * math.cos(phi_dest) * math.sin(delta_long/2) * math.sin(delta_long/2)
  c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  d = r * c
  return d
$$ LANGUAGE plpythonu
;
看看这个

------- DISTANCE FUNCTION ---------
CREATE FUNCTION DISTANCE (orig_lat float, orig_long float, dest_lat float, dest_long float)
  RETURNS float
STABLE  
AS $$
  import math
  r = 3963.1676          
  phi_orig = math.radians(orig_lat)
  phi_dest = math.radians(dest_lat)
  delta_lat = math.radians(dest_lat - orig_lat)
  delta_long = math.radians(dest_long - orig_long)
  a = math.sin(delta_lat/2) * math.sin(delta_lat/2) + math.cos(phi_orig) \
      * math.cos(phi_dest) * math.sin(delta_long/2) * math.sin(delta_long/2)
  c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  d = r * c
  return d
$$ LANGUAGE plpythonu
;

虽然PythonUDF方法没有错,但我们发现PythonUDF比原生SQLUDF花费的时间更长,消耗的资源也更多(我们在数十亿条记录上运行了这个方法)。这就是我们所使用的,它当然不是完美的(即,我们平均地球半径为3961英里,但我们不需要精确到我们的使用)


这只是使用内置sql数学函数手动计算haversine距离。这将返回以英里为单位的距离,如果您想以另一个度量单位返回距离,您可以用地球的平均半径替换
3961
,以您想要的任何单位为单位(即
6371
以公里为单位,或
6371000
以米为单位,等等)

尽管python udf方法没有错,我们发现python udf比本机sql udf花费的时间更长,消耗的资源也更多(我们在数十亿条记录上运行了这个过程)。这就是我们所使用的,它当然不是完美的(即,我们平均地球半径为3961英里,但我们不需要精确到我们的使用)

这只是使用内置sql数学函数手动计算haversine距离。这将返回以英里为单位的距离,如果您希望以另一个度量单位返回距离,您可以用您想要的任何单位(即以公里为单位的
6371
,或以米为单位的
6371000
等)替换
3961

不正确(不再?)。Redshift实现了足够的数学函数,可以使用Haversine公式或类似公式计算。不正确(不再正确)。Redshift实现了足够的数学函数,可以使用Haversine公式或类似公式来计算。