Mercurial Hosting > traffic-intelligence
changeset 2:de5642925615
started implementation of TimeInterval and Spatio-temporal object
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 19 Oct 2009 13:16:21 -0400 |
parents | f5806717cc36 |
children | ace29ecfb846 |
files | python/moving.py python/utils.py |
diffstat | 2 files changed, 89 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/python/moving.py Sun Oct 18 22:32:46 2009 -0400 +++ b/python/moving.py Mon Oct 19 13:16:21 2009 -0400 @@ -6,3 +6,88 @@ __metaclass__ = type #class MovingObject: + +class TimeInterval: + '''Temporal interval''' + def __init__(self, first=0, last=-1): + self.first=first + self.last=last + + def __str__(self): + return '%d %d'%(self.first, self.last) + + def empty(self): + ''' + >>> TimeInterval().empty() + True + >>> TimeInterval(0,1).empty() + False + ''' + return self.first > self.last + + def length(self): + '''Returns the length of the interval + + >>> TimeInterval(0,1).length() + 2 + >>> TimeInterval(10,8).length() + 0 + ''' + return max(0,self.last-self.first+1) + + def getList(self): + return [self.first, self.last] + + def contains(self, instant): + return (self.first<=instant and self.last>=instant) + + def inside(self, interval2): + 'indicates if the temporal interval of self is comprised in interval2' + return (self.first >= interval2.first) and (self.last <= interval2.last) + + def union(self, interval2): + '''Largest interval comprising self and interval2''' + return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last)) + + def intersection(self, interval2): + '''Largest interval comprising self and interval2''' + return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) + +class STObject: + '''Class for spatio-temporal object + i.e. with temporal and spatial existence + (time interval and bounding polygon for positions (e.g. rectangle)''' + + def __init__(self, timeInterval = TimeInterval(), boundingPolygon = None, num = None): + self.timeInterval = timeInterval + self.boundingPolygon = boundingPolygon + self.num = num + + def empty(self): + return self.timeInterval.empty() + + def getFirstInstant(self): + return self.timeInterval.first() + + def getLastInstant(self): + return self.timeInterval.first() + + + +# return bounding box, and generic polygon, that defaults to box + +class Track(STObject): + '''Class for trajectories + i.e. sequence of positions''' + pass + + + +if __name__ == "__main__": + import doctest + import unittest + #suite = doctest.DocFileSuite('tests/ubc_utils.txt') + suite = doctest.DocTestSuite() + unittest.TextTestRunner().run(suite) + #doctest.testmod() + #doctest.testfile("example.txt")
--- a/python/utils.py Sun Oct 18 22:32:46 2009 -0400 +++ b/python/utils.py Mon Oct 19 13:16:21 2009 -0400 @@ -59,5 +59,8 @@ if __name__ == "__main__": import doctest - doctest.testmod() + import unittest + suite = doctest.DocFileSuite('tests/ubc_utils.txt') + unittest.TextTestRunner().run(suite) + #doctest.testmod() #doctest.testfile("example.txt")