简体   繁体   English

如何在 Pandas 中插入纬度/经度和航向

[英]How to interpolate latitude/longitude and heading in Pandas

Description: I have a Pandas dataframe formed by three columns: latitude [-90;90], longitude [-180;180] and direction [0;360].描述:我有一个由三列组成的 Pandas 数据框:纬度 [-90;90]、经度 [-180;180] 和方向 [0;360]。 All columns are in degrees.所有列都以度为单位。 The index is instead formed by date + time like so:索引由日期 + 时间组成,如下所示:

df = pd.DataFrame({'lat':[87,90,85,10,-40,-85,-89,-40],
                   'lon':[-150,-178,176,100,10,1,-20,-100],
                   'dir':[180,200,356,4,20,1,351,20]},
                   index = pd.to_datetime(['2019-06-17 08:29:07','2019-06-17 08:29:11', '2019-06-17 08:29:16', '2019-06-17 08:29:25', '2019-06-17 08:29:33', '2019-06-17 08:29:40', '2019-06-17 08:29:48', '2019-06-17 08:29:57']))

This is what it looks like:这是它的样子:

                     lat  lon  dir
2019-06-17 08:29:07   87 -150  180
2019-06-17 08:29:11   90 -178  200
2019-06-17 08:29:16   85  176  356
2019-06-17 08:29:25   10  100    4
2019-06-17 08:29:33  -40   10   20
2019-06-17 08:29:40  -85    1    1
2019-06-17 08:29:48  -89  -20  351
2019-06-17 08:29:57  -40 -100   20

GOAL: My goal is to add the missing datetimes between the indexes and perform an interpolation (ex linear) between the missing coordinates and angles.目标:我的目标是在索引之间添加缺失的日期时间,并在缺失的坐标和角度之间执行插值(前线性)。 I was able to add the missing dates like so:我能够像这样添加缺少的日期:

idx = pd.to_datetime(pd.date_range(df.index[0], df.index[-1], freq='s').strftime('%Y-%m-%d %H:%M:%S'))
df  = df.reindex(idx, fill_value='NaN')

                     lat   lon  dir
2019-06-17 08:29:07   87  -150  180
2019-06-17 08:29:08  NaN   NaN  NaN
2019-06-17 08:29:09  NaN   NaN  NaN
2019-06-17 08:29:10  NaN   NaN  NaN
2019-06-17 08:29:11   90  -178  200
2019-06-17 08:29:12  NaN   NaN  NaN
2019-06-17 08:29:13  NaN   NaN  NaN
...................  ...   ...  ...
2019-06-17 08:29:55  NaN   NaN  NaN
2019-06-17 08:29:56  NaN   NaN  NaN
2019-06-17 08:29:57  -40  -100   20

In order to achieve my goal I tried to use the pandas function pandas.Series.interpolate without success because it does not take into account the angle "jumps" between -180;180 for the longitude and the "jump" between 360 and 0 for the direction.为了实现我的目标,我尝试使用熊猫函数pandas.Series.interpolate没有成功,因为它没有考虑到经度在 -180;180 之间的“跳跃”角度和 360 和 0 之间的“跳跃”角度方向。

QUESTION: Could you please provide a smart and elengant way to achieve such interpolation so that it takes into account those jumps between the limits of their range?问题:您能否提供一种智能而优雅的方式来实现这种插值,以便考虑到它们范围限制之间的跳跃?

Note: here there is an example just to be more clear (interpolation between -176 and 176): -176,-177,-178,-179,-180/180,179,178,177,176?注意:这里有一个例子更清楚(-176 和 176 之间的插值):-176,-177,-178,-179,-180/180,179,178,177,176?

Here there is the answer to my question:这是我的问题的答案:

df['dir'] = np.rad2deg(np.unwrap(np.deg2rad(df['dir'])))
df['lat'] = np.rad2deg(np.unwrap(np.deg2rad(df['lat'])))
df['lon'] = np.rad2deg(np.unwrap(np.deg2rad(df['lon'])))

df  = df.reindex(idx, fill_value=np.nan)
df.reset_index(drop=False, inplace=True)
df = df.interpolate()#pd.merge(left=pd.DataFrame({'index':idx}), right=df, on='index', how='left').interpolate()

df[['lat','lon','dir']] %= 360

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM