我试图使用python来计算答案here中的步骤之后的时间有序坐标之间的距离和速度。接近代码的末尾,我遇到一个错误,表示全局名称尚未定义但显然已经定义。
这是我的数据样本
ID timestamp latitude longitude
0 72 20/01/2015 09:47 -6.646405565 71.35696828
1 72 20/01/2015 15:47 -6.642237759 71.36032005
2 72 20/01/2015 21:47 -6.639229675 71.36914769
3 73 21/01/2015 03:47 -6.648699053 71.37865551
4 73 21/01/2015 09:47 -6.65574147 71.37957366
5 74 21/01/2015 15:47 -6.660118996 71.37990588
6 74 21/01/2015 21:47 -6.666138734 71.38266541
到目前为止,我已经能够运行以下代码
import pandas as pd
df = pd.read_csv(filename)
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%d/%m/%Y %H:%M')
from math import sin, cos, sqrt, atan2, radians
def getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2):
R = 6371 # Radius of the earth in km
dLat = radians(lat2-lat1)
dLon = radians(lon2-lon1)
rLat1 = radians(lat1)
rLat2 = radians(lat2)
a = sin(dLat/2) * sin(dLat/2) + cos(rLat1) * cos(rLat2) * sin(dLon/2) * sin(dLon/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c # Distance in km
return d
def calc_velocity(dist_km, time_start, time_end):
"""Return 0 if time_start == time_end, avoid dividing by 0"""
return dist_km / (time_end - time_start).seconds if time_end > time_start else 0
# First sort by ID and timestamp:
df = df.sort_values(by=['ID', 'timestamp'])
# Group the sorted dataframe by ID, and grab the initial value for lat, lon, and time.
df['lat0'] = df.groupby('ID')['latitude'].transform(lambda x: x.iat[0])
df['lon0'] = df.groupby('ID')['longitude'].transform(lambda x: x.iat[0])
df['t0'] = df.groupby('ID')['timestamp'].transform(lambda x: x.iat[0])
# create a new column for distance
df['dist_km'] = df.apply(
lambda row: getDistanceFromLatLonInKm(
lat1=row['latitude'],
lon1=row['longitude'],
lat2=row['lat0'],
lon2=row['lon0']
),
axis=1
)
在这一点上,我得到一个错误,暗示'getDistanceFromLatLonInKm'
虽然已经定义但尚未定义。以下是回溯和错误
Traceback (most recent call last):
File "<pyshell#36>", line 9, in <module>
axis=1
File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\core\frame.py", line 4061, in apply
return self._apply_standard(f, axis, reduce=reduce)
File "C:\Python27\ArcGIS10.6\lib\site-packages\pandas\core\frame.py", line 4157, in _apply_standard
results[i] = func(v)
File "<pyshell#36>", line 3, in <lambda>
lambda row: getDistanceFromLatLonInKm(
NameError: ("global name 'getDistanceFromLatLonInKm' is not defined", u'occurred at index 0')
我在这个代码中哪里出错了?
如果您需要有关执行Python代码的不同方法的背景知识,请查看此链接。 https://realpython.com/run-python-scripts/
将以下代码复制粘贴到文件中,并将文件另存为lat_long.py。根据您的系统仅更改csv文件名'lat_long.csv'。在shell或命令提示符下,执行以下命令:
python lat_long.py。
python解释器将运行文件lat_long.py的内容并打印结果(如果有)。
import pandas as pd
from math import sin, cos, sqrt, atan2, radians
filename = 'lat_long.csv'
df = pd.read_csv(filename)
df['timestamp'] = pd.to_datetime(df['timestamp'], format='%d/%m/%Y %H:%M')
def getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2):
R = 6371 # Radius of the earth in km
dLat = radians(lat2-lat1)
dLon = radians(lon2-lon1)
rLat1 = radians(lat1)
rLat2 = radians(lat2)
a = sin(dLat/2) * sin(dLat/2) + cos(rLat1) * cos(rLat2) * sin(dLon/2) * sin(dLon/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c # Distance in km
return d
def calc_velocity(dist_km, time_start, time_end):
"""Return 0 if time_start == time_end, avoid dividing by 0"""
return dist_km / (time_end - time_start).seconds if time_end > time_start else 0
# First sort by ID and timestamp:
df = df.sort_values(by=['ID', 'timestamp'])
# Group the sorted dataframe by ID, and grab the initial value for lat, lon, and time.
df['lat0'] = df.groupby('ID')['latitude'].transform(lambda x: x.iat[0])
df['lon0'] = df.groupby('ID')['longitude'].transform(lambda x: x.iat[0])
df['t0'] = df.groupby('ID')['timestamp'].transform(lambda x: x.iat[0])
# create a new column for distance
df['dist_km'] = df.apply(
lambda row: getDistanceFromLatLonInKm(
lat1=row['latitude'],
lon1=row['longitude'],
lat2=row['lat0'],
lon2=row['lon0']
),
axis=1
)
print(df)