SINTEF / Splipy

Spline modelling made easy.
GNU General Public License v3.0
100 stars 18 forks source link

More curve factories #39

Closed VikingScientist closed 6 years ago

VikingScientist commented 6 years ago

Example application of fitting curve to point-cloud. The following code takes as input an exported (GPX-file) GPS log of your training: bike, running, skiing etc as captured from a training app on your phone. Tested on export files from endomondo

import xml.etree.ElementTree as etree                                          
import utm                                                                     
import sys                                                                     
import numpy as np                                                             
from datetime import datetime                                                  
from splipy import *                                                           
from splipy.IO import *                                                        
import splipy.curve_factory as curves                                    

# initialize data containers                                                   
X = []                                                                         
T = []                                                                         

# setup xml parser                                                             
myFinds = etree.parse(sys.argv[1])                                             
location = "{http://www.topografix.com/GPX/1/1}"                               
root = myFinds.getroot()                                                       

trk = root.find(location+'trk')                                                
trkseg = trk.find(location+'trkseg')                                           

# for all trackpoints in the track segment                                     
for trkpt in trkseg:                                                           
    utmcoord  = utm.from_latlon(float(trkpt.attrib['lat']), float(trkpt.attrib['lon']))
    print(utmcoord)                                                            
    elevation = trkpt.find(location+'ele')                                     
    timestamp = trkpt.find(location+'time')                                    
    if(elevation is not None): # incomplete track-points exist...              
        z = float(elevation.text)                                              
    if(timestamp is not None): # is going to crash later for incomplete time stamps
        timeobject = datetime.strptime(timestamp.text,'%Y-%m-%dT%H:%M:%SZ')    
        t = timeobject.timestamp()                                             
    x = utmcoord[0]                                                            
    y = utmcoord[1]                                                            

    X.append([x,y])                                                            
    T.append(t)                                                                

print(np.diff(T))                                                              

### do spline fit of the point data extracted                        
crv = curves.fit_points(X, t=T, atol=20)                                       

Also added a 3point circle segment factory. The method signature might be a mouthful and I am open to hear other suggestions if you have any.