changeset 79:5d487f183fe2

added method to test points in polygons and tests
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 11 Mar 2011 18:12:29 -0500
parents 99e807c29753
children 9a51372607d6
files python/moving.py python/tests/moving.txt
diffstat 2 files changed, 44 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Fri Feb 25 14:25:34 2011 -0500
+++ b/python/moving.py	Fri Mar 11 18:12:29 2011 -0500
@@ -139,6 +139,29 @@
     def aslist(self):
         return [self.x, self.y]
 
+    def inPolygon(self, poly):
+        '''Returns if the point x, y is inside the polygon.
+        The polygon is defined by the ordered list of points in poly
+        
+        taken from http://www.ariel.com.au/a/python-point-int-poly.html'''
+
+        n = len(poly);
+        counter = 0;
+
+        p1 = poly[0];
+        for i in range(n+1):
+            p2 = poly[i % n];
+            if self.y > min(p1.y,p2.y):
+                if self.y <= max(p1.y,p2.y):
+                    if self.x <= max(p1.x,p2.x):
+                        if p1.y != p2.y:
+                            xinters = (self.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
+                        if p1.x == p2.x or self.x <= xinters:
+                            counter+=1;
+            p1=p2
+        return (counter%2 == 1);
+
+
     @staticmethod
     def dot(p1, p2):
         return p1.x*p2.x+p1.y*p2.y
@@ -158,7 +181,7 @@
 
     the class is iterable.'''
 
-    def __init__(self, positions):
+    def __init__(self, positions=None):
         self.positions = positions
 
     @staticmethod
@@ -194,7 +217,7 @@
             self.positions[1].append(y)
 
     def addPosition(self, p):
-        self.addPosition(p.x, p.y)
+        self.addPositionXY(p.x, p.y)
         
     def draw(self, options = ''):
         from matplotlib.pylab import plot
@@ -258,10 +281,19 @@
         else:
             return None
     
-    def getTrajectoryInPolygon(self, polygon):
+    def getTrajectoryInPolygon1(self, polygon):
         'Returns the set of points inside the polygon'
-        # use shapely polygon contains
-        pass
+        t = Trajectory()
+        for i in xrange(self.length()):
+            p = self.__getitem__(i)
+            if p.inPolygon(polygon):
+                t.addPosition(p)
+        if t.length()>0:
+            return t
+        else:
+            return None
+
+    # version 2: use shapely polygon contains
 
 ##################
 # Moving Objects
--- a/python/tests/moving.txt	Fri Feb 25 14:25:34 2011 -0500
+++ b/python/tests/moving.txt	Fri Mar 11 18:12:29 2011 -0500
@@ -25,11 +25,18 @@
 >>> Point.distanceNorm2(Point(3,4),Point(1,7))
 3.6055512754639891
 
+>>> Point(3,2).inPolygon([Point(0,0),Point(1,0),Point(1,1),Point(0,1)])
+False
+>>> Point(3,2).inPolygon([Point(0,0),Point(4,0),Point(4,3),Point(0,3)])
+True
+
 >>> t1 = Trajectory([[0.5,1.5,2.5],[0.5,3.5,6.5]])
 >>> t1.length()
 3
 >>> t1[1]
 (1.500000,3.500000)
+>>> t1.getTrajectoryInPolygon1([Point(0,0),Point(4,0),Point(4,3),Point(0,3)])
+(0.500000,0.500000)
 
 >>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6))
 >>> indic1.empty()