用地理系经纬高参数,求A,B两个目标的斜距的公式,给出python和EXCEL两种代码表示
时间: 2024-10-28 18:15:55 浏览: 38
在地理空间计算中,两点之间的斜距通常使用球面大圆距离公式(也称为 Vincenty's Inverse formula),考虑到地球并非完美的球体形状。以下是Python和Excel中计算两点之间斜距(忽略椭球体曲率的近似值)的简单示例:
### Python (Haversine公式)
```python
import math
def haversine(lat1, lon1, lat2, lon2):
R = 6371 # 地球平均半径,单位公里
dLat = math.radians(lat2 - lat1)
dLon = math.radians(lon2 - lon1)
a = math.sin(dLat / 2) * math.sin(dLat / 2) + \
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * \
math.sin(dLon / 2) * math.sin(dLon / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c
# 示例:A点经纬度 = (lat1, lon1),B点经纬度 = (lat2, lon2)
distance_AB = haversine(lat1, lon1, lat2, lon2)
```
### Excel (VBA)
```vba
Function Haversine(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double
Dim pi As Double
Dim R As Double
Dim dLat As Double, dLon As Double
Dim a As Double, c As Double
pi = 3.141592653589793
R = 6371 ' 地球平均半径,单位公里
dLat = Radians(lat2 - lat1)
dLon = Radians(lon2 - lon1)
a = Sin(dLat / 2)^2 + Cos(Radians(lat1)) * Cos(Radians(lat2)) * Sin(dLon / 2)^2
c = 2 * Atan2(Sqrt(a), Sqrt(1 - a))
Haversine = R * c
End Function
' 示例:在Excel中计算斜距,调用函数 Haversine(lat1, lon1, lat2, lon2)
```
请注意,实际应用中可能需要考虑地球扁率的影响,上述代码只适用于简单的近似计算。对于精确的地理距离计算,应该使用更专业的地理信息系统工具或库,如GDAL、PyGeod等。
阅读全文