- changes for python3 default tip
authorwim
Thu, 15 Jun 2023 08:46:26 +0200
changeset 15 cfb0607e5afc
parent 14 f440529b9606
- changes for python3
gpx_plot.py
gpx_reduce.py
--- a/gpx_plot.py	Sat May 23 16:51:16 2020 +0200
+++ b/gpx_plot.py	Thu Jun 15 08:46:26 2023 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf8 -*-
 
 '''
@@ -46,7 +46,7 @@
 # find gnuplot binary
 gnuPlotCmd = which (options.gnuplot)
 if not gnuPlotCmd:
-    print '***', options.gnuplot, 'does not exist or is not executable\n'
+    print ('***', options.gnuplot, 'does not exist or is not executable\n')
     parser.print_help ()
     sys.exit ()
 
@@ -109,9 +109,9 @@
     
     # import xml data from files
     if not os.path.exists (fname):
-        print fname, 'does not exist'
+        print (fname, 'does not exist')
         continue
-    print 'opening file', fname
+    print ('opening file', fname)
     infile = open (fname)
     tree = etree.parse(infile)
     infile.close()
@@ -134,7 +134,7 @@
             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
                                        ).text, timeformat) for trkpt in trkpts]
         except Exception as e:
-            print '-- trackpoint without time'
+            print ('-- trackpoint without time')
             times = None
 
         # save original trackseg for plotting
@@ -147,12 +147,12 @@
             sumy += y
             sumz += z
 
-        print 'segment %d, with %d points:' % (si, n)
+        print ('segment %d, with %d points:' % (si, n))
         ntot += n
 
     tracks.append (tracksegs)
     npoints.append ((fname.replace ('_','\_'), ntot))   # underscore -> subscripts in gnuplot
-    print 'number of points in track %s = %d:' % (fname, ntot)
+    print ('number of points in track %s = %d:' % (fname, ntot))
 
 latm, lonm, elesum = xyz_to_latlonele (sumx, sumy, sumz)
 
@@ -179,9 +179,9 @@
 
 from subprocess import Popen, PIPE
 try:
-    plot = Popen ([gnuPlotCmd], stdin=PIPE, stdout=PIPE, stderr=PIPE)
+    plot = Popen ([gnuPlotCmd], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
 except Exception as e:
-    print e, 'while executing', gnuPlotCmd
+    print (e, 'while executing', gnuPlotCmd)
     sys.exit ()
 
 range = 'set xrange [%f:%f]\nset yrange [%f:%f]\n' % (xmin, xmax, ymin, ymax)
@@ -193,4 +193,4 @@
 plot.stdin.write ("\n".join (data))
 plot.stdin.write ('\n')
 plot.stdin.flush ()
-raw_input ('press enter to exit program')
+input ('press enter to exit program')
--- a/gpx_reduce.py	Sat May 23 16:51:16 2020 +0200
+++ b/gpx_reduce.py	Thu Jun 15 08:46:26 2023 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf8 -*-
 
 '''
@@ -33,6 +33,7 @@
 from math import *
 import xml.etree.ElementTree as etree
 from optparse import OptionParser
+from functools import reduce
 
 
 parser = OptionParser('usage: %prog [options] input-file.gpx')
@@ -142,7 +143,7 @@
     
     # progress printing initialisations
     progress_printed = False
-    progress = None
+    progress = 0
     tprint = time.time()
     
     # execute Dijkstra-like algorithm on points
@@ -242,7 +243,7 @@
         # find best predecessor
         imin = None
         costmin = float('inf')
-        for prev, penalty in penalties.iteritems():
+        for prev, penalty in penalties.items():
             # cost function is sum of points used (1.0) plus penalties
             cost = points[prev]['cost'] + 1.0 + penalty
             if cost < costmin:
@@ -255,12 +256,12 @@
         if options.verbose == 1 and (100 * i2) / n > progress and time.time() >= tprint + 1:
             tprint = time.time()
             progress = (100 * i2) / n
-            print '\r', progress, '%',
+            print ('\r', progress, '%', end=' ')
             sys.stdout.flush()
             progress_printed = True
     
     if progress_printed:
-        print '\r',
+        print ('\r', end=' ')
     
     # trace route backwards to collect final points
     final_pnums = []
@@ -280,7 +281,7 @@
     newtot = 0 # total number of trackpoints after removal
     
     # import xml data from files
-    print 'opening file', fname
+    print ('opening file', fname)
     infile = open(fname)
     tree = etree.parse(infile)
     infile.close()
@@ -303,7 +304,7 @@
             times = [datetime.datetime.strptime(trkpt.find(nsmap + 'time'
                                        ).text, timeformat) for trkpt in trkpts]
         except Exception as e:
-            print '-- trackpoint without time'
+            print ('-- trackpoint without time')
             times = None
 
         # calculate projected points to work on
@@ -316,7 +317,7 @@
         final_pnums = reduced_track_indices(coords, times)
 
         n_new = len (final_pnums)
-        print 'segment %d, with %d - %d = %d points' % (si, n, n - n_new, n_new)
+        print ('segment %d, with %d - %d = %d points' % (si, n, n - n_new, n_new))
         ntot += n
         newtot += n_new
 
@@ -329,13 +330,13 @@
         # 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)
+        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)
 
-    print 'total number of points: %d - %d = %d' % (ntot, ntot - newtot, newtot)
+    print ('total number of points: %d - %d = %d' % (ntot, ntot - newtot, newtot))
 
     # export data to file
     if options.ofname != None:
@@ -344,7 +345,7 @@
         ofname = fname[:-4] + '_reduced.gpx'
     else:
         ofname = fname + '_reduced.gpx'
-    outfile = open(ofname, 'w')
+    outfile = open(ofname, 'wb')
     tree.write (outfile, encoding="utf-8", xml_declaration=True, default_namespace=None, method="xml")
     outfile.close()
-    print 'modified copy written to', ofname
+    print ('modified copy written to', ofname)