-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (79 loc) · 3.63 KB
/
app.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
import time
from dateutil import parser
from flask import Flask, request, jsonify
from flask_cors import CORS
import service
from data_parser import EventType
if __name__ == 'app':
time1 = time.time()
service.initialize('./data/MC2/mc2.json', geo_file_path='./data/MC2/Oceanus Information/Oceanus Geography.geojson',
num_workers=10)
time2 = time.time()
print('load node:', len(service.node_list))
print('load edge:', len(service.edge_list))
print('cost time:', time2 - time1, 's')
app = Flask(__name__)
CORS(app)
@app.route('/mc2/select_transponder_ping', methods=['POST'])
def select_transponder_ping(): # put application's code here
data = request.get_json()
# data = {'startTime': '2035-09-15', 'endTime': '2035-9-29', 'queryType': '1', 'selectedCompany': 'WestRiver Shipping KgaA', 'selectedBoat': 'perchplundererbc0'}
start_time = parser.parse(data['startTime'])
end_time = parser.parse(data['endTime'])
vessel_id = data['selectedBoat']
company = data['selectedCompany']
results = service.select_edge_attribute(EventType.TransportEvent_TransponderPing, attribute='time',
func=lambda x: start_time <= x <= end_time
)
vessel_id_list = [vessel_id] if vessel_id is not '' else [vessel.id for vessel in
service.select_fishing_vessel_by_company(company)]
result = []
for vessel_id in vessel_id_list:
path = []
for ping in results:
if ping.target == vessel_id:
location_id = ping.source
geo_feature = service.select_geo_by_id(location_id)
time = ping.time
x, y = geo_feature.center()
path.append({'time': time, 'point': [x, y]})
result.append({
'vessel': vessel_id,
'path': path
})
for r in result:
r['path'] = sorted(r['path'], key=lambda t: t['time'])
return jsonify(result)
@app.route('/mc2/select_dwell', methods=['POST'])
def select_dwell(): # put application's code here
data = request.get_json()
# data = {'startTime': '2035-09-15', 'endTime': '2035-9-29', 'queryType': '1', 'selectedCompany': 'WestRiver Shipping KgaA', 'selectedBoat': 'perchplundererbc0'}
start_time = parser.parse(data['startTime'])
end_time = parser.parse(data['endTime'])
vessel_id = data['selectedBoat']
company = data['selectedCompany']
results = service.select_edge_attribute(EventType.TransportEvent_TransponderPing, attribute='time',
func=lambda x: start_time <= x <= end_time
)
vessel_id_list = [vessel_id] if vessel_id is not '' else [vessel.id for vessel in
service.select_fishing_vessel_by_company(company)]
result = []
for vessel_id in vessel_id_list:
dwell_time = {}
for ping in results:
if ping.target == vessel_id:
location_id = ping.source
dwell = ping.dwell
geo_feature = service.select_geo_by_id(location_id)
x, y = geo_feature.center()
if location_id in dwell_time.keys():
dwell_time[location_id]['time'] += dwell
else:
dwell_time[location_id] = {'time': 0, 'x': x, 'y': y}
result.append({
'vessel': vessel_id,
'dwell': dwell_time
})
return jsonify(result)
if __name__ == '__main__':
app.run()