gpx_reduce.py
changeset 4 1b96bb9de1f3
parent 3 f94099a0277a
child 5 6be382f3be34
--- a/gpx_reduce.py	Tue Aug 22 11:22:13 2017 +0200
+++ b/gpx_reduce.py	Wed Aug 23 21:25:58 2017 +0200
@@ -67,14 +67,14 @@
 sqrlength (number of points plus sum of squared new line segment lengths normalized by maxsep),
 mix (number of points plus sum of squared distances to each maximally separated leftout point per new line segment weighted with corresponding segment length),
 exp (number of points plus sum of squared distances to leftout points with exponential weighting of 1/2, 1/4, 1/8... from furthest to closest point). exp=standard''')
-(options, args) = parser.parse_args()
+parser.add_option ('-r', dest='remove', default='extensions,hdop', help='remove tags T1,T2,..,Tn from every trackpoint', metavar='T1 T2 Tn')
 
+options, args = parser.parse_args()
 
 if len(args) < 1:
     parser.print_usage()
     exit(2)
 
-
 # use the WGS-84 ellipsoid
 rE = 6356752.314245 # earth's radius
 a = 6378137.0
@@ -333,7 +333,7 @@
         except Exception as e:
             print e
             times = None
-        
+
         # save original trackseg for plotting
         if options.plot:
             tracksegs_old.append([[lats[i], lons[i], eles[i]] for i in range(n)])
@@ -356,14 +356,23 @@
         # delete certain points from original data
         delete_pnums = [i for i in range(n) if i not in final_pnums]
         for i in reversed(delete_pnums):
-            trkseg.remove (trkpts[i])
-        
+            trkseg.remove (trkpts[i])   # remove from the xml-tree
+            del trkpts [i]              # also remove from the list
+
+        # remove certain sub-elements from remaining points
+        options.remove = options.remove.replace ('+','extensions,hdop,')
+        taglist = options.remove.split (',')
+        if taglist: print 'remove %s subelements from points' % ', '.join (taglist)
+        for trkpnt in trkpts:
+            for tag in taglist:
+                e = trkpnt.find (nsmap + tag)
+                if e != None: trkpnt.remove (e)
+
         # save reduced trackseg for plotting
         if options.plot:
-            tracksegs_new.append([[float(trkpt.get('lat')),
+            tracksegs_new.append ([[float(trkpt.get('lat')),
                 float(trkpt.get('lon')), float(trkpt.find(nsmap + 'ele').text)]
                 for trkpt in trkseg.findall(nsmap + 'trkpt')])
-        
     
     # export data to file
     if options.ofname != None:
@@ -379,23 +388,21 @@
 
     # plot result to screen
     if options.plot:
-        from subprocess import Popen, PIPE
-
         latm, lonm, elesum = xyz_to_latlonele(sumx, sumy, sumz)
 
+        data_old = []
         for trkseg in tracksegs_old:
-            data_old = []
             for trkpt in trkseg:
                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
                 data_old.append (xy)
         
+        data = []
         for trkseg in tracksegs_new:
-            data = []
             for trkpt in trkseg:
                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
                 data.append (xy)
 
-        #~ gnuPlotCmd = ['gnuplot', '-persist']
+        from subprocess import Popen, PIPE
         gnuPlotCmd = ['gnuplot']
         plot = Popen (gnuPlotCmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
         plot.stdin.write (b"plot '-' with linespoints lc rgb 'red' pt 4, '-' with linespoints pt 6\n")
@@ -404,5 +411,4 @@
         plot.stdin.write ("\n".join ('%f %f' % d for d in data_old).encode ())
         plot.stdin.write (b'\ne\n')
         plot.stdin.flush ()
-        #~ raw_input ('druk')
-        while 1: time.sleep (1)
+        raw_input ('druk')