c8ca59988e92e20fae868175871945feba3a933b
[pyrungps.git] / parsegpx.py
1 #!/usr/bin/env python
2 # coding: UTF-8
3
4 import sys
5 import os
6 from lxml import etree
7 from urllib2 import unquote
8 import pygpx
9 import pygeocode
10 import sqlite3
11 import datetime
12
13 def check_db_for_training(db,sport,timestamp):
14
15   conn = sqlite3.connect(db)
16   conn.text_factory = str
17   cur = conn.cursor()
18
19   cur.execute ("select count(*) from tracks where sport=? and start_time=?" , (sport,timestamp))
20   return cur.fetchall()[0][0]
21
22 def write_parsed_to_db(db,gpx,filename):
23
24   conn = sqlite3.connect(db)
25   conn.text_factory = str
26   cur = conn.cursor()
27
28   if type(filename) is str:
29     filename=filename.decode('UTF-8')
30
31   cur.execute ("delete from tracks where filename=?" , (filename,))
32   
33   tracks = gpx.tracks
34   
35   for track in tracks:
36
37       try:
38         author = gpx.author
39       except:
40         author = None 
41         
42       try:
43         name = gpx.name
44         if type(name) is str:
45           name=name.decode('UTF-8')
46       except:
47         name = None
48         
49       if author:
50         try:
51           cur.execute("insert into authors(name,description) values(?,?)", (author,''))         
52           print "created author %s" % (author)
53         except:
54           print "failed to create author %s" % (author)
55           pass
56
57       try:
58
59         print "processing..."
60         start = track.start()
61         if start:
62           printable = pygeocode.GeoName(start.lat,start.lon).printable
63           start_time = track.start_time()
64           full_duration = track.full_duration().total_seconds()
65           distance = track.distance()
66           filtered_distance = track.filtered_distance(max_speed=50)
67           ascent = track.elevation_gain()
68           descent = track.elevation_loss()
69           ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
70           params = ( 
71             gpx.author,name,filename,
72             track.sport,start_time,full_duration,
73             distance,filtered_distance,ascent,descent,
74             start.lat,start.lon,
75             printable,
76             minlat,minlon,maxlat,maxlon
77              )
78           print(params)
79           cur.execute("""
80             insert into tracks(
81               author,name,filename,sport,
82               start_time,duration,
83               distance,distance_filtered,
84               ascent,descent,
85               lat,lon,printable_location,minlat,minlon,maxlat,maxlon) 
86             values(
87               ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
88               """ 
89               , params )
90           conn.commit()
91           print "created track %s" % (filename)
92
93       except:
94       
95         raise
96
97 def write_tree_to_db(db,tree,filename):
98
99   gpx = pygpx.GPX()
100   gpx.ReadTree(tree)
101
102   write_parsed_to_db(db,gpx,filename)
103
104 def print_parsed_file(filename):
105
106   gpx = pygpx.GPX()
107   gpx.ReadFile(filename)
108
109   for track in gpx.tracks:
110
111     try:
112       author = gpx.author
113     except:
114       author = None 
115        
116     try:
117       name = gpx.name
118     except:
119       name = None
120         
121     start = track.start()
122     if start:
123       printable = pygeocode.GeoName(start.lat,start.lon).printable
124       start_time = track.start_time()
125       full_duration = track.full_duration().total_seconds()
126       distance = track.distance()
127       filtered_distance = track.filtered_distance(max_speed=50)
128       ascent = track.elevation_gain()
129       descent = track.elevation_loss()
130       ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
131       params = ( 
132         gpx.author,name,filename.decode('UTF-8'),
133         track.sport,start_time,full_duration,
134         distance,filtered_distance,ascent,descent,
135         start.lat,start.lon,
136         printable,
137         minlat,minlon,maxlat,maxlon
138       )
139       print(params)