view python/ubc_utils.py @ 25:28e546861263

added Point class and modified trajectory accordingly
author Nicolas Saunier <nico@confins.net>
date Sat, 05 Dec 2009 12:08:25 -0500
parents 9d6831cfe675
children 1a2ac2d4f53a
line wrap: on
line source

#! /usr/bin/env python
'''Various utilities to load data saved by the UBC tool(s)'''

import utils
from moving import MovingObject, TimeInterval, Trajectory

__metaclass__ = type

delimiterChar = '%';

def getLines(f):
    '''Gets a complete entry (all the lines) in between delimiterChar.'''
    dataStrings = []
    s = utils.readline(f)
    while (len(s) > 0) and (not s.startswith(delimiterChar)):
        dataStrings += [s.strip()]
        s = utils.readline(f)
    return dataStrings

def loadTrajectories(filename, nObjects = -1):
    '''Loads trajectories'''

    file = utils.openCheck(filename)
    if (not file):
        return []

    objects = []
    objNum = 0
    lines = getLines(file)
    while (lines != []) and ((nObjects<0) or (objNum<nObjects)):
        l = lines[0].split(' ')
        parsedLine = [int(n) for n in l[:4]]
        obj = MovingObject(num = objNum, timeInterval = TimeInterval(parsedLine[1],parsedLine[2]))
        add = True
        if (len(lines) >= 3):
            obj.positions = Trajectory(lines[1], lines[2])
            if (len(lines) >= 5):
                obj.velocities = Trajectory(lines[3], lines[4])

        if len(lines) != 2:
            objects.append(obj)
            objNum+=1
        else:
            print("Error two lines of data for feature %d"%(f.num))

        lines = getLines(file)

    file.close()
    return objects