File size: 6,342 Bytes
9953d1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import io
import pandas as pd
import geopandas
import mercury as mr

import matplotlib.pyplot as plt
import contextily as cx

import numpy as np

def adjust_coordinates(row):
    row.lat += np.random.normal(0, 2e-2)
    row.lon += np.random.normal(0, 2e-2)
    return row
    

def load_data():
    df_receiver = pd.read_csv("../receiver.csv", encoding="latin-1", delimiter=";")
    
    df_fishes = pd.read_excel("../20230310_Receiver_Polen.xlsx")
    df_stoer_ids = pd.read_excel("../20230310_Receiver_Polen.xlsx",
                                 sheet_name = "eingesetzte Akustik-Tags")
    
    df_fishes_vemco = pd.read_excel("../Daten Vemco Receiver.xlsx", decimal=',').copy()
    #df_stoer_ids_vemco = pd.read_excel("Daten Vemco Receiver.xlsx",)
                                 #sheet_name = "eingesetzte Akustik-Tags")
    df_fishes_vemco = df_fishes_vemco.drop(columns=["Longitude", "Latitude"])
    
    df_receiver_vemco = pd.read_csv("../Sturgeons.txt",
                                    delimiter=",").copy()
    
    df_fishes_vemco_m = pd.merge(left=df_fishes_vemco, right=df_receiver_vemco, how="left", on="Receiver", suffixes=("", "_y"))
    
    receiver = df_receiver.copy()
    receiver_vemco = df_receiver_vemco.copy()
    
    receiver_formatted = receiver_vemco.rename(columns={
                                  "Receiver" : "receiver_sn",
                                  "Latitude": "lat",
                                  "Longitude": "lon",
                                  "Station.name" : "station_name"})[["receiver_sn",
                                                                     "lat", "lon",
                                                                     "station_name"]]
    all_receivers = pd.concat([receiver, receiver_formatted])
    
    fishes = df_fishes.copy()
    fishes = fishes.loc[fishes[" ID"].isin(df_stoer_ids["ID"])]
    fishes = fishes.rename(columns={" Receiver":"receiver_sn"})
    fishes = pd.merge(fishes, all_receivers[["lon", "lat", "receiver_sn"]],
                      on="receiver_sn", how="left")
    
    
    df_fishes_vemco_form = df_fishes_vemco_m.drop(columns="ID")
    df_fishes_vemco_form = df_fishes_vemco_form.rename(columns={"Date.and.Time.nocor": "Date and Time (UTC)",
                                                      "Longitude" : "lon",
                                                      "Latitude": "lat",
                                                      "Transmitter": " ID",
                                                      "Receiver" : "receiver_sn"
                                                      })
    
    all_fishes = pd.concat([fishes, df_fishes_vemco_form])
    
    
    all_fishes = all_fishes.drop_duplicates([" ID", "receiver_sn"]).reset_index().drop(columns="index")
    all_fishes.index = list(all_fishes.index)


    
    all_fishes = all_fishes.apply(adjust_coordinates, axis=1)
    
    gdf_fishes = geopandas.GeoDataFrame(
        all_fishes, geometry=geopandas.points_from_xy(
            all_fishes.lon, all_fishes.lat), crs = 4326)
    
    receiver_vemco = df_receiver_vemco.copy()
    
    df_wm = gdf_fishes.to_crs(epsg=3857)
    
    gdf_receiver = geopandas.GeoDataFrame(
        all_receivers,
        geometry=geopandas.points_from_xy(all_receivers.lon, all_receivers.lat),
        crs = 4326)
    
    gdf_receiver['coords'] = gdf_receiver.to_crs(epsg=3857)['geometry'].apply(
        lambda x: x.representative_point().coords[:])
    
    gdf_receiver['coords'] = [coords[0] for coords in gdf_receiver['coords']]

    return gdf_receiver, df_wm

def dontcall():

    gdf_receiver = pd.merge(gdf_receiver,
                            df_wm.groupby("receiver_sn")["ID"].count().reset_index().rename(columns={" ID": "fish_count"}),
                            on="receiver_sn", how="left")
    #gdf_receiver = gdf_receiver.rename(columns={" ID_x": "fish_count"})
    gdf_receiver.fillna(0, inplace=True)
    
    gdf_receiver.drop_duplicates("receiver_sn")
    
    
    ax = df_wm.plot(figsize=(20, 20), alpha=1, edgecolor='k')
    
    
    gdf_receiver.drop_duplicates("receiver_sn")[:7].to_crs(epsg=3857).plot(ax=ax, marker="^",
                                        markersize=300, color="r",
                                        alpha=.45,
                                        label="Receiver Station")
    
    gdf_receiver.drop_duplicates("receiver_sn")[7:].to_crs(epsg=3857).plot(ax=ax, marker="^",
                                        markersize=100, color="g",
                                        alpha=.45,
                                        label="Receiver Station Polen")
    
    cx.add_basemap(ax)
    for idx, row in gdf_receiver.drop_duplicates("receiver_sn").iterrows():
        plt.annotate(text=int(row["fish_count"]), xy=row['coords'],
                     horizontalalignment=str(np.random.choice(['left'])), size=20)
    
    
    ax = gdf_receiver[:7].to_crs(epsg=3857).plot( marker="^", markersize=300, color="r", alpha=.6, label="Receiver Station",  figsize=(15, 20))
    ax = gdf_receiver[7:].to_crs(epsg=3857).plot( ax=ax, marker="^", markersize=150, color="b", alpha=.6, label="Receiver Station Vemco",  figsize=(15, 20))
    
    
    df_wm.loc[df_wm[" ID"].isin(only_one.index)].plot(ax=ax, cmap="tab20b", markersize=100, figsize=(15, 20), alpha=.7, edgecolor='k', categorical=True, column=" ID", legend=True, legend_kwds={'bbox_to_anchor':(1, 1.05),'fontsize':16,'frameon':True}, label="an einem Receiver")
    df_wm.loc[df_wm[" ID"].isin(two_to_four.index)].plot(cmap="tab20c", markersize=100, marker="s", ax=ax, figsize=(15, 20), alpha=.8, edgecolor='k', categorical=True, column=" ID", legend=False, legend_kwds={'bbox_to_anchor':(1, 1.05),'fontsize':16,'frameon':True}, label="an zwei bis vier Receivern")
    df_wm.loc[df_wm[" ID"].isin(more_than_four.index)].plot( ax=ax, markersize=120, marker="d", figsize=(15, 20), alpha=.7, edgecolor='k', categorical=True, column=" ID", legend=False, legend_kwds={'bbox_to_anchor':(1, 1.05),'fontsize':16,'frameon':True}, label="an mehr als vier Receivern")
    
    cx.add_basemap(ax)
    
    
    ax.legend(loc="upper right")
    
    for idx, row in gdf_receiver.iterrows():
        plt.annotate(text=str(" "), xy=row['coords'],
                     horizontalalignment='left', size=20)