view python/moving.py @ 19:5a21d2cfee44

added polygon plotting
author Nicolas Saunier <nico@confins.net>
date Fri, 27 Nov 2009 19:16:12 -0500
parents 9d6831cfe675
children 3c4629550f5f
line wrap: on
line source

#! /usr/bin/env python
'''Libraries for moving objects, trajectories...'''

import utils;

from math import sqrt, hypot;

from shapely.geometry import Polygon

__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 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 
    (time interval and bounding polygon for positions (e.g. rectangle)).
    It does not mean that the object is defined 
    for all time instants within the time interval'''

    def __init__(self, num = None, timeInterval = None, boundingPolygon = None):
        self.num = num
        self.timeInterval = timeInterval
        self.boundingPolygon = boundingPolygon

    def empty(self):
        return self.timeInterval.empty() or not self.boudingPolygon

    def getFirstInstant(self):
        return self.timeInterval.first()

    def getLastInstant(self):
        return self.timeInterval.first()

class Trajectory:
    '''Class for trajectories
    i.e. a temporal sequence of positions'''

    def __init__(self, positions = None):
        self.positions = positions

    def __str__(self):
        return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1]))

    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])

    def xBounds(self):
        # look for function that does min and max in one pass
        return [min(self.positions[0]), max(self.positions[0])]
    
    def yBounds(self):
        # look for function that does min and max in one pass
        return [min(self.positions[1]), max(self.positions[1])]

    def norm(self):
        '''Returns the list of the norms at each instant'''
#        def add(x, y): return x+y
#        sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]])
#        return sqrt(sq)
        return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])]

    def getTrajectoryInPolygon(self, polygon):
        'Returns the set of points inside the polygon'
        # use shapely polygon contains
        pass

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, positions = None, geometry = None, type = None):
        STObject.__init__(self, num, timeInterval)
        self.positions = positions
        self.geometry = geometry
        self.type = type
        # compute bounding polygon from trajectory

    def draw(self):
        self.positions.draw()

    # def computeVelocities(self):

# need for a class representing the indicators, their units, how to print them in graphs...
class TemporalIndicator:
    '''Class for temporal indicators
    i.e. indicators that take a value at specific instants'''
    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")