comparison python/moving.py @ 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 30559b2cf7a9
comparison
equal deleted inserted replaced
6:597d61c1eebe 7:ffddccfab7f9
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Libraries for moving objects, trajectories...''' 2 '''Libraries for moving objects, trajectories...'''
3 3
4 import utils; 4 import utils;
5
6 from shapely.geometry import Polygon
5 7
6 __metaclass__ = type 8 __metaclass__ = type
7 9
8 #class MovingObject: 10 #class MovingObject:
9 11
51 53
52 def intersection(self, interval2): 54 def intersection(self, interval2):
53 '''Largest interval comprising self and interval2''' 55 '''Largest interval comprising self and interval2'''
54 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) 56 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last))
55 57
58
59 # class BoundingPolygon:
60 # '''Class for a polygon bounding a set of points
61 # with methods to create intersection, unions...
62 # '''
63 # We will use the polygon class of Shapely
64
56 class STObject: 65 class STObject:
57 '''Class for spatio-temporal object 66 '''Class for spatio-temporal object
58 i.e. with temporal and spatial existence 67 i.e. with temporal and spatial existence
59 (time interval and bounding polygon for positions (e.g. rectangle)). 68 (time interval and bounding polygon for positions (e.g. rectangle)).
60 It does not mean that the object is defined 69 It does not mean that the object is defined
61 for all time instants within the time interval''' 70 for all time instants within the time interval'''
62 71
63 def __init__(self, timeInterval = TimeInterval(), boundingPolygon = None, num = None): 72 def __init__(self, num = None, timeInterval = None, boundingPolygon = None):
73 self.num = num
64 self.timeInterval = timeInterval 74 self.timeInterval = timeInterval
65 self.boundingPolygon = boundingPolygon 75 self.boundingPolygon = boundingPolygon
66 self.num = num
67 76
68 def empty(self): 77 def empty(self):
69 return self.timeInterval.empty() 78 return self.timeInterval.empty() or not self.boudingPolygon
70 79
71 def getFirstInstant(self): 80 def getFirstInstant(self):
72 return self.timeInterval.first() 81 return self.timeInterval.first()
73 82
74 def getLastInstant(self): 83 def getLastInstant(self):
75 return self.timeInterval.first() 84 return self.timeInterval.first()
76 85
77 86 class Trajectory:
78
79 # return bounding box, and generic polygon, that defaults to box
80
81 class Trajectory(STObject):
82 '''Class for trajectories 87 '''Class for trajectories
83 i.e. a temporal sequence of positions''' 88 i.e. a temporal sequence of positions'''
84 pass
85 89
90 def __init__(self, positions = None):
91 self.positions = positions
92
93 def addPosition(self, point):
94 if not self.positions:
95 self.positions = [[point[0]],[point[1]]]
96 else:
97 self.positions[0].append(point[0])
98 self.positions[1].append(point[1])
99
100 def draw(self):
101 from matplotlib.pylab import plot
102 plot(self.positions[0], self.positions[1])
103
104 class MovingObject(STObject):
105 '''Class for moving objects
106 i.e. with a trajectory and a geometry (volume)
107 and a type (e.g. road user)
108 '''
109
110 def __init__(self, num = None, timeInterval = None, trajectory = None, geometry = None, type = None):
111 STObject.__init__(self, num, timeInterval)
112 self.trajectory = trajectory
113 self.geometry = geometry
114 self.type = type
115 # compute bounding polygon from trajectory
116
117 # def computeVelocities(self):
86 118
87 class TemporalIndicator: 119 class TemporalIndicator:
88 '''Class for temporal indicators 120 '''Class for temporal indicators
89 i.e. indicators that take a value at specific instants''' 121 i.e. indicators that take a value at specific instants'''
90 pass 122 pass
91
92 123
93 if __name__ == "__main__": 124 if __name__ == "__main__":
94 import doctest 125 import doctest
95 import unittest 126 import unittest
96 #suite = doctest.DocFileSuite('tests/ubc_utils.txt') 127 #suite = doctest.DocFileSuite('tests/ubc_utils.txt')