-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_data.py
62 lines (48 loc) · 2.16 KB
/
gps_data.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
import matplotlib
import numpy as np
import pandas as pd
from iss4e.db import influxdb, mysql
from iss4e.util.config import load_config
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pytz import timezone
config = load_config()
participants = pd.read_excel("participant map.xlsx", header=[1])
female_staff = participants.iloc[1:8][["Email", "IMEI"]]
male_staff = participants.iloc[10:16][["Email", "IMEI"]]
female_students = participants.iloc[18:23][["Email", "IMEI"]]
male_students = participants.iloc[25:32][["Email", "IMEI"]]
eastern = timezone('Canada/Eastern')
def get_trips(l):
trips_by_imei = {}
for imei in l["IMEI"]:
cursor.execute("SELECT start,end from trips where imei={imei}".format(imei=imei))
result = cursor.fetchall()
trips_by_imei[str(int(imei))] = [(start, end) for (start, end) in
result]
return trips_by_imei
def write_gps_data(trips_collection, file_name):
gps = []
for imei, trips in trips_collection.items():
for start, end in trips:
query = "select latitude, longitude from {measurement} where imei='{imei}' and time >= '{start}' and time <= '{end}' and longitude != 0 and latitude != 0".format(
imei=imei, measurement=config["webike.measurement"], start=start, end=end)
result = influx_client.query(query)
gps.append(list(result[config["webike.measurement"]]))
with open(file_name, mode='w') as file:
file.write(str(gps))
with mysql.connect(**config["webike.mysql"]) as mysql_client, influxdb.connect(
**config["webike.influx"]) as influx_client:
cursor = mysql_client.cursor()
fstu_trips = get_trips(female_students)
fsta_trips = get_trips(female_staff)
mstu_trips = get_trips(male_students)
msta_trips = get_trips(male_staff)
ftrips = dict(fstu_trips, **fsta_trips)
mtrips = dict(mstu_trips, **msta_trips)
staff = dict(fsta_trips, **msta_trips)
students = dict(fstu_trips, **mstu_trips)
write_gps_data(ftrips, "data/fgps")
write_gps_data(mtrips, "data/mgps")
write_gps_data(staff, "data/staffgps")
write_gps_data(students, "data/studentsgps")