changeset 113:606010d1d9a4

corrected errors in trajectories (if empty) and getTrajectoryInPolygon
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 18 Jul 2011 14:29:07 -0400
parents 67555e968b5e
children 680d4c82886d
files python/moving.py python/tests/moving.txt
diffstat 2 files changed, 13 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Mon Jul 18 14:28:40 2011 -0400
+++ b/python/moving.py	Mon Jul 18 14:29:07 2011 -0400
@@ -206,7 +206,10 @@
     the class is iterable.'''
 
     def __init__(self, positions=None):
-        self.positions = positions
+        if positions:
+            self.positions = positions
+        else:
+            self.positions = [[],[]]
 
     @staticmethod
     def load(line1, line2):
@@ -234,11 +237,8 @@
             return self[self.iterInstantNum-1]
 
     def addPositionXY(self, x, y):
-        if not self.positions:
-            self.positions = [[x],[y]]
-        else:
-            self.positions[0].append(x)
-            self.positions[1].append(y)
+        self.positions[0].append(x)
+        self.positions[1].append(y)
 
     def addPosition(self, p):
         self.addPositionXY(p.x, p.y)
@@ -334,15 +334,12 @@
         '''Returns the set of points inside the polygon
         (array of Nx2 coordinates of the polygon vertices)'''
         import matplotlib.nxutils as nx
-        t = Trajectory()
+        traj = Trajectory()
         result = nx.points_inside_poly(self.asArray().T, polygon)
         for i in xrange(self.length()):
             if result[i]:
-                t.addPositionXY(self.positions[0][i], self.positions[1][i])
-        if t.length()>0:
-            return t
-        else:
-            return None
+                traj.addPositionXY(self.positions[0][i], self.positions[1][i])
+        return traj
 
     # version 2: use shapely polygon contains
 
--- a/python/tests/moving.txt	Mon Jul 18 14:28:40 2011 -0400
+++ b/python/tests/moving.txt	Mon Jul 18 14:29:07 2011 -0400
@@ -43,6 +43,8 @@
 >>> Point(3,2).inPolygon([Point(0,0),Point(4,0),Point(4,3),Point(0,3)])
 True
 
+>>> Trajectory().length()
+0
 >>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]])
 >>> t1.length() == 3.
 True
@@ -50,6 +52,8 @@
 (1.500000,3.500000)
 >>> t1.getTrajectoryInPolygon(np.array([[0,0],[4,0],[4,3],[0,3]]))
 (0.500000,0.500000)
+>>> t1.getTrajectoryInPolygon(np.array([[10,10],[14,10],[14,13],[10,13]])).length()
+0
 
 >>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6))
 >>> indic1.empty()