gpx_plot.py
changeset 15 cfb0607e5afc
parent 13 b6ccd1aab66f
equal deleted inserted replaced
14:f440529b9606 15:cfb0607e5afc
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python3
     2 # -*- coding: utf8 -*-
     2 # -*- coding: utf8 -*-
     3 
     3 
     4 '''
     4 '''
     5 gpx_plot.py calls gnuplot to draw the gpx-tracks given as arguments.
     5 gpx_plot.py calls gnuplot to draw the gpx-tracks given as arguments.
     6 Copyright 2017 willem179
     6 Copyright 2017 willem179
    44 (options, args) = parser.parse_args()
    44 (options, args) = parser.parse_args()
    45 
    45 
    46 # find gnuplot binary
    46 # find gnuplot binary
    47 gnuPlotCmd = which (options.gnuplot)
    47 gnuPlotCmd = which (options.gnuplot)
    48 if not gnuPlotCmd:
    48 if not gnuPlotCmd:
    49     print '***', options.gnuplot, 'does not exist or is not executable\n'
    49     print ('***', options.gnuplot, 'does not exist or is not executable\n')
    50     parser.print_help ()
    50     parser.print_help ()
    51     sys.exit ()
    51     sys.exit ()
    52 
    52 
    53 if len(args) < 1:
    53 if len(args) < 1:
    54     parser.print_help ()
    54     parser.print_help ()
   107     sumx, sumy, sumz = 0.0, 0.0, 0.0
   107     sumx, sumy, sumz = 0.0, 0.0, 0.0
   108     ntot = 0    # total number of trackpoints (sum of segments)
   108     ntot = 0    # total number of trackpoints (sum of segments)
   109     
   109     
   110     # import xml data from files
   110     # import xml data from files
   111     if not os.path.exists (fname):
   111     if not os.path.exists (fname):
   112         print fname, 'does not exist'
   112         print (fname, 'does not exist')
   113         continue
   113         continue
   114     print 'opening file', fname
   114     print ('opening file', fname)
   115     infile = open (fname)
   115     infile = open (fname)
   116     tree = etree.parse(infile)
   116     tree = etree.parse(infile)
   117     infile.close()
   117     infile.close()
   118 
   118 
   119     gpx = tree.getroot()
   119     gpx = tree.getroot()
   132         eles = [float(trkpt.find(nsmap + 'ele').text) for trkpt in trkpts]
   132         eles = [float(trkpt.find(nsmap + 'ele').text) for trkpt in trkpts]
   133         try:
   133         try:
   134             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
   134             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
   135                                        ).text, timeformat) for trkpt in trkpts]
   135                                        ).text, timeformat) for trkpt in trkpts]
   136         except Exception as e:
   136         except Exception as e:
   137             print '-- trackpoint without time'
   137             print ('-- trackpoint without time')
   138             times = None
   138             times = None
   139 
   139 
   140         # save original trackseg for plotting
   140         # save original trackseg for plotting
   141         tracksegs.append ([ (lats[i], lons[i], eles[i]) for i in range(n) ])
   141         tracksegs.append ([ (lats[i], lons[i], eles[i]) for i in range(n) ])
   142 
   142 
   145             x, y, z = latlonele_to_xyz (lats[i], lons[i], eles[i])
   145             x, y, z = latlonele_to_xyz (lats[i], lons[i], eles[i])
   146             sumx += x
   146             sumx += x
   147             sumy += y
   147             sumy += y
   148             sumz += z
   148             sumz += z
   149 
   149 
   150         print 'segment %d, with %d points:' % (si, n)
   150         print ('segment %d, with %d points:' % (si, n))
   151         ntot += n
   151         ntot += n
   152 
   152 
   153     tracks.append (tracksegs)
   153     tracks.append (tracksegs)
   154     npoints.append ((fname.replace ('_','\_'), ntot))   # underscore -> subscripts in gnuplot
   154     npoints.append ((fname.replace ('_','\_'), ntot))   # underscore -> subscripts in gnuplot
   155     print 'number of points in track %s = %d:' % (fname, ntot)
   155     print ('number of points in track %s = %d:' % (fname, ntot))
   156 
   156 
   157 latm, lonm, elesum = xyz_to_latlonele (sumx, sumy, sumz)
   157 latm, lonm, elesum = xyz_to_latlonele (sumx, sumy, sumz)
   158 
   158 
   159 data = []
   159 data = []
   160 xmin, xmax = ymin, ymax = float ('inf'), float ('-inf')
   160 xmin, xmax = ymin, ymax = float ('inf'), float ('-inf')
   177     xmax += dr
   177     xmax += dr
   178     xmin -= dr
   178     xmin -= dr
   179 
   179 
   180 from subprocess import Popen, PIPE
   180 from subprocess import Popen, PIPE
   181 try:
   181 try:
   182     plot = Popen ([gnuPlotCmd], stdin=PIPE, stdout=PIPE, stderr=PIPE)
   182     plot = Popen ([gnuPlotCmd], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
   183 except Exception as e:
   183 except Exception as e:
   184     print e, 'while executing', gnuPlotCmd
   184     print (e, 'while executing', gnuPlotCmd)
   185     sys.exit ()
   185     sys.exit ()
   186 
   186 
   187 range = 'set xrange [%f:%f]\nset yrange [%f:%f]\n' % (xmin, xmax, ymin, ymax)
   187 range = 'set xrange [%f:%f]\nset yrange [%f:%f]\n' % (xmin, xmax, ymin, ymax)
   188 plot.stdin.write (range)
   188 plot.stdin.write (range)
   189 
   189 
   191 plot.stdin.write ('plot ' + curves + '\n')
   191 plot.stdin.write ('plot ' + curves + '\n')
   192 
   192 
   193 plot.stdin.write ("\n".join (data))
   193 plot.stdin.write ("\n".join (data))
   194 plot.stdin.write ('\n')
   194 plot.stdin.write ('\n')
   195 plot.stdin.flush ()
   195 plot.stdin.flush ()
   196 raw_input ('press enter to exit program')
   196 input ('press enter to exit program')