单独的数据帧纬度/经度对,并根据列值绘制多个图形

问题描述 投票:0回答:1

我有一个纬度/经度对构成一个多边形的数据框。纬度/经度列中带有“ GAP”和“ NaN”的行是分隔符。因此,在这种情况下,我有4个具有多个纬度/经度位置的多边形。我的目标是将这些多边形彼此分开,然后使用Cartopy进行绘制。

    0      1           2
0   POINT  87.6298     9.397332
1   POINT  87.8435     9.842206
2   POINT  87.2354     9.472004
4     GAP         NaN       NaN
5   POINT  87.8354     9.397332
6   POINT  87.9544     9.472004
7   POINT  87.9632     9.191509
8   POINT  87.6244     9.221509
9   POINT  87.4554     9.397332
10    GAP         NaN       NaN
11  POINT  87.6249     9.397332
12  POINT  87.7556     9.221509
13  POINT  87.5567     9.086767
14  POINT  87.3222     9.397332
15    GAP         NaN       NaN
16  POINT  87.6554     9.221509
17  POINT  87.9667     9.191509
18  POINT  87.8854     9.056767
19  POINT  87.4452     9.086767

假设在任何时候运行,每个多边形中的多边形数量和纬度/经度对的数量都可以改变。

下面的示例代码:

df = pd.read_excel(xl, sheet_name=0, header=None)
#change column names
df.rename(columns={1:'lon', 2:'lat'},inplace=True)
#replace GAP with nan so i can separate by group numbers with each line of nans (didnt work with 'GAP')
df.replace('GAP',np.nan, inplace=True)
df['group_no'] = df.isnull().all(axis=1).cumsum()
#define amount of unique numbers and put into list for looping
numbers = df['group_no'].unique()

aa = list(numbers)

这里是我迷路的地方(设置之后和绘制代码之前的区域),如下所示。

a_lon, a_lat = 87.8544, 8.721576
b_lon, b_lat = 87.6554, 8.585951


fig,ax = plt.subplots()
plt.figure(figsize=(10.6,6))
proj = ccrs.PlateCarree()

ax = plt.axes(projection=proj)
ax.stock_img()
ax.set_extent([90, 85, 7, 11], crs=ccrs.PlateCarree()) 


proj = ccrs.Geodetic()
plt.plot([a_lon, b_lon], [a_lat, b_lat], linewidth=1, color='blue', transform=proj)
#plt.show()

如您所见,我用NaN替换了'GAP',然后用新列'group_no'分隔了行。然后取出楠氏。结果数据框:

        0         lon       lat  group_no
0   POINT  87.6298     9.397332         0
1   POINT  87.8435     9.842206         0
2   POINT  87.2354     9.472004         0
4   POINT  87.8354     9.397332         1
5   POINT  87.9544     9.472004         1
6   POINT  87.9632     9.191509         1
7   POINT  87.6244     9.221509         1
8   POINT  87.4554     9.397332         1
10  POINT  87.6249     9.397332         2
11  POINT  87.7556     9.221509         2
12  POINT  87.5567     9.086767         2
13  POINT  87.3222     9.397332         2
15  POINT  87.6554     9.221509         3
16  POINT  87.9667     9.191509         3
17  POINT  87.8854     9.056767         3
18  POINT  87.4452     9.086767         3

我尝试了一些尝试,但似乎无法达成交易。我试过使用group_by在字典中将它们分开,键是group_no,值是经/纬对,但是我对字典的了解不足,无法操纵它们并为每个“键”作图。

我还试图将它们分成一个新的数据框。认为我可以遍历并创建df0,df1等,然后使用for循环进行绘制,但也无法弄清楚。

任何帮助将不胜感激,请询问是否需要更多详细信息。

python pandas loops dictionary latitude-longitude
1个回答
0
投票

您快到了,如果您在组号上呼叫groupby,您可以拔出每个组并获得经纬度对。确保您也设置了正确的投影。

from shapely.geometry import Polygon

for group_no,group_data in df.groupby('group_no'):
    poly_coords = group_data[['lon','lat']].values

    # Whatever function you are using to create shape with the 'poly_coords e.g.'
    polygon = Polygon(poly_coords)

    #add to map ...



© www.soinside.com 2019 - 2024. All rights reserved.