Добавлена возможность выгрузки треков с OpenStreetMap
authorRoman Bazalevskiy <rvb@rvb.name>
Tue, 27 Mar 2018 07:20:13 +0000 (10:20 +0300)
committerRoman Bazalevskiy <rvb@rvb.name>
Tue, 27 Mar 2018 07:20:13 +0000 (10:20 +0300)
pyrungps.py

index bff314aee32a87d1587c53f9107926d892303494..db2497238f3abdf030d54cce4e225cc7e9a21753 100644 (file)
@@ -6,7 +6,8 @@ import urllib2
 import os
 from lxml import html,etree
 from optparse import OptionParser
-from datetime import date
+from datetime import date,datetime
+from mx.DateTime.ISO import ParseDateTimeUTC
 from parsegpx import write_parsed_to_db
 import pygpx
 
@@ -39,6 +40,95 @@ def get_gpx_track(trid,name):
 
   return xml
 
+def get_osm_list(username,password,year,month):
+
+  url = "https://www.openstreetmap.org/api/0.6/user/gpx_files"
+  passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
+  passman.add_password(None, url, username, password)
+  authhandler = urllib2.HTTPBasicAuthHandler(passman)
+  opener = urllib2.build_opener(authhandler)
+  urllib2.install_opener(opener)
+
+  req = urllib2.urlopen(url)
+  xml = etree.parse(req)
+
+  res=[]
+
+  for gpx_file in xml.getroot():
+    attrib = dict(gpx_file.attrib)
+    timestamp=datetime.fromtimestamp(ParseDateTimeUTC(attrib['timestamp']))
+    id = attrib['id']
+    description=None
+    for descr in gpx_file.iter('description'):
+      description=descr.text
+    sport=None
+    for tag in gpx_file.iter('tag'):
+      sport=tag.text
+    if timestamp.year==year and timestamp.month==month and description=='Run.GPS Track':
+      print id,description,sport,timestamp
+      record={ 'id': id, 'sport': sport }
+      res.append(record)
+      
+    return res      
+
+def get_osm_gpx(username,password,track_id):
+
+  url = "https://www.openstreetmap.org/api/0.6/gpx/"+track_id+"/data"
+  passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
+  passman.add_password(None, url, username, password)
+  authhandler = urllib2.HTTPBasicAuthHandler(passman)
+  opener = urllib2.build_opener(authhandler)
+  urllib2.install_opener(opener)
+
+  req = urllib2.urlopen(url)
+  xml = etree.parse(req)
+
+  return xml
+
+def sync_osm(username,password,year,month,dir=".",verbose=False,force=False):
+
+    training_list = get_osm_list(username,password,year,month)
+
+    for training in training_list:
+      filename = "%s/%04d/%02d/%s_%s.gpx" % (dir,year,month,training['sport'],training['id'])   
+      dirname = "%s/%04d/%02d" % (dir,year,month)
+
+      if os.path.exists(filename) and not force:
+
+        if verbose:
+          print "training %s exists, skipping" % (filename)
+
+      else:  
+
+        try:
+          os.makedirs(dirname)    
+        except:
+          None
+
+        xml = get_osm_gpx(username,password,training['id'])
+
+        if verbose:
+          print "writing training %s" % (filename)
+
+        gpx = pygpx.GPX()
+        gpx.ReadTree(xml)
+
+        gpx.author = username
+        gpx.FixNames(training['sport'])
+        gpx.ProcessTrackSegs()
+        for track in gpx.tracks:
+          track.sport=training['sport']
+        
+        xml = gpx.XMLTree();
+        f = open(filename,"w")
+        f.write(etree.tostring(xml,encoding='UTF-8',pretty_print=True))
+        f.close
+        write_parsed_to_db(db,gpx,filename)
+        try:
+          render_tiles.queue_render(db,filename)
+        except:
+          None  
+
 def sync_folder(username,year,month,dir=".",verbose=False,force=False):
 
     training_list = get_page(username,year,month)
@@ -96,13 +186,24 @@ def main():
       help="year and month in YYYY-MM format", metavar="YYYY-MM")                                                          
     parser.add_option("-u", "--username", dest="username",
       help="Run.GPS username")                                                          
+    parser.add_option("-o", "--osm-username", dest="osmname",
+      help="OpenStreetMap username")                                                          
+    parser.add_option("-p", "--osm-passwd", dest="osmpasswd",
+      help="OpenStreetMap password")                                                          
     (options, args) = parser.parse_args()
 
     username = options.username
-    if not username:
-      print "Run.GPS username is mandatory!"
+    osmname = options.osmname
+    osmpwd = options.osmpasswd
+    if not (username or (osmname and osmpwd)):
+      print "Run.GPS username or OSM username/password is mandatory!"
       return
 
+    if username:
+      pusername = username+'@Run.GPS'
+    elif osmname:
+      pusername = osmname+'@OSM'  
+
     try:
       if options.yearmonth:
         year,month = options.yearmonth.split('-')
@@ -126,20 +227,29 @@ def main():
     
     if year:
       if options.verbose:
-        print "retrieving trainings for user %s, year %s, month %s to %s" % (username,year,month+1,outdir)
-      sync_folder(username,year,month,outdir,options.verbose,options.force)
+        print "retrieving trainings for user %s, year %s, month %s to %s" % (pusername,year,month+1,outdir)
+      if username:
+        sync_folder(username,year,month,outdir,options.verbose,options.force)
+      elif osmname:
+        sync_osm(osmname,osmpwd,year,month+1,outdir,options.verbose,options.force)  
     else:
       if options.verbose:
-        print "retrieving two last months for user %s to %s" % (username,outdir)
+        print "retrieving two last months for user %s to %s" % (pusername,outdir)
       now = date.today()
       current_year = now.year
       current_month = now.month
-      sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
+      if username:
+        sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
+      elif osmname:
+        sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)  
       current_month = current_month -1
       if current_month == 0:
         current_month = 12
         current_year = current_year -1
-      sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
+      if username:
+        sync_folder(username,current_year,current_month-1,outdir,options.verbose,options.force)
+      elif osmname:  
+        sync_osm(osmname,osmpwd,current_year,current_month,outdir,options.verbose,options.force)  
 
 if __name__ == "__main__":