From: Roman Bazalevsky Date: Sun, 13 Sep 2020 08:17:34 +0000 (+0300) Subject: Многочисленные мелкие доработки, связанные с изменением отрисовки треков и загрузкой... X-Git-Url: https://git.rvb.name/pyrungps.git/commitdiff_plain/1690b8b7c8ccfdf60be2b33b314e6c26bdef86e6?ds=sidebyside Многочисленные мелкие доработки, связанные с изменением отрисовки треков и загрузкой треков из Google Fit. --- diff --git a/generate_image.py b/generate_image.py index e6a5021..12169ca 100644 --- a/generate_image.py +++ b/generate_image.py @@ -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) diff --git a/parsegpx.py b/parsegpx.py index 25161c8..c8ca599 100644 --- a/parsegpx.py +++ b/parsegpx.py @@ -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) diff --git a/pygpx.py b/pygpx.py index 4692538..49ccdff 100644 --- 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", diff --git a/pyrungps.py b/pyrungps.py index bfff892..ed3fce5 100644 --- a/pyrungps.py +++ b/pyrungps.py @@ -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__": diff --git a/render_tiles.py b/render_tiles.py index 2d81f77..443ae80 100755 --- a/render_tiles.py +++ b/render_tiles.py @@ -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'),))