from xml.dom.minidom import * from datetime import datetime from numpy import * from math import sin,cos,radians # def readtv(filename): t=[] v=[]; tvfile=open(filename,'r') for line in tvfile: tandv=line.strip().split(",") t.append(float(tandv[0])) v.append(float(tandv[1])) tvfile.close() return t,v # Reads a gpx file with a given name, and computes time and speed values from this. theses values are stored in the comma separated output file with a given name def gpxtotv(filenamein,filenameout): doc=parse(filenamein) lat=[] lon=[] elev=[] t=[] childnodes=doc.getElementsByTagName('trkpt') for nexttrkpt in childnodes: nextlat=float(nexttrkpt.getAttribute('lat')) nextlon=float(nexttrkpt.getAttribute('lon')) nextelev=[]; nextelevs=nexttrkpt.getElementsByTagName('ele') if nextelevs.length>=1: nextelev=float(nextelevs[0].firstChild.data) nexttime=[] nexttimes=nexttrkpt.getElementsByTagName('time') if nexttimes.length>=1: try: timeStr=nexttimes[0].firstChild.data timeStr=timeStr.replace('T',' ') timeStr=timeStr.replace('Z', '') dt=datetime.strptime(timeStr, "%Y-%m-%d %H:%M:%S") nexttime=dt.hour*3600 + dt.minute*60 + dt.second except: print 'Something went wrong' if nextlat!=0 and nextlon!=0 and nextelev!=0 and nexttime!=0: lat.append(nextlat) lon.append(nextlon) elev.append(nextelev) t.append(nexttime) coords=zeros((3,len(lat))) r=6378500 elev=array(elev) lat=array(lat) lon=array(lon) t=array(t) coordsx=zeros((len(lat))) coordsy=zeros((len(lat))) coordsz=zeros((len(lat))) for k in range(len(lat)): coordsx[k]=(r+elev[k])*cos(radians(lon[k]))*sin(radians(90-lat[k])) coordsy[k]=(r+elev[k])*sin(radians(lon[k]))*sin(radians(90-lat[k])) coordsz[k]=(r+elev[k])*cos(radians(90-lat[k])) v=zeros((len(lat)-1)); for k in range(len(lat)-1): v[k]=sqrt((coordsx[k+1]-coordsx[k])**2 + (coordsy[k+1]-coordsy[k])**2 + (coordsz[k+1]-coordsz[k])**2)/(t[k+1]-t[k]) tvfile=open(filenameout,'w') for k in range(len(lat)-1): tvfile.write(str(t[k]) + ',' + str(v[k]) + '\n') tvfile.close()