bd3
bd3

Reputation: 101

Separate dataframe lat/lon pairs and plot multiple figures based on column value

I have a dataframe of lat/lon pairs that form a polygon. The rows with 'GAP' and NaN's in the lat/lon columns are separators. So in this case I have 4 polygons with multiple lat/lon locations. My goal is to separate these polygons from each other and then plot using 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

Assume that with any time this is run, the amount of polygons and amount of lat/lon pairs in each polygon can change.

Sample code below of the set up:

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)

Here is where I get lost (area after set up and before plotting code) shown below.

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()

So as you can see, I replaced the 'GAP' with a NaN, then separated the rows by 'group_no', which is a new column. Then removed the Nan's. Resulting dataframe:

        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

I've attempted a few things but can't seem to close the deal. I've tried separated them in a dictionary using group_by, key being the group_no, values being lat/lon pair, but I don't know dictionaries well enough to manipulate them and plot for each 'key'.

I also attempted to separate each into a new dataframe. Thinking I could loop through and create a df0, df1, etc. and then use a for loop to plot, but couldn't figure that out either.

Any help would be appreciated and please ask if further details are needed.

Upvotes: 0

Views: 238

Answers (1)

BernieFeynman
BernieFeynman

Reputation: 121

You're almost there, if you call groupby on the group number you can just pull out each group and get the lat/lon pairs. Make sure you have proper projections set up as well.

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 ...



Upvotes: 1

Related Questions