В Ubuntu 22.04 сломали проекции в Mapnik, приходится использовать альтернативный...
[pyrungps.git] / pyrungps / 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 urllib.parse import unquote
8 import pyrungps.pygpx
9 import pyrungps.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   cur.execute ("delete from tracks where filename=?" , (filename,))
29   
30   tracks = gpx.tracks
31   
32   for track in tracks:
33
34       try:
35         author = gpx.author
36       except:
37         author = None 
38         
39       try:
40         name = gpx.name
41       except:
42         name = None
43         
44       if author:
45         try:
46           cur.execute("insert into authors(name,description) values(?,?)", (author,''))         
47           print("created author %s" % (author))
48         except:
49           print("failed to create author %s" % (author))
50           pass
51
52       try:
53
54         print("processing...")
55         start = track.start()
56         if start:
57           printable = pygeocode.GeoName(start.lat,start.lon).printable
58           start_time = track.start_time()
59           full_duration = track.full_duration().total_seconds()
60           distance = track.distance()
61           filtered_distance = track.filtered_distance(max_speed=50)
62           ascent = track.elevation_gain()
63           descent = track.elevation_loss()
64           ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
65           params = ( 
66             gpx.author,name,filename,
67             track.sport,start_time,full_duration,
68             distance,filtered_distance,ascent,descent,
69             start.lat,start.lon,
70             printable,
71             minlat,minlon,maxlat,maxlon
72              )
73           print(params)
74           cur.execute("""
75             insert into tracks(
76               author,name,filename,sport,
77               start_time,duration,
78               distance,distance_filtered,
79               ascent,descent,
80               lat,lon,printable_location,minlat,minlon,maxlat,maxlon) 
81             values(
82               ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
83               """ 
84               , params )
85           conn.commit()
86           print("created track %s" % (filename))
87
88       except:
89       
90         raise
91
92 def write_tree_to_db(db,tree,filename):
93
94   gpx = pygpx.GPX()
95   gpx.ReadTree(tree)
96
97   write_parsed_to_db(db,gpx,filename)
98
99 def print_parsed_file(filename):
100
101   gpx = pygpx.GPX()
102   gpx.ReadFile(filename)
103
104   for track in gpx.tracks:
105
106     try:
107       author = gpx.author
108     except:
109       author = None 
110        
111     try:
112       name = gpx.name
113     except:
114       name = None
115         
116     start = track.start()
117     if start:
118       printable = pygeocode.GeoName(start.lat,start.lon).printable
119       start_time = track.start_time()
120       full_duration = track.full_duration().total_seconds()
121       distance = track.distance()
122       filtered_distance = track.filtered_distance(max_speed=50)
123       ascent = track.elevation_gain()
124       descent = track.elevation_loss()
125       ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
126       params = ( 
127         gpx.author,name,filename,
128         track.sport,start_time,full_duration,
129         distance,filtered_distance,ascent,descent,
130         start.lat,start.lon,
131         printable,
132         minlat,minlon,maxlat,maxlon
133       )
134       print(params)