Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 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 |
comparison
equal
deleted
inserted
replaced
91:daa05fae1a70 | 92:a5ef9e40688e |
---|---|
141 | 141 |
142 def inPolygon(self, poly): | 142 def inPolygon(self, poly): |
143 '''Returns if the point x, y is inside the polygon. | 143 '''Returns if the point x, y is inside the polygon. |
144 The polygon is defined by the ordered list of points in poly | 144 The polygon is defined by the ordered list of points in poly |
145 | 145 |
146 taken from http://www.ariel.com.au/a/python-point-int-poly.html''' | 146 taken from http://www.ariel.com.au/a/python-point-int-poly.html |
147 | |
148 Use points_inside_poly from matplotlib.nxutils''' | |
147 | 149 |
148 n = len(poly); | 150 n = len(poly); |
149 counter = 0; | 151 counter = 0; |
150 | 152 |
151 p1 = poly[0]; | 153 p1 = poly[0]; |
235 def getXCoordinates(self): | 237 def getXCoordinates(self): |
236 return self.positions[0] | 238 return self.positions[0] |
237 | 239 |
238 def getYCoordinates(self): | 240 def getYCoordinates(self): |
239 return self.positions[1] | 241 return self.positions[1] |
242 | |
243 def asArray(self): | |
244 from numpy.core.multiarray import array | |
245 return array(self.positions) | |
240 | 246 |
241 def xBounds(self): | 247 def xBounds(self): |
242 # look for function that does min and max in one pass | 248 # look for function that does min and max in one pass |
243 return [min(self.getXCoordinates()), max(self.getXCoordinates())] | 249 return [min(self.getXCoordinates()), max(self.getXCoordinates())] |
244 | 250 |
306 return Trajectory([self.positions[0][inter.first:inter.last], | 312 return Trajectory([self.positions[0][inter.first:inter.last], |
307 self.positions[1][inter.first:inter.last]]) | 313 self.positions[1][inter.first:inter.last]]) |
308 else: | 314 else: |
309 return None | 315 return None |
310 | 316 |
311 def getTrajectoryInPolygon1(self, polygon): | 317 def getTrajectoryInPolygon(self, polygon): |
312 'Returns the set of points inside the polygon' | 318 '''Returns the set of points inside the polygon |
319 (array of Nx2 coordinates of the polygon vertices)''' | |
320 import matplotlib.nxutils as nx | |
313 t = Trajectory() | 321 t = Trajectory() |
322 result = nx.points_inside_poly(self.asArray().T, polygon) | |
314 for i in xrange(self.length()): | 323 for i in xrange(self.length()): |
315 p = self.__getitem__(i) | 324 if result[i]: |
316 if p.inPolygon(polygon): | 325 t.addPositionXY(self.positions[0][i], self.positions[1][i]) |
317 t.addPosition(p) | |
318 if t.length()>0: | 326 if t.length()>0: |
319 return t | 327 return t |
320 else: | 328 else: |
321 return None | 329 return None |
322 | 330 |