diff -r 5467c893d121 -r f93786d4f68e gpx_reduce.py --- a/gpx_reduce.py Sun Aug 20 21:59:58 2017 +0200 +++ b/gpx_reduce.py Mon Aug 21 14:07:37 2017 +0200 @@ -31,10 +31,10 @@ import datetime import sys import time -import numpy as sc +import numpy as np import numpy.linalg as la from math import * -from lxml import etree +import xml.etree.ElementTree as etree from optparse import OptionParser @@ -86,7 +86,7 @@ def distance(p1_, pm_, p2_, ele_weight=1.0): # returns distance of pm from line between p1 and p2 - p1, pm, p2 = sc.array(p1_), sc.array(pm_), sc.array(p2_) + p1, pm, p2 = np.array(p1_), np.array(pm_), np.array(p2_) h1, hm, h2 = la.norm(p1), la.norm(pm), la.norm(p2) if ele_weight != 1.0 and min(h1, hm, h2) > 0.0: hmean = (h1 + hm + h2) / 3.0 @@ -100,7 +100,7 @@ return la.norm(vm) linem = line / linel - position = sc.dot(vm, linem) / linel + position = np.dot(vm, linem) / linel if position < 0.0: return la.norm(vm) elif position > 1.0: @@ -181,8 +181,8 @@ imin = None costmin = float('inf') for i1 in reversed(range(i2)): - p1 = sc.array(points[i1]['p']) - p2 = sc.array(points[i2]['p']) + p1 = np.array(points[i1]['p']) + p2 = np.array(points[i2]['p']) seglength = la.norm(p2 - p1) # estimate speed between p1 and p2 @@ -216,7 +216,7 @@ distances_squared = [] # iterate all medium points between i1 and i2 for im in range(i1+1, i2): - pm = sc.array(points[im]['p']) + pm = np.array(points[im]['p']) d = distance(p1, pm, p2, options.ele_weight) if (d <= options.max_dist): d_sq = (d / options.max_dist) ** 2 @@ -227,10 +227,10 @@ i1_i2_segment_valid = False # check if connection to any further point i1 is impossible - d1 = sc.dot(p1 - p2, p1 - p2) - d2 = sc.dot(pm - p2, pm - p2) + d1 = np.dot(p1 - p2, p1 - p2) + d2 = np.dot(pm - p2, pm - p2) dd = options.max_dist ** 2 - d1d2 = sc.dot(p1 - p2, pm - p2) + d1d2 = np.dot(p1 - p2, pm - p2) # formula from cosines of point separation angle and cone-opening angles around points if (d1 > dd and d2 > dd and (d1d2 + dd)**2 < (d2 - dd) * (d1 - dd)): lower_i1_possible = False @@ -257,13 +257,13 @@ # add a penalty for kinks if options.bend > 0.: if points[i1]['prev'] != -1: - p0 = sc.array(points[points[i1]['prev']]['p']) + p0 = np.array(points[points[i1]['prev']]['p']) v0 = p1 - p0 v1 = p2 - p1 if la.norm(v0) > 0. and la.norm(v1) > 0.: v0 /= la.norm(v0) v1 /= la.norm(v1) - kink = (1.0 - sc.dot(v0, v1)) / 2.0 + kink = (1.0 - np.dot(v0, v1)) / 2.0 penalties[i1] += options.bend * kink # find best predecessor @@ -310,15 +310,14 @@ # import xml data from files print 'opening file', fname infile = open(fname) - tree = etree.parse(infile) infile.close() + gpx = tree.getroot() - if gpx.nsmap.has_key(None): - nsmap = '{' + gpx.nsmap[None] + '}' - else: - nsmap = '' - + nsurl = gpx.tag.split ('}')[0][1:] # == 'http://www.topografix.com/GPX/1/1' + etree.register_namespace('', nsurl) # default namespace -> xmlns:.... in the output + nsmap = '{' + nsurl + '}' # prefix for all tags in the tree + # extract data from xml for trkseg in gpx.findall('.//' + nsmap + 'trkseg'): trkpts = trkseg.findall(nsmap + 'trkpt') @@ -357,7 +356,7 @@ # 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): - del trkseg[trkseg.index(trkpts[i])] + trkseg.remove (trkpts[i]) # save reduced trackseg for plotting if options.plot: @@ -374,12 +373,10 @@ else: ofname = fname + '_reduced.gpx' outfile = open(ofname, 'w') - outfile.write(etree.tostring(tree, xml_declaration=True, - pretty_print=True, encoding='utf-8')) + tree.write (outfile, encoding="utf-8", xml_declaration=True, default_namespace=None, method="xml") outfile.close() print 'modified copy written to', ofname - - + # plot result to screen if options.plot: import pylab as pl