首页 > 解决方案 > 我可以在 Python 中使用什么代码将 40,000 行纬度和经度数字(以弧度表示)转换为笛卡尔坐标?

问题描述

下表是我正在使用的数据的示例。

unimportant      latitude    longitude        lat         long
    a            52.927700    -3.243150     0.975123    -0.054621
    b            54.396837    -1.680645     0.959385    -0.029333
    c            52.902610    -1.473000     0.623323    -0.029691
    d            51.541824    0.081160      0.899576    0.000417
    e            51.073047    -0.423974     0.705355    -0.009382
                                  :
                                  :

我有一个有 5 列和 40000 行的表。1 列并不重要。第 2 列和第 3 列以度为单位显示“经度”和“纬度”,第 4 列和第 5 列以弧度显示标题为“纬度”和“经度”的纬度和经度。我的任务是添加 3 个新列 x、y、z,它们显示基于“lat”和“long”的笛卡尔坐标。我假设 z 代表海拔但是我不完全确定如何计算它。

如何计算 x、y、z 并将它们添加到表格中?

我已经尝试使用本网站上类似问题的答案中显示的代码,但是我得到 TypeError: must be real number, not str 错误。

def Cartesian_coordinates(lat,long):
    R = 6371
    x = R * math.cos(lat) * math.cos(long)
    y = R * math.cos(lat) * math.sin(long)
    z = R * math.sin(lat)
    return x,y,z

使用调用函数

Cartesian_coordinates('lat', 'long')

只有产量

TypeError: must be real number, not str

标签: pythonmath

解决方案


Cartesian_coordinates(df.latitude, df.longitude)

您应该将 args 作为值而不是字符串,例如:

'df.latitude' - this is string
df.latitude - this is value with type of df.latitude

推荐阅读