gpx_reduce.py
changeset 4 1b96bb9de1f3
parent 3 f94099a0277a
child 5 6be382f3be34
equal deleted inserted replaced
3:f94099a0277a 4:1b96bb9de1f3
    65 sqrdistsum (number of points plus sum of squared distances to leftout points),
    65 sqrdistsum (number of points plus sum of squared distances to leftout points),
    66 sqrdistmax (number of points plus sum of squared distances to each maximally separated leftout point per new line segment),
    66 sqrdistmax (number of points plus sum of squared distances to each maximally separated leftout point per new line segment),
    67 sqrlength (number of points plus sum of squared new line segment lengths normalized by maxsep),
    67 sqrlength (number of points plus sum of squared new line segment lengths normalized by maxsep),
    68 mix (number of points plus sum of squared distances to each maximally separated leftout point per new line segment weighted with corresponding segment length),
    68 mix (number of points plus sum of squared distances to each maximally separated leftout point per new line segment weighted with corresponding segment length),
    69 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''')
    69 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''')
    70 (options, args) = parser.parse_args()
    70 parser.add_option ('-r', dest='remove', default='extensions,hdop', help='remove tags T1,T2,..,Tn from every trackpoint', metavar='T1 T2 Tn')
    71 
    71 
       
    72 options, args = parser.parse_args()
    72 
    73 
    73 if len(args) < 1:
    74 if len(args) < 1:
    74     parser.print_usage()
    75     parser.print_usage()
    75     exit(2)
    76     exit(2)
    76 
       
    77 
    77 
    78 # use the WGS-84 ellipsoid
    78 # use the WGS-84 ellipsoid
    79 rE = 6356752.314245 # earth's radius
    79 rE = 6356752.314245 # earth's radius
    80 a = 6378137.0
    80 a = 6378137.0
    81 b = 6356752.314245179
    81 b = 6356752.314245179
   331             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
   331             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
   332                                        ).text, timeformat) for trkpt in trkpts]
   332                                        ).text, timeformat) for trkpt in trkpts]
   333         except Exception as e:
   333         except Exception as e:
   334             print e
   334             print e
   335             times = None
   335             times = None
   336         
   336 
   337         # save original trackseg for plotting
   337         # save original trackseg for plotting
   338         if options.plot:
   338         if options.plot:
   339             tracksegs_old.append([[lats[i], lons[i], eles[i]] for i in range(n)])
   339             tracksegs_old.append([[lats[i], lons[i], eles[i]] for i in range(n)])
   340         
   340         
   341         # calculate projected points to work on
   341         # calculate projected points to work on
   354         print 'number of points:', n, '-', n - n_new, '=', n_new
   354         print 'number of points:', n, '-', n - n_new, '=', n_new
   355         
   355         
   356         # delete certain points from original data
   356         # delete certain points from original data
   357         delete_pnums = [i for i in range(n) if i not in final_pnums]
   357         delete_pnums = [i for i in range(n) if i not in final_pnums]
   358         for i in reversed(delete_pnums):
   358         for i in reversed(delete_pnums):
   359             trkseg.remove (trkpts[i])
   359             trkseg.remove (trkpts[i])   # remove from the xml-tree
   360         
   360             del trkpts [i]              # also remove from the list
       
   361 
       
   362         # remove certain sub-elements from remaining points
       
   363         options.remove = options.remove.replace ('+','extensions,hdop,')
       
   364         taglist = options.remove.split (',')
       
   365         if taglist: print 'remove %s subelements from points' % ', '.join (taglist)
       
   366         for trkpnt in trkpts:
       
   367             for tag in taglist:
       
   368                 e = trkpnt.find (nsmap + tag)
       
   369                 if e != None: trkpnt.remove (e)
       
   370 
   361         # save reduced trackseg for plotting
   371         # save reduced trackseg for plotting
   362         if options.plot:
   372         if options.plot:
   363             tracksegs_new.append([[float(trkpt.get('lat')),
   373             tracksegs_new.append ([[float(trkpt.get('lat')),
   364                 float(trkpt.get('lon')), float(trkpt.find(nsmap + 'ele').text)]
   374                 float(trkpt.get('lon')), float(trkpt.find(nsmap + 'ele').text)]
   365                 for trkpt in trkseg.findall(nsmap + 'trkpt')])
   375                 for trkpt in trkseg.findall(nsmap + 'trkpt')])
   366         
       
   367     
   376     
   368     # export data to file
   377     # export data to file
   369     if options.ofname != None:
   378     if options.ofname != None:
   370         ofname = options.ofname
   379         ofname = options.ofname
   371     elif fname.endswith('.gpx'):
   380     elif fname.endswith('.gpx'):
   377     outfile.close()
   386     outfile.close()
   378     print 'modified copy written to', ofname
   387     print 'modified copy written to', ofname
   379 
   388 
   380     # plot result to screen
   389     # plot result to screen
   381     if options.plot:
   390     if options.plot:
   382         from subprocess import Popen, PIPE
       
   383 
       
   384         latm, lonm, elesum = xyz_to_latlonele(sumx, sumy, sumz)
   391         latm, lonm, elesum = xyz_to_latlonele(sumx, sumy, sumz)
   385 
   392 
       
   393         data_old = []
   386         for trkseg in tracksegs_old:
   394         for trkseg in tracksegs_old:
   387             data_old = []
       
   388             for trkpt in trkseg:
   395             for trkpt in trkseg:
   389                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
   396                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
   390                 data_old.append (xy)
   397                 data_old.append (xy)
   391         
   398         
       
   399         data = []
   392         for trkseg in tracksegs_new:
   400         for trkseg in tracksegs_new:
   393             data = []
       
   394             for trkpt in trkseg:
   401             for trkpt in trkseg:
   395                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
   402                 xy = project_to_meters(trkpt[0], trkpt[1], latm, lonm)
   396                 data.append (xy)
   403                 data.append (xy)
   397 
   404 
   398         #~ gnuPlotCmd = ['gnuplot', '-persist']
   405         from subprocess import Popen, PIPE
   399         gnuPlotCmd = ['gnuplot']
   406         gnuPlotCmd = ['gnuplot']
   400         plot = Popen (gnuPlotCmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
   407         plot = Popen (gnuPlotCmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
   401         plot.stdin.write (b"plot '-' with linespoints lc rgb 'red' pt 4, '-' with linespoints pt 6\n")
   408         plot.stdin.write (b"plot '-' with linespoints lc rgb 'red' pt 4, '-' with linespoints pt 6\n")
   402         plot.stdin.write ("\n".join ('%f %f' % d for d in data).encode ())
   409         plot.stdin.write ("\n".join ('%f %f' % d for d in data).encode ())
   403         plot.stdin.write (b'\ne\n')
   410         plot.stdin.write (b'\ne\n')
   404         plot.stdin.write ("\n".join ('%f %f' % d for d in data_old).encode ())
   411         plot.stdin.write ("\n".join ('%f %f' % d for d in data_old).encode ())
   405         plot.stdin.write (b'\ne\n')
   412         plot.stdin.write (b'\ne\n')
   406         plot.stdin.flush ()
   413         plot.stdin.flush ()
   407         #~ raw_input ('druk')
   414         raw_input ('druk')
   408         while 1: time.sleep (1)