Многочисленные мелкие доработки, связанные с изменением отрисовки треков и загрузкой...
authorRoman Bazalevsky <rvb@rvb.name>
Sun, 13 Sep 2020 08:17:34 +0000 (11:17 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Sun, 13 Sep 2020 08:17:34 +0000 (11:17 +0300)
generate_image.py
parsegpx.py
pygpx.py
pyrungps.py
render_tiles.py

index e6a50210e36cf5e1d96c2540abcabbf79f2ec267..12169cad91e6a80140d5ac50dd56b56f5338fec3 100644 (file)
@@ -1,9 +1,6 @@
 #!/usr/bin/env python
 
-try:
-    import mapnik2 as mapnik
-except:
-    import mapnik
+import mapnik
 
 import sys, os
 
@@ -68,19 +65,18 @@ def render_map(mapfile,map_uri,gpx_file,imgx,imgy):
     rule = mapnik.Rule()
 
     line_symbolizer = mapnik.LineSymbolizer()
-    line_symbolizer.stroke = mapnik.Color('rgb(125, 125, 0)')
-    rule.symbols.append(line_symbolizer)
 
-    point_symbolizer = mapnik.MarkersSymbolizer()
-    point_symbolizer.allow_overlap = True
-    point_symbolizer.opacity = 0.8 # semi-transparent
-    rule.symbols.append(point_symbolizer)
+    line_symbolizer.stroke = mapnik.Color('rgb(0,0,127)')
+    line_symbolizer.stroke_width = 4.0
+    line_symbolizer.stroke_opacity = 0.5
+    
+    rule.symbols.append(line_symbolizer)
 
     style.rules.append(rule)
     m.append_style('GPS_tracking_points', style)
 
     layer = mapnik.Layer('GPS_tracking_points')
-    layer.datasource = mapnik.Ogr(file=gpx_file, layer='track_points')
+    layer.datasource = mapnik.Ogr(file=gpx_file, layer='tracks')
     layer.styles.append('GPS_tracking_points')
     m.layers.append(layer)
 
@@ -96,6 +92,7 @@ def render_all(db,mapfile,imgx,imgy):
     from os.path import dirname    
 
     conn = sqlite3.connect(db)
+    conn.text_factory = str
     cur = conn.cursor()
     updcur = conn.cursor()
 
@@ -139,7 +136,7 @@ def main():
     if options.mapfile:  
       mapfile = options.mapfile
     else:
-      mapfile = "/etc/mapnik-osm-carto-data/veloroad-transparent.xml"
+      mapfile = "/etc/mapnik-osm-carto-data/veloroad-imposm.xml"
 
     if options.outfile:  
       map_uri = options.outfile
@@ -149,11 +146,11 @@ def main():
     if options.x:  
       imgx = int(options.x)
     else:
-      imgx= 400
+      imgx= 640
     if options.y:  
       imgy = int(options.y)
     else:
-      imgy= 400
+      imgy= 640
 
     if options.db:
       render_all(options.db,mapfile,imgx,imgy) 
index 25161c8d761517d5d46122d777c71fe8efb022e3..c8ca59988e92e20fae868175871945feba3a933b 100644 (file)
@@ -13,6 +13,7 @@ import datetime
 def check_db_for_training(db,sport,timestamp):
 
   conn = sqlite3.connect(db)
+  conn.text_factory = str
   cur = conn.cursor()
 
   cur.execute ("select count(*) from tracks where sport=? and start_time=?" , (sport,timestamp))
@@ -21,9 +22,13 @@ def check_db_for_training(db,sport,timestamp):
 def write_parsed_to_db(db,gpx,filename):
 
   conn = sqlite3.connect(db)
+  conn.text_factory = str
   cur = conn.cursor()
 
-  cur.execute ("delete from tracks where filename=?" , (filename.decode('UTF-8'),))
+  if type(filename) is str:
+    filename=filename.decode('UTF-8')
+
+  cur.execute ("delete from tracks where filename=?" , (filename,))
   
   tracks = gpx.tracks
   
@@ -36,6 +41,8 @@ def write_parsed_to_db(db,gpx,filename):
         
       try:
         name = gpx.name
+        if type(name) is str:
+          name=name.decode('UTF-8')
       except:
         name = None
         
@@ -61,13 +68,14 @@ def write_parsed_to_db(db,gpx,filename):
           descent = track.elevation_loss()
           ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
           params = ( 
-            gpx.author,name,filename.decode('UTF-8'),
+            gpx.author,name,filename,
             track.sport,start_time,full_duration,
             distance,filtered_distance,ascent,descent,
             start.lat,start.lon,
             printable,
             minlat,minlon,maxlat,maxlon
              )
+          print(params)
           cur.execute("""
             insert into tracks(
               author,name,filename,sport,
@@ -93,4 +101,39 @@ def write_tree_to_db(db,tree,filename):
 
   write_parsed_to_db(db,gpx,filename)
 
-  
\ No newline at end of file
+def print_parsed_file(filename):
+
+  gpx = pygpx.GPX()
+  gpx.ReadFile(filename)
+
+  for track in gpx.tracks:
+
+    try:
+      author = gpx.author
+    except:
+      author = None 
+       
+    try:
+      name = gpx.name
+    except:
+      name = None
+        
+    start = track.start()
+    if start:
+      printable = pygeocode.GeoName(start.lat,start.lon).printable
+      start_time = track.start_time()
+      full_duration = track.full_duration().total_seconds()
+      distance = track.distance()
+      filtered_distance = track.filtered_distance(max_speed=50)
+      ascent = track.elevation_gain()
+      descent = track.elevation_loss()
+      ((minlat,minlon),(maxlat,maxlon)) = track.bound_box()
+      params = ( 
+        gpx.author,name,filename.decode('UTF-8'),
+        track.sport,start_time,full_duration,
+        distance,filtered_distance,ascent,descent,
+        start.lat,start.lon,
+        printable,
+        minlat,minlon,maxlat,maxlon
+      )
+      print(params)
index 469253854e0b70c3dd169f68819c377ecccb2afe..49ccdff205f00eca95bd03f6e790c6c3ef205a29 100644 (file)
--- a/pygpx.py
+++ b/pygpx.py
@@ -7,6 +7,7 @@ import os
 import math
 import datetime
 from dateutil.parser import parse
+from pprint import pprint
 
 def deg2rad(deg):
 # Преобразование из градусов в радианы.
@@ -190,20 +191,40 @@ class GPXTrackSeg:
         i.write(trksegnode,"trkpt")      
 
     def _get_elevation(self):
+
+        elev_data = []
+        for pt in self.trkpts:
+          if pt.elevation:
+            elev_data.append(pt.elevation)
+
         gain = 0.0
         loss = 0.0
-        last_pt = None
-        for pt in self.trkpts:
-          if last_pt is not None:
-            last_elevation = last_pt.elevation
+        last_elevation = None
+
+        window_size = 5
+        i = 0
+        moving_averages = []
+
+        while i < len(elev_data) - window_size + 1:
+          this_window = elev_data[i : i + window_size]
+          window_average = sum(this_window) / window_size
+          moving_averages.append(window_average)
+          i += 1
+          
+        if len(moving_averages)>2:
+          elev_data = moving_averages
+
+        for pt in elev_data:    
+          if last_elevation is not None:
             try:
-              if pt.elevation > last_elevation:
-                  gain += pt.elevation -last_elevation
+              if pt > last_elevation:
+                  gain += pt - last_elevation
               else:
-                  loss += last_elevation - pt.elevation
+                  loss += last_elevation - pt
             except:
               pass      
-          last_pt=pt    
+          last_elevation=pt    
+
         self.elevation_gain = gain
         self.elevation_loss = loss
 
@@ -554,7 +575,12 @@ class GPX:
       return ((minlat,minlon),(maxlat,maxlon))      
 
     def FixNames(self,linkname):
-      goodname = linkname.decode('UTF-8')
+      pprint(linkname)
+      pprint(type(linkname))
+      if type(linkname) is unicode:
+        goodname=linkname
+      else:  
+        goodname = linkname.decode('UTF-8')
       badname = goodname.encode('ascii','replace')
       if self.name and self.name.startswith(badname):
         self.name=self.name.replace(badname,goodname)
@@ -589,7 +615,10 @@ class GPX:
         if current_seg is not None:
           new_segs.append(current_seg)  
         # Обработали
+        for trs in new_segs:
+          trs._get_elevation()
         tr.trksegs = new_segs
+        
 
     def XMLTree(self):
       root = etree.Element("gpx", attrib ={"creator": "pygpx",
index bfff892fca2164b19b10c762e1b6175e18f642f4..ed3fce52b94fac40d21a84fcb49270dc5ffaaec1 100644 (file)
@@ -2,6 +2,9 @@
 # coding: UTF-8
 
 import requests
+
+from urllib3.exceptions import InsecureRequestWarning
+requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
     
 #import sys
 import os
@@ -25,7 +28,7 @@ def get_page(uname,year,month):
   
   trainings = []
 
-  req = requests.get("http://www.gps-sport.net/services/getMonthlyTrainingDataHTML_V2.jsp?userName=%s&year=%s&month=%s&rnd=0.645673"% (uname,year,month), headers = {'User-agent': 'Mozilla/5.0'})
+  req = requests.get("http://www.gps-sport.net/services/getMonthlyTrainingDataHTML_V2.jsp?userName=%s&year=%s&month=%s&rnd=0.645673"% (uname,year,month), headers = {'User-agent': 'Mozilla/5.0'}, verify=False)
   page = req.text.encode('utf-8')
   dom = html.document_fromstring(page)
 
@@ -42,7 +45,7 @@ def get_gpx_track(trid,name):
 
   print "trid=",trid
 
-  req = requests.get("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid))
+  req = requests.get("http://www.gps-sport.net/services/trainingGPX.jsp?trainingID=%s&tz=-180" % (trid), verify=False)
   xml = etree.fromstring(req.text.encode('utf-8'))
 
   return xml
@@ -116,6 +119,7 @@ def get_dbx_list(dbx,username,year,month):
       if entry.name == u'Приложения':
         for entry_app in dbx.files_list_folder(entry.id).entries:
           if entry_app.name == u'Run.GPS':
+            print "id="+entry_app.id
             gpx_list_id=entry_app.id
             break
         break
@@ -125,20 +129,29 @@ def get_dbx_list(dbx,username,year,month):
     if gpx_list_id:
     
       for file in dbx.files_list_folder(gpx_list_id).entries:
-        
+      
         filename,ext = os.path.splitext(file.name)
         if ext == '.tcx':
           try:
             fyear = int(filename[0:4])
             fmonth = int(filename[5:7])
             if fyear == year and fmonth == month:
-              sport = filename[18:]
-              timestamp = datetime.strptime(filename[0:16],'%Y-%m-%d_%H%M%S')
+              sport = filename[41:]
+              timestamp = datetime.strptime(filename[0:19],'%Y-%m-%dT%H_%M_%S')
               record={ 'id': file.id, 'timestamp': timestamp, 'sport': sport }
               res.append(record)
           except:
-            None
-    
+            try:
+              fyear = int(filename[0:4])
+              fmonth = int(filename[5:7])
+              if fyear == year and fmonth == month:
+                sport = filename[18:]
+                timestamp = datetime.strptime(filename[0:16],'%Y-%m-%d_%H%M%S')
+                record={ 'id': file.id, 'timestamp': timestamp, 'sport': sport }
+                res.append(record)
+            except:
+              None
+
     return res        
       
 
@@ -395,7 +408,7 @@ def main():
       elif dbauth:
         sync_db(dbx,pusername,current_year,current_month,outdir,options.verbose,options.force)  
 
-    generate_image.render_all(db,'/etc/mapnik-osm-carto-data/veloroad-transparent.xml',400,400)
+    generate_image.render_all(db,'/etc/mapnik-osm-carto-data/veloroad-imposm.xml',640,640)
 
 if __name__ == "__main__":
 
index 2d81f7736bcbd68affecb637b20b79d46ba0e611..443ae807337cb2c3b33909860e6882da9f2a6308 100755 (executable)
@@ -8,6 +8,7 @@ from pprint import pprint
 def queue_render(db,filename,forced_max_zoom=None):
 
   conn = sqlite3.connect(db)
+  conn.text_factory = str
   cur = conn.cursor()
         
   cur.execute("select minlat,minlon,maxlat,maxlon from tracks where filename=?" , (filename.decode('UTF-8'),))