diff -r f440529b9606 -r cfb0607e5afc gpx_reduce.py --- 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)