如何计算PostGIS中两点之间的方位角? [英] How to calculate the Azimuth between two points in PostGIS?

查看:1082
本文介绍了如何计算PostGIS中两点之间的方位角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于OpenLayers的GIS API.我一直试图在JavaScript中实现方位角计算,因此我需要某种方式来计算方位角以便执行测试.

I have a GIS API based on OpenLayers. I have been trying to implement Azimuth calculation in JavaScript and I needed some way to calculate the Azimuth in order to perform tests.

我开始使用PostGIS,但是似乎有很多方法可以计算两点之间的方位角.我给你看了其中的三个,有些返回不同的结果.

I started using PostGIS, but there seem to be many ways to calculate the Azimuth between two points. I show you three of them and, some return different results.

-- Example 1 - Result 90

SELECT ST_Azimuth(
   ST_Transform(st_geomfromtext('POINT(-81328.998084106 7474929.8690234)', 900913), 4326),
   ST_Transform(st_geomfromtext('POINT(4125765.0381464 7474929.8690234)', 900913), 4326)
)/pi()*180

-- Example 2 - Result 155.692090425822
SELECT degrees(
   ST_Azimuth(
   ST_MakePoint(-81328.998084106, 7474929.8690234)::geography,
   ST_MakePoint(4125765.0381464, 7474929.8690234)::geography)
)

-- Example 3 - Result 90

SELECT degrees(
   ST_Azimuth(
   ST_MakePoint(-81328.998084106, 7474929.8690234),
   ST_MakePoint(4125765.0381464, 7474929.8690234))
)

在PostGIS中计算方位角的正确方法是什么?当然,我想考虑测地线坐标.

What is the correct way to calculate the Azimuth in PostGIS? Of course, I want to consider geodesic coordinates.

有没有办法知道PostGIS如何实现计算?我的意思是,有可能看到"ST_Azimuth"功能的实现方式吗?

Is there a way to know how PostGIS realizes the calculations? I mean, is it possible to see the way the "ST_Azimuth" function is implemented?

推荐答案

首先要回答您的第二个问题,基本上有两种类型的方位角计算,一种是在平面坐标上完成的,另一种是在球体或椭球形上完成的,地理数据类型的情况. 维基百科方位角文章对此进行了很好的解释.

To answer your second question first, there are basically two types of azimuth calculation, those done on planar coordinates, and those done on a sphere or spheroid, as is the case with the geography datatype. The wikipedia azimuth article explains this well.

在第一种情况下,计算非常简单,实际上只是两对点之间的atan.您可以在方法azimuth_pt_pt github上查看源代码. >.

In the first case the calculation is extremely simple, being essentially just the atan between the two pairs of points. You can see the source code on github in the method azimuth_pt_pt.

在第二种情况下,计算较为复杂,在一个球体上,您可以在

For the 2nd case, the calculation is a bit more complex, being on a sphere, and you can find the actual calculation again on github in the method spheroid_direction.

原因2不同是因为您使用的地理数据类型应在[-180,180],[-90,90]范围内.实际上,我很惊讶它没有给出错误.

The reason 2 is different is because you are using the geography datatype which should be in the range [-180,180],[-90,90]. Actually, I am surprised it doesn't give an error.

示例1和3本质上是相同的,因为您只是从一个坐标参考系切换到了另一个坐标参考系,所以这些点的相对方向不变.

Examples 1 and 3 are essentially the same, as you have simply switched from one coordinate reference system to another, so the relative direction of the points is unchanged.

没有正确的方法,但是,如果要使用geodetic coordinates,请使用geography datatype.

There is no correct way to do it, but if you want to use geodetic coordinates, then use the geography datatype.

请注意,两者之间存在差异

Note there is a difference between:

select degrees(
   st_azimuth(
       st_makepoint(0, 0)::geography, 
       st_makepoint(45, 45)::geography)
);

产生 35.4100589051161

同时

select degrees(
    st_azimuth(
       st_setsrid(st_makepoint(0, 0),4326),
       st_setsrid(st_makepoint(45, 45),4326))
);

收益 45 .一种是在平面上进行点对点方位角的测量,另一种是按照其功能名称的建议进行椭球体的方位测量.

yields 45. One is doing point to point azimuth on the plane, while the other is doing the spheroid azimuth, as suggested by their function names.

这篇关于如何计算PostGIS中两点之间的方位角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆