将纬度/经度转换为XY的有效方法

时间:2018-11-08 04:16:11

标签: python pandas coordinates

我有一个有效的script,可将LatitudeLongitude的坐标转换为笛卡尔坐标。但是,我必须在每个时间点(row by row)的特定时间点执行此操作。

我想在较大的df上执行类似的操作。我不确定在每个loop上进行迭代的row是否是最有效的方法?下面是script,它可以转换单个XY点。

import math
import numpy as np
import pandas as pd

point1 = [-37.83028766, 144.9539561]

r = 6371000 #radians of earth meters

phi_0 = point1[1]
cos_phi_0 = math.cos(np.radians(phi_0))

def to_xy(point, r, cos_phi_0):
    lam = point[0]
    phi = point[1]
    return (r * np.radians(lam) * cos_phi_0, r * np.radians(phi))

point1_xy = to_xy(point1, r, cos_phi_0)

如果我想在单点之间进行转换,这很好用。问题是,如果我有一个较大的数据框或坐标列表(>100,000 rows)。 loopiterates到每个row的{​​{1}}效率低下。有没有更好的方法来执行相同的功能?

以下是df稍大的示例。

d = ({
    'Time' : [0,1,2,3,4,5,6,7,8],       
    'Lat' : [37.8300,37.8200,37.8200,37.8100,37.8000,37.8000,37.7900,37.7900,37.7800],       
    'Long' : [144.8500,144.8400,144.8600,144.8700,144.8800,144.8900,144.8800,144.8700,144.8500],                               
     })

df = pd.DataFrame(data = d)

1 个答案:

答案 0 :(得分:0)

如果我是你,我会做的。 (顺便说一句:元组铸造部分可以优化。

import numpy as np
import pandas as pd

point1 = [-37.83028766, 144.9539561]

def to_xy(point):

    r = 6371000  #radians of earth meters
    lam,phi = point
    cos_phi_0 = np.cos(np.radians(phi))


    return (r * np.radians(lam) * cos_phi_0,
            r * np.radians(phi))

point1_xy = to_xy(point1)
print(point1_xy)

d = ({
    'Lat' : [37.8300,37.8200,37.8200,37.8100,37.8000,37.8000,37.7900,37.7900,37.7800],       
    'Long' : [144.8500,144.8400,144.8600,144.8700,144.8800,144.8900,144.8800,144.8700,144.8500],                               
     })

df = pd.DataFrame(d)

df['to_xy'] = df.apply(lambda x: 
         tuple(x.values),
         axis=1).map(to_xy)

print(df)
相关问题