如何在postgresql中将球形坐标转换为笛卡尔坐标

时间:2014-07-12 15:49:15

标签: postgresql postgis

我试图在postgresql中将球形转换为笛卡尔坐标。

插图:

  • 输入:海拔,方位角
  • 输出:x,y,z

示例数据

hour    Elevation   Azimuth x   y   z
06:29:00    -0.833  67.72   0.379094033 0.9252439455    -0.0145380682
07:00:00    6.28    68.75   0.3602640625    0.9264147202    0.1093872546
08:00:00    20.15   69.78   0.3244723425    0.880938168 0.3444787991
09:00:00    34.06   69.39   0.2916199874    0.7754290366    0.5600603468

有任何查询或功能可以转换吗?

我尝试创建一个函数

CREATE OR REPLACE FUNCTION ST_spr2cart(point_a geometry, point_b geometry)
  RETURNS geometry AS
$BODY$SELECT ST_SetSRID(ST_MakePoint(
  Cos(a1) * Cos(b1),
  Cos (a1) * sin (b1),
  sin(a1)), ST_SRID($1))
FROM (SELECT
  ST_m($1) AS a1, 
  ST_m($2) AS b1
) AS f$BODY$
LANGUAGE sql IMMUTABLE;

创建了函数,但是当我执行它时,出现错误。

1 个答案:

答案 0 :(得分:0)

CREATE OR REPLACE FUNCTION ST_spr2cart(point_a float, point_b float)
  RETURNS geometry AS
$BODY$SELECT ST_MakePoint(
  Cos(a * pi() / 180) * Cos(b * pi() / 180),
  Cos(a * pi() / 180) * sin(b * pi() / 180),
  sin(a * pi() / 180))
FROM (SELECT
 $1 AS a, 
 $2 AS b
) AS f$BODY$
LANGUAGE sql IMMUTABLE;

select st_astext(ST_spr2cart (elevation , azimuth)) from tablename;