-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathais-mapper
67 lines (56 loc) · 1.76 KB
/
ais-mapper
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
#!/usr/bin/env python3
import os
import webbrowser
import sys
import json
import folium
import pandas as pd
import datetime as dt
import numpy as np
filename = sys.argv[1] # parsed nmea data in json formatted file.
def process_file(filename):
data = []
keys = ['mmsi', 'name', 'lat', 'lon', 'year', 'month', 'day', 'hour', \
'minute', 'second']
with open(filename) as lines:
for item in lines:
zerodeal = False
element = {}
q = json.loads(item)
for key in keys:
if key not in q:
if key == 'name':
element['name'] = 'Noname'
else:
zerodeal = True
break
if key in q:
element[key] = q[key]
if zerodeal == False:
data.append(element)
d = pd.DataFrame.from_dict(data)
return d
df = process_file(filename)
if df.empty:
print('Not enough data for mapping...')
exit()
# For testing
# print(df)
# set up map parameters
minlon = df['lon'].min()
maxlon = df['lon'].max()
minlat = df['lat'].min()
maxlat = df['lat'].max()
extent = [minlon, maxlon, minlat, maxlat]
centerlat = np.mean(extent[2:]) # center of map, lat
centerlon = np.mean(extent[:2]) # center of map, lon
# center the map
MAPCENTER = [centerlat, centerlon]
# build the map
map_ais = folium.Map(location=MAPCENTER, zoom_start=12, tiles='cartodbpositron', \
width=640, height=480)
for i in range(0, len(df)):
folium.Marker(location=[df.iloc[i]['lat'], df.iloc[i]['lon']], \
popup=[df.iloc[i]['mmsi'],df.iloc[i]['name']], fill_color='#43d9de', radius=8).add_to(map_ais)
map_ais.save('map.html')
webbrowser.open('map.html')