python函数——根据经纬度计算距离公式的错误及修正

前端之家收集整理的这篇文章主要介绍了python函数——根据经纬度计算距离公式的错误及修正前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 函数
  2. import math
  3. def cal_dis(latitude1,longitude1,latitude2,longitude2):
  4. latitude1 = (Math.PI/180)*latitude1
  5. latitude2 = (Math.PI/180)*latitude2
  6. longitude1 = (Math.PI/180)*longitude1
  7. longitude2= (Math.PI/180)*longitude2
  8. #因此AB两点的球面距离为:{arccos[sinb*siny+cosb*cosy*cos(a-x)]}*R
  9. #地球半径
  10. global R = 6378.1;
  11. d = math.acos(math.sin(latitude1)*math.sin(latitude2)+\
  12. math.cos(latitude1)*math.cos(latitude2)*math.cos(longitude2-longitude1))*R
  13. return d;
  14. }

实现了根据输入两点经纬度,计算这两点距离的函数,但是在实际操作过程中,出现了报错:


下面是会报错的数据集:

经过搜索相关文章,最终发现是由于acos(x)中的x越界引起的。

语法

方法的语法:

Highlighter_972490" class="SyntaxHighlighter py" style="width:651.111145019531px;line-height:25.2000007629395px;overflow:auto !important;">
nofollow" class="toolbar_item command_help help" style="text-decoration:none;color:rgb(0,102,153);border:0px !important;line-height:1.1em !important;overflow:visible !important;text-align:center !important;vertical-align:baseline !important;min-height:auto !important;display:block !important;background:none !important;">?

猜你在找的Python相关文章