-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeDB.py
80 lines (69 loc) · 2.55 KB
/
changeDB.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
#Adds users/ gpxfiles
import sqlite3
from sqlite3 import Error
from createDB import create_connection
import os
import gpxpy
import models
#Heavily modified from http://www.sqlitetutorial.net/sqlite-python/
def create_user(conn, user):
sql = ''' INSERT INTO users(name)
VALUES(?) '''
cur = conn.cursor()
cur.execute(sql, user)
return cur.lastrowid
def create_gpxFile(conn, gpxFile):
sql = ''' INSERT INTO gpxFiles(name,file,miles_ridden,min_lat,min_lon,max_lat,max_lon,user_id)
VALUES(?,?,?,?,?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, gpxFile)
return cur.lastrowid
def test():
database = "db/user.db"
# create a database connection
conn = create_connection(database)
with conn:
# create a new project
user = ('Bepis Man',)
user_id = create_user(conn, user)
# tasks
gpx = open('gpx/nstar.gpx', 'r')
add_gpxFile(conn, 'nstar', gpx, user_id )
def add_user(conn, name):
with conn:
user = (name,)
user_id = create_user(conn, user)
return user_id
def add_gpxFile(conn, name, gpxFile, user):
with conn:
gpxFile = gpxFile.read()
(milesRidden, min_lat, min_lon, max_lat, max_lon) = findGpxData(gpxFile)
gpx = (name, gpxFile, milesRidden, min_lat, min_lon, max_lat, max_lon, user)
gpx_id = create_gpxFile(conn, gpx)
return gpx_id
def findGpxData(gpxFile):
gpx = gpxpy.parse(gpxFile)
#gpxpy starter code https://github.com/tkrajina/gpxpy
dx = (0,None)
max_lat = None
max_lon = None
min_lat = None
min_lon = None
i = 0
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
if dx[1] == None:
dx = (0,(point.latitude, point.longitude))
else:
distance = models.findDistance(dx[1][0], dx[1][1], point.latitude, point.longitude)
dx = (dx[0] + distance, (point.latitude, point.longitude))
if min_lat == None or point.latitude < min_lat:
min_lat = point.latitude
if min_lon == None or point.longitude < min_lon:
min_lon = point.longitude
if max_lat == None or point.latitude > max_lat:
max_lat = point.latitude
if max_lon == None or point.longitude > max_lon:
max_lon = point.longitude
return (dx[0], min_lat, min_lon, max_lat, max_lon)