前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >经纬度距离计算 python_Python已知两坐标求距离

经纬度距离计算 python_Python已知两坐标求距离

作者头像
全栈程序员站长
发布2022-09-29 11:15:28
6110
发布2022-09-29 11:15:28
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
#coding: utf-8
#封装函数
import math
def cal_dis(latitude1, longitude1,latitude2, longitude2):
	latitude1 = (Math.PI/180)*latitude1
	latitude2 = (Math.PI/180)*latitude2
	longitude1 = (Math.PI/180)*longitude1
	longitude2= (Math.PI/180)*longitude2
	#因此AB两点的球面距离为:{arccos[sinb*siny+cosb*cosy*cos(a-x)]}*R
	#地球半径
	global R = 6378.1;
	d =  math.acos(math.sin(latitude1)*math.sin(latitude2)+\
		 math.cos(latitude1)*math.cos(latitude2)*math.cos(longitude2-longitude1))*R
	return d;
	}

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

ValueError: math domain error

下面是测试数据,这部分是正确的数据

代码语言:javascript
复制
print cal_dis(39.762146, -104.98248,39.758066, -104.902431)
print cal_dis(39.77455175, -105.01426466666665,39.76241472, -104.90343176000002)
print "横向一个纬度的距离:",cal_dis(39, -104,40, -104)
print "纵向一个经度的距离:",cal_dis(39, -105,39, -104)
print cal_dis(39.772779500000006, -104,39.762146, -104)

下面是会报错的数据集:

代码语言:javascript
复制
print "error"
print cal_dis(-6.174444, 106.829444, -6.174444, 106.82944400000001)
print cal_dis(59.439339, 24.74682, 59.439339, 24.74682)
print cal_dis(59.439339, 24.74682, 59.439339, 24.74682)
print cal_dis(59.439339, 24.74682, 59.439339, 24.74682)
print cal_dis(37.647464, -77.624973, 37.647464, -77.624973)

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

语法

以下是acos()方法的语法:

?

1

acos(x)

注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数。 参数

  • x — 这必须是在范围内的数字值-1到1,如果x大于1,则它会产生一个错误。

返回值

此方法返回的X反余弦,以弧度表示。

解决方案:

查看越界代码,使用repr将数字转化为字符串显示、查看

代码语言:javascript
复制
temp = math.sin(latitude1)*math.sin(latitude2)+\
		 math.cos(latitude1)*math.cos(latitude2)*math.cos(longitude2-longitude1)
代码语言:javascript
复制
<pre name="code" class="python">temp=math.sin(latitude1)*math.sin(latitude2)+\
		 math.cos(latitude1)*math.cos(latitude2)*\
		 math.cos(longitude2-longitude1)
	print temp,repr(temp)
代码语言:javascript
复制

发现打印结果是:1.0 1.0000000000000002

也就是实际的acos()函数的自变量是1.0000000000000002, 已经超出1的范围,由于精度问题只显示了1.0。

代码语言:javascript
复制
	if temp>1.0:		print format(temp,".19e")

可看出在科学计数法下的输出

>>1.0000000000000002220e+00

推导公式本身出问题的机率很小,但计算机中的浮点数舍入误差会放大、缩小数据,从而溢出。因此代码改为:

代码语言:javascript
复制
#coding: utf-8#封装函数import mathdef cal_dis(latitude1, longitude1,latitude2, longitude2):	latitude1 = (math.pi/180.0)*latitude1	latitude2 = (math.pi/180.0)*latitude2	longitude1 = (math.pi/180.0)*longitude1	longitude2= (math.pi/180.0)*longitude2	#因此AB两点的球面距离为:{arccos[sina*sinx+cosb*cosx*cos(b-y)]}*R  (a,b,x,y)	#地球半径	R = 6378.1	temp=math.sin(latitude1)*math.sin(latitude2)+\		 math.cos(latitude1)*math.cos(latitude2)*math.cos(longitude2-longitude1)	if repr(temp)>1.0:		 temp = 1.0	d = math.acos(temp)*R	return d;	

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/193577.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月15日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档