Mercurial Hosting > traffic-intelligence
changeset 7:ffddccfab7f9
loading shell objects from NGSIM works
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Thu, 05 Nov 2009 17:17:20 -0500 |
parents | 597d61c1eebe |
children | 59b7e3954178 |
files | python/moving.py python/storage.py python/utils.py |
diffstat | 3 files changed, 77 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/python/moving.py Wed Nov 04 19:13:08 2009 -0500 +++ b/python/moving.py Thu Nov 05 17:17:20 2009 -0500 @@ -3,6 +3,8 @@ import utils; +from shapely.geometry import Polygon + __metaclass__ = type #class MovingObject: @@ -53,6 +55,13 @@ '''Largest interval comprising self and interval2''' return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) + +# class BoundingPolygon: +# '''Class for a polygon bounding a set of points +# with methods to create intersection, unions... +# ''' +# We will use the polygon class of Shapely + class STObject: '''Class for spatio-temporal object i.e. with temporal and spatial existence @@ -60,13 +69,13 @@ It does not mean that the object is defined for all time instants within the time interval''' - def __init__(self, timeInterval = TimeInterval(), boundingPolygon = None, num = None): + def __init__(self, num = None, timeInterval = None, boundingPolygon = None): + self.num = num self.timeInterval = timeInterval self.boundingPolygon = boundingPolygon - self.num = num def empty(self): - return self.timeInterval.empty() + return self.timeInterval.empty() or not self.boudingPolygon def getFirstInstant(self): return self.timeInterval.first() @@ -74,22 +83,44 @@ def getLastInstant(self): return self.timeInterval.first() - - -# return bounding box, and generic polygon, that defaults to box - -class Trajectory(STObject): +class Trajectory: '''Class for trajectories i.e. a temporal sequence of positions''' - pass + + def __init__(self, positions = None): + self.positions = positions + + def addPosition(self, point): + if not self.positions: + self.positions = [[point[0]],[point[1]]] + else: + self.positions[0].append(point[0]) + self.positions[1].append(point[1]) + + def draw(self): + from matplotlib.pylab import plot + plot(self.positions[0], self.positions[1]) +class MovingObject(STObject): + '''Class for moving objects + i.e. with a trajectory and a geometry (volume) + and a type (e.g. road user) + ''' + + def __init__(self, num = None, timeInterval = None, trajectory = None, geometry = None, type = None): + STObject.__init__(self, num, timeInterval) + self.trajectory = trajectory + self.geometry = geometry + self.type = type + # compute bounding polygon from trajectory + + # def computeVelocities(self): class TemporalIndicator: '''Class for temporal indicators i.e. indicators that take a value at specific instants''' pass - if __name__ == "__main__": import doctest import unittest
--- a/python/storage.py Wed Nov 04 19:13:08 2009 -0500 +++ b/python/storage.py Thu Nov 05 17:17:20 2009 -0500 @@ -1,7 +1,8 @@ #! /usr/bin/env python '''Various utilities to save and load data''' -import utils; +import utils +import moving __metaclass__ = type @@ -12,52 +13,59 @@ def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1): '''Reads data from the trajectory data provided by NGSIM project and returns the list of Feature objects''' - features = [] + objects = [] - input = utils.open(filename) + input = utils.openCheck(filename) if not input: import sys sys.exit() - def createFeature(numbers): + def createObject(numbers): firstFrameNum = int(numbers[1]) - lastFrameNum = firstFrameNum+int(numbers[2])-1 - f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0])) - f.sizeLength = float(numbers[8]) - f.sizeWidth = float(numbers[9]) - f.userType = int(numbers[10]) - f.positions = [[float(numbers[6])],[float(numbers[7])]] - f.speeds = [float(numbers[11])] - return f + time = moving.TimeInterval(firstFrameNum, firstFrameNum+int(numbers[2])-1) + obj = moving.MovingObject(num = int(numbers[0]), timeInterval = time) + # do the geometry and usertype + +# firstFrameNum = int(numbers[1]) +# lastFrameNum = firstFrameNum+int(numbers[2])-1 +# f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0])) +# f.sizeLength = float(numbers[8]) +# f.sizeWidth = float(numbers[9]) +# f.userType = int(numbers[10]) +# f.positions = [[float(numbers[6])],[float(numbers[7])]] +# f.speeds = [float(numbers[11])] + return obj numbers = input.readline().strip().split() if (len(numbers) > 0): - f = createFeature(numbers) + obj = createObject(numbers) for line in input: numbers = line.strip().split() - if f.num != int(numbers[0]): + if obj.num != int(numbers[0]): # check and adapt the length to deal with issues in NGSIM data - objLength = f.length() - if (objLength != len(f.positions[0])): - print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0])) - f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1 - f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? - features.append(f) - if (nObjects>0) and (len(features)>=nObjects): + #objLength = f.length() + #if (objLength != len(f.positions[0])): + # print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0])) + # f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1 + #f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? + objects.append(obj) + if (nObjects>0) and (len(objects)>=nObjects): break - f = createFeature(numbers) + obj = createObject(numbers) else: - f.positions[0] += [float(numbers[6])] - f.positions[1] += [float(numbers[7])] - f.speeds += [float(numbers[11])] + print(numbers[6]) +# f.positions[0] += [float(numbers[6])] +# f.positions[1] += [float(numbers[7])] +# f.speeds += [float(numbers[11])] + # if (f.sizeWidth != float(numbers[9])): # print 'changed width obj %d' % (f.num) # if (f.sizeLength != float(numbers[8])): # print 'changed length obj %d' % (f.num) input.close() - return features + return objects def convertNgsimFile(inFile, outFile, append = False, nObjects = -1, sequenceNum = 0): '''Reads data from the trajectory data provided by NGSIM project
--- a/python/utils.py Wed Nov 04 19:13:08 2009 -0500 +++ b/python/utils.py Thu Nov 05 17:17:20 2009 -0500 @@ -60,7 +60,8 @@ if __name__ == "__main__": import doctest import unittest - suite = doctest.DocFileSuite('tests/ubc_utils.txt') + #suite = doctest.DocFileSuite('tests/ubc_utils.txt') + suite = doctest.DocTestSuite() unittest.TextTestRunner().run(suite) #doctest.testmod() #doctest.testfile("example.txt")