-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_raw.py
70 lines (55 loc) · 2.06 KB
/
import_raw.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
#!/usr/bin/python3
import csv
import sqlite3
import sys
if len(sys.argv) < 2:
exit("Filename of datafile is missing!\nUsage: 'python {} <datafile>'".format(sys.argv[0]))
DATA_VIBRATION = sys.argv[1]
# Seq = Sequence number inserted by the Arduino
# vibration_type = 1 (soft vibration), 2 (hard vibration), 3 (timer update)
FIELDS_VIBRATION = ['seq', 'imu_x', 'imu_y', 'imu_z',
'lat', 'lon', 'day', 'month', 'year', 'hours', 'minutes', 'seconds',
'runtime', 'vibration_type']
dbconnector = sqlite3.connect('data.db')
c = dbconnector.cursor()
c.execute('DROP TABLE IF EXISTS `vibration`')
c.execute('CREATE TABLE `vibration` ('
'seq INTEGER UNIQUE PRIMARY KEY, '
'imu_x FLOAT, '
'imu_y FLOAT, '
'imu_z FLOAT, '
'lat FLOAT, '
'lon FLOAT, '
'day INTEGER, '
'month INTEGER, '
'year INTEGER, '
'hours INTEGER, '
'minutes INTEGER, '
'seconds INTEGER, '
'runtime INTEGER, '
'vibration_type INTEGER)')
counter_total = 0
counter_imported = 0
with open(DATA_VIBRATION, 'r') as fh:
reader = csv.DictReader(fh, fieldnames=FIELDS_VIBRATION, delimiter=',')
for line in reader:
counter_total += 1
# filter criterias
lat = float(line['lat'])
lon = float(line['lon'])
if lat == 0 or lon == 0:
# skip lines with no GPS coordinates
continue
if lat < 5000 or lat > 5200 or lon < 1340 or lon > 1400:
# skip lines with broken GPS coordinates
continue
# convert NMEA 4-digit to 2-digit GPS
for x in ['lat', 'lon']:
line[x] = float(line[x][:2]) + float(line[x][2:]) / 60
# convert line to a tuple and insert the tuple as row into the database
data = tuple(line[f] for f in FIELDS_VIBRATION)
c.execute('INSERT INTO vibration VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)', data)
counter_imported += 1
dbconnector.commit()
dbconnector.close()
print("{} of {} entries imported into database.".format(counter_imported, counter_total))