简体   繁体   English

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

[英]Efficient way to convert Latitude/Longitude to XY

I have a working script that converts Latitude and Longitude coordinates to Cartesian coordinates. 我有一个工作script ,可将LatitudeLongitude坐标转换为笛卡尔坐标。 However, I have to perform this for specific points at each point in time ( row by row ). 但是,我必须在每个时间点( row by row )针对特定点执行此操作。

I want to do something similar on a larger df . 我想在较大的df上执行类似的操作。 I'm not sure if a loop that iterates over each row is the most efficient way to do this? 我不确定遍历每rowloop是否是最有效的方法? Below is the script that converts a single XY point. 下面是转换单个XY点的script

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)

This works fine if I want to convert between single points. 如果我想在单点之间进行转换,则效果很好。 The issue is if I have a large data frame or list ( >100,000 rows ) of coordinates. 问题是如果我有一个大的数据框或坐标列表( >100,000 rows )。 Would a loop that iterates through each row be inefficient. iterates每一rowloop效率低下。 Is there a better way to perform the same function? 有没有更好的方法来执行相同的功能?

Below is an example of a fractionally bigger df . 以下是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)

I will do this if I were you. 如果我是你,我会做的。 (Btw: the tuple casting part can be optimized. (顺便说一句:元组铸造部分可以优化。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中给定纬度和经度数据计算距离矩阵的有效方法 - Efficient way to calculate distance matrix given latitude and longitude data in Python 用指定半径的圆点填充经度/纬度图的最有效方法是什么? - What's the most efficient way to fill a longitude / latitude map with circular points of specified radius? 如何在熊猫数据框中将UTM转换为经度/纬度? - How to convert UTM to Longitude/Latitude in Pandas dataframe? 如何将经度、纬度、高程转换为笛卡尔坐标? - How to convert Longitude,Latitude, Elevation to Cartesian coordinates? 如何在python中将经度转换为十进制? - How to convert latitude longitude to decimal in python? 在 GeoPandas 中将纬度/经度点转换为网格多边形 - Convert Latitude/Longitude points to Grid Polygons in GeoPandas 将物理地址转换为地理位置纬度和经度 - Convert physical addresses to Geographic locations Latitude and Longitude 如何将HEXEWKB转换为Latitude,Longitude(在python中)? - How to convert HEXEWKB to Latitude, Longitude (in python)? 将纬度和经度转换为3D空间中的点 - Convert Latitude and Longitude to point in 3D space 从24位带符号转换为纬度/经度? - Convert from 24 bit signed to latitude / longitude?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM