【发布时间】:2020-11-27 05:15:13
【问题描述】:
使用Haversine公式计算大圆上的距离,我使用以下代码计算已知起始位置(lat1/lon1)和已知目的地(lat2/lon2)之间任意点的坐标:
这是完整的代码:
from math import radians, sin, cos, acos, atan2, sqrt, pi
#enter the following numbers in the corresponding input fields:
#lat1 = starting latitude = 33.95
#lon1 = starting longitude = -118.40
#lat2 = destination latitude = 40.6333
#lon2= destination longitude = -73.7833
lat1 = radians(float(input("Starting latitude: ")))
lon1 = radians(float(input("Starting longitude: ")))
lat2 = radians(float(input("Destination latitude: ")))
lon2 = radians(float(input("Destination longitude: ")))
#Haversine formula to calculate the distance, in radians, between starting point and destination:
d = ((6371.01 * acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1 - lon2)))/1.852)/(180*60/pi)
import numpy as np
x = np.arange(0, 1, 0.2)
for f in x:
A=sin((1-f)*d)/sin(d)
B=sin(f*d)/sin(d)
x = A*cos(lat1)*cos(lon1) + B*cos(lat2)*cos(lon2)
y = A*cos(lat1)*sin(lon1) + B*cos(lat2)*sin(lon2)
z = A*sin(lat1) + B*sin(lat2)
lat_rad=atan2(z,sqrt(x**2+y**2))
lon_rad=atan2(y,x)
lat_deg = lat_rad*180/pi
lon_deg = lon_rad*180/pi
print('%.2f' %f, '%.4f' %lat_deg, '%.4f' %lon_deg)
我使用np.arange() 函数在 0(起点)和 1(终点)之间进行小数迭代 f。
for循环的输出是:
0.00 33.9500 -118.4000
0.20 36.6040 -110.2685
0.40 38.6695 -101.6259
0.60 40.0658 -92.5570
0.80 40.7311 -83.2103
其中,第一个数字是分数(f);第二个数字是纬度(lat_deg),第三个数字是经度(lon_deg)。
我的问题是:如何将我的代码输出转换为 pandas (3x6) 数据帧,其中数据排列在 3 列中,标题为 Fraction (col1)、Latitude (col2)、Longitude (col3)?
一旦输出在 pandas 数据框中,我就可以轻松地将数据写入 CSV 文件。
【问题讨论】:
-
什么是d?它没有在您的代码中列出
-
我在代码中添加了 d(=起点和目的地之间的距离)的计算。
标签: pandas export-to-csv