与长距离线段的距离在长距离错误 [英] Distance from long lat line segment wrong over long distances

查看:133
本文介绍了与长距离线段的距离在长距离错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的功能似乎有效,但随着我增加了距离,它是清除它不工作。我认为这是由于地球的曲率造成的。在我的计算中,罗马显示为距离线5公里。




  • 伦敦,英国 - 0公里

  • 罗马,意大利 - 5公里

  • li>


但在Google Earth上,它实际上更像是61km

当我开始走得更远时,计算变得更糟!




  • 罗马,意大利 - 0公里

  • Mohenjo-daro,巴基斯坦 - 0公里
  • 伊斯坦布尔,土耳其 - 250km





我相信这个问题出现在代码的某处:

  function distToSegment(lat1,lon1,lat2,lon2,lat3,lon3){
var y = Math.sin(lon3 - lon1)* Math.cos(lat3);
var x = Math.cos(lat1)* Math.sin(lat3) - Math.sin(lat1)* Math.cos(lat3)* Math.cos(lat3 - lat1);
var bearing1 = radiansToDegrees(Math.atan2(y,x));
bearing1 = 360 - (bearing1 + 360%360);
var y2 = Math.sin(lon2 - lon1)* Math.cos(lat2);
var x2 = Math.cos(lat1)* Math.sin(lat2) - Math.sin(lat1)* Math.cos(lat2)* Math.cos(lat2 - lat1);
var bearing2 = radiansToDegrees(Math.atan2(y2,x2));
bearing2 = 360 - (bearing2 + 360%360);
var lat1Rads = degreesToRadians(lat1);
var lat3Rads = degreesToRadians(lat3);
var dLon = degreesToRadians(lon3 - lon1);
var distanceAC = Math.acos(Math.sin(lat1Rads)* Math.sin(lat3Rads)+ Math.cos(lat1Rads)* Math.cos(lat3Rads)* Math.cos(dLon))* 6371; (数学.asin(Math.sin(distanceAC / 6371)* Math.sin(degreesToRadians(bearing1) - degreesToRadians(bearing2)))* 6371);
var min_distance = Math.abs
返回min_distance;

这是一个可以用来测试的小工具:



http://jsfiddle.net/kmturley/cfg2D/3 /



任何有助于找出这个问题的方法都将不胜感激!

解决方案

  bearing1 = 360  - (bearing1 + 360%360)

看上去很腥。你的意思是:
$ b $ pre $ bearing1 = 360 - (bearing1 + 360)%360



同样对于 bearing2

是一个乘法运算符,优先级高于 +


I am calculating how far away a point is from a line segment on the earth.

My function seemed to work, but as i've increased the distances it's clear it's not working. I presume this is due to the curvature of the earth.

In my calculations Rome is shown as 5km from the line:

  • London, UK - 0km
  • Rome, Italy - 5km
  • Cyrene, Libya - 0km

But on Google Earth it's actually more like 61km

When I start going longer distances the calculations get even worse!

  • Rome, Italy - 0km
  • Mohenjo-daro, Pakistan - 0km
  • Istanbul, Turkey - 250km

I believe the problem is somewhere in the code here:

function distToSegment(lat1, lon1, lat2, lon2, lat3, lon3) {
    var y = Math.sin(lon3 - lon1) * Math.cos(lat3);
    var x = Math.cos(lat1) * Math.sin(lat3) - Math.sin(lat1) * Math.cos(lat3) * Math.cos(lat3 - lat1);
    var bearing1 = radiansToDegrees(Math.atan2(y, x));
    bearing1 = 360 - (bearing1 + 360 % 360);
    var y2 = Math.sin(lon2 - lon1) * Math.cos(lat2);
    var x2 = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lat2 - lat1);
    var bearing2 = radiansToDegrees(Math.atan2(y2, x2));
    bearing2 = 360 - (bearing2 + 360 % 360);
    var lat1Rads = degreesToRadians(lat1);
    var lat3Rads = degreesToRadians(lat3);
    var dLon = degreesToRadians(lon3 - lon1);
    var distanceAC = Math.acos(Math.sin(lat1Rads) * Math.sin(lat3Rads) + Math.cos(lat1Rads) * Math.cos(lat3Rads) * Math.cos(dLon)) * 6371;  
    var min_distance = Math.abs(Math.asin(Math.sin(distanceAC / 6371) * Math.sin(degreesToRadians(bearing1) - degreesToRadians(bearing2))) * 6371);
    return min_distance;
}

Here is a working fiddle you can use to test:

http://jsfiddle.net/kmturley/cfg2D/3/

Any help to figure this one out would be appreciated!

解决方案

This

bearing1 = 360 - (bearing1 + 360 % 360)

looks fishy to me. Do you mean

bearing1 = 360 - (bearing1 + 360) % 360

?

Likewise for bearing2.

% is a multiplicative operator and has higher precedence than +.

这篇关于与长距离线段的距离在长距离错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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