-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsats.py
218 lines (181 loc) · 7.87 KB
/
sats.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import csv
import pandas
import csv
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
import numpy
import os
import struct
from matplotlib.patches import Circle
rEarth = 6371000 #radius of the earth in meters
# Input the file name.
# fname = raw_input("Enter file name WITHOUT extension: ")
# def latSign(value):
# if value=='S':
# return -1
# else:
# return 1
# def longSign(value):
# if value=='W':
# return -1
# else:
# return 1
# def cep_analysis(df_in, center_lat, center_lon, outfile):
# df = df_in.copy()
# plt.rc('xtick',labelsize=20)
# plt.rc('ytick',labelsize=20)
# #calcualte distance with law of Haversines
# df['lat_rad'] = df['lat'].map(lambda x: math.radians(x))
# df['lng_rad'] = df['lng'].map(lambda x: math.radians(x))
# # print(df['lat_rad'])
# # print(df['lng_rad'])
# # df['phi_deg'] = df['lat_rad'].sub(math.radians(center_lat))
# # df['lambda_deg'] = df['lng_rad'].sub(math.radians(center_lon))
# # df['sin'] = numpy.sin(df['phi_deg']/2)
# # df['cos'] = ((numpy.sin(df['phi_deg']/2)*(numpy.sin(df['phi_deg']/2)))) + ((numpy.cos(math.radians(center_lat)) * numpy.cos(df['lat_rad'])) * (numpy.sin(df['lambda_deg']/2) * numpy.sin(df['lambda_deg']/2)))
# # df['a'] = ((numpy.sin(df['phi_deg']/2) * (numpy.sin(df['phi_deg']/2)))) + ((numpy.cos(center_lat) * numpy.cos(df['lat_rad'])) * (numpy.sin(df['lambda_deg']/2) * numpy.sin(df['lambda_deg']/2)))
# # df['c'] = 2 * numpy.arctan2(numpy.sqrt(df['a']),numpy.sqrt(1-df['a']))
# # df['d'] = rEarth * df['c']
# #calculate delta lat/lng
# df['deltalat'] = df['lat_rad'].sub(math.radians(center_lat))
# df['deltalng'] = df['lng_rad'].sub(math.radians(center_lon))
# #assume points are close together anc cald distances/angles
# #df['dy'] = numpy.sin(df['deltalng'])
# #df['dx'] = math.cos(math.radians(center_lat))*(df['lng']-center_lon)
# #df['dx'] = numpy.cos(math.radians(center_lat)) * numpy.sin(df['lat_rad']) - numpy.sin(math.radians(center_lat)) * numpy.cos(df['lat_rad']) * numpy.cos(df['deltalng'])
# df['dy'] = df['deltalat']
# df['dx'] = numpy.cos(math.radians(center_lat))*(df['deltalng'])
# df['r'] = numpy.sqrt(numpy.power(df['dy'],2) + numpy.power(df['dx'],2))
# df['rd'] = df['r'] * rEarth
# df['angle'] = numpy.arctan2(df['dy'],df['dx'])
# df['angle'] = df['angle'] + math.pi
# df['angle_deg'] = df['angle'].map(lambda x : math.degrees(x))
# df = df.sort_values(by='angle')
# df['dy_m'] = numpy.sin(df['angle']) * df['rd']
# df['dx_m'] = numpy.cos(df['angle']) * df['rd']
# #df.to_csv("test_out.csv")
# #Calculate CEP Here
# dx_extreme = df['dx_m'].abs().max()
# dy_extreme = df['dy_m'].abs().max()
# if dx_extreme > dy_extreme:
# ax_extreme = dx_extreme
# else:
# ax_extreme = dy_extreme
# sigma_dx = df['dx_m'].std()
# sigma_dy = df['dy_m'].std()
# radius_CEP = (0.56*sigma_dx) + (0.62*sigma_dy)
# radius_2DRMS = 2*numpy.sqrt(sigma_dx**2 + sigma_dy**2)
# print("CEP = " + str(radius_CEP))
# print("2DRMS = " + str(radius_2DRMS))
# #Draw Circles for Plotting
# cir_CEP = plt.Circle((0,0),radius_CEP,color='forestgreen',alpha=0.2)
# cir_2DRMS = plt.Circle((0,0),radius_2DRMS,color='tomato',alpha=0.2)
# #patches.append(CEP_circle)
# #patches.append(2DRMS_circle)
# #custom legend definition
# labels = [plt.Circle((0,0),0.005,color='forestgreen',alpha=0.2),plt.Circle((0,0),0.005,color='tomato',alpha=0.2),plt.Line2D((0,1),(0,0),color='dodgerblue',marker='o',linestyle='')]
# descriptions = ['CEP = ' + str(radius_CEP),'2DRMS = ' + str(radius_2DRMS),'Fix Distance From True Position']
# #p = PatchCollection(patches,alpha=0.4)
# fig, ax1 = plt.subplots(figsize=(20,20))
# sc=ax1.scatter(df['dx_m'],df['dy_m'],s=50,color='dodgerblue')
# ax1.grid(True)
# #ax1.legend(loc='upper left',prop={'size':'24'})
# ax1.set_facecolor('whitesmoke')
# ax1.add_artist(cir_2DRMS)
# ax1.add_artist(cir_CEP)
# ax1.legend(labels,descriptions,numpoints=1,markerscale=2,prop={'size': 24})
# ax1.set_xlim([-ax_extreme,ax_extreme])
# ax1.set_ylim([-ax_extreme,ax_extreme])
# ax1.set_xlabel('Distance (m)')
# ax1.set_ylabel('Distance (m)')
# ax1.xaxis.label.set_size(24)
# ax1.yaxis.label.set_size(24)
# axes = plt.gca()
# plt.title('CEP {0}'.format(outfile),fontsize=24,weight='bold')
# plt.savefig(outfile+".png",dpi=300)
# # --------------------------------------------------------------------
# styles = """
# <Style id="s_marca_{0}_hl">
# <IconStyle>
# <color>{2}</color>
# <scale>0.945455</scale>
# <Icon>
# <href>http://maps.google.com/mapfiles/kml/paddle/{1}.png</href>
# </Icon>
# <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
# </IconStyle>
# <ListStyle>
# <ItemIcon>
# <href>http://maps.google.com/mapfiles/kml/paddle/{1}-lv.png</href>
# </ItemIcon>
# </ListStyle>
# </Style>
# <Style id="s_marca_{0}">
# <IconStyle>
# <color>{2}</color>
# <scale>0.8</scale>
# <Icon>
# <href>http://maps.google.com/mapfiles/kml/paddle/{1}.png</href>
# </Icon>
# <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
# </IconStyle>
# <ListStyle>
# <ItemIcon>
# <href>http://maps.google.com/mapfiles/kml/paddle/{1}-lv.png</href>
# </ItemIcon>
# </ListStyle>
# </Style>
# <StyleMap id="m_marca_{0}">
# <Pair>
# <key>normal</key>
# <styleUrl>#s_marca_{0}</styleUrl>
# </Pair>
# <Pair>
# <key>highlight</key>
# <styleUrl>#s_marca_{0}_hl</styleUrl>
# </Pair>
# </StyleMap>
# """
centros_db = pandas.read_csv('centros.csv')
centros_db.set_index('grupo')
relaciones_db = pandas.read_csv('relacion.csv')
db = pandas.read_csv('tramas_export_1645791400550.csv')
db['lng'] = db['longitude'].where(db['ns'] == 'S', -1*db['longitude'])
db['lat'] = db['latitude'].where(db['ew'] == 'W', -1*db['latitude'])
# id;etiqueta;modulo;grupo;color
fulldb = db.set_index('deviceId').join(relaciones_db.set_index('id'))
has_location = fulldb['eventLength']>77
loc_db = fulldb[has_location]
loc_db.to_csv('out.csv',index=False)
""" for grupo in range(1,4):
for modulo in ['LC76F','L76LB']:
has_grupo = loc_db['grupo']==grupo
grupo_db = loc_db[has_grupo]
has_modulo = grupo_db['modulo']==modulo
grupo_modulo_db = grupo_db[has_modulo]
outfile = 'Grupo{0}_{1}'.format(grupo,modulo)
print(outfile)
cep_analysis(grupo_modulo_db, centros_db.at[grupo-1,'center_lat'], centros_db.at[grupo-1,'center_lon'], outfile)
outfile = 'Grupo{0}_{1}.kml'.format(grupo,modulo)
with open(outfile, 'w') as f:
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
f.write("<Document>\n")
f.write(" <name>"+outfile+"</name>\n")
for index, row in relaciones_db.iterrows():
f.write(styles.format(row['modulo']+row['etiqueta'],row['etiqueta'],row['color']))
for index, row in grupo_modulo_db.iterrows():
if int(row['eventLength'])>77 and (row['modulo']==modulo):
f.write(" <Placemark>\n")
# f.write(" <name>" + str(row['satellites']) + "</name>\n")
# f.write(" <description>" + str(row[0]) + "</description>\n")
f.write(" <Point>\n")
f.write(" <coordinates>" + str(row['lng']) + "," + str(row['lat']) + "," + str(row['altitude']) + "</coordinates>\n")
f.write(" </Point>\n")
f.write(" <styleUrl>#m_marca_{0}</styleUrl>\n".format(row['modulo']+row['etiqueta']))
f.write(" </Placemark>\n")
f.write("</Document>\n")
f.write("</kml>\n")
"""