Mercurial Hosting > traffic-intelligence
changeset 92:a5ef9e40688e
makes use of matplotlib function to test if point is in a polygon
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sun, 12 Jun 2011 03:24:55 -0400 |
parents | daa05fae1a70 |
children | 19603b5fa578 |
files | python/moving.py python/tests/moving.txt |
diffstat | 2 files changed, 16 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/python/moving.py Thu Jun 09 16:03:17 2011 -0400 +++ b/python/moving.py Sun Jun 12 03:24:55 2011 -0400 @@ -143,7 +143,9 @@ '''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''' + taken from http://www.ariel.com.au/a/python-point-int-poly.html + + Use points_inside_poly from matplotlib.nxutils''' n = len(poly); counter = 0; @@ -237,6 +239,10 @@ def getYCoordinates(self): return self.positions[1] + + def asArray(self): + from numpy.core.multiarray import array + return array(self.positions) def xBounds(self): # look for function that does min and max in one pass @@ -308,13 +314,15 @@ else: return None - def getTrajectoryInPolygon1(self, polygon): - 'Returns the set of points inside the polygon' + def getTrajectoryInPolygon(self, polygon): + '''Returns the set of points inside the polygon + (array of Nx2 coordinates of the polygon vertices)''' + import matplotlib.nxutils as nx t = Trajectory() + result = nx.points_inside_poly(self.asArray().T, polygon) for i in xrange(self.length()): - p = self.__getitem__(i) - if p.inPolygon(polygon): - t.addPosition(p) + if result[i]: + t.addPositionXY(self.positions[0][i], self.positions[1][i]) if t.length()>0: return t else:
--- a/python/tests/moving.txt Thu Jun 09 16:03:17 2011 -0400 +++ b/python/tests/moving.txt Sun Jun 12 03:24:55 2011 -0400 @@ -1,4 +1,5 @@ >>> from moving import * +>>> import numpy as np >>> Interval().empty() True @@ -35,7 +36,7 @@ True >>> t1[1] (1.500000,3.500000) ->>> t1.getTrajectoryInPolygon1([Point(0,0),Point(4,0),Point(4,3),Point(0,3)]) +>>> t1.getTrajectoryInPolygon(np.array([[0,0],[4,0],[4,3],[0,3]])) (0.500000,0.500000) >>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6))