PostgreSQL+PostGIS 函数,为一对(纬度、经度)点提供角度分离
我编写了以下代码(在花了几个小时尝试正确引用之后):
CREATE FUNCTION ast3angsep(double precision, double precision, double precision, double precision) RETURNS double precision AS $$
SELECT ST_Distance( ST_GeographyFromText('SRID=4326;POINT('||$1||' '||$2||')'), ST_GeographyFromText('SRID=4326;POINT('||$3||' '||$4||')') )/6370986.0/PI()*180.0;
$$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
这个函数可以工作,但不幸的是,它的精度非常糟糕。似乎应该有一种更简单的方法来做到这一点。我只想将值传递给函数并让它将它们传递给 ST 函数。我完全无法找到一种方法来做到这一点,而不先将它们转换为字符串,这可能是精度损失的原因。
我这样做完全搞砸了吗?
I wrote the following code (after spending a few hours trying to get the quoting correct):
CREATE FUNCTION ast3angsep(double precision, double precision, double precision, double precision) RETURNS double precision AS $
SELECT ST_Distance( ST_GeographyFromText('SRID=4326;POINT('||$1||' '||$2||')'), ST_GeographyFromText('SRID=4326;POINT('||$3||' '||$4||')') )/6370986.0/PI()*180.0;
$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
This function works but, unfortunately, it's precision is HORRIBLE. It seems like there should be a much simpler way to do this. I just want to pass the values to the function and have it pass them to the ST functions. I was completely unable to find a way of doing this without converting them to string first, which is probably where the loss of precision enters.
Am I doing this totally screwed up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
ST_GeographyFromText
转换为字符串并创建点,而是尝试使用 ST_MakePoint< /a> 直接使用函数接收到的值。Instead of converting to strings and creating a point using
ST_GeographyFromText
, try using ST_MakePoint directly with the values received by the funciton.