Mercurial Hosting > traffic-intelligence
diff python/utils.py @ 372:349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
In general, shapely should be used, especially for lots of points:
from shapely.geometry import Polygon, Point
poly = Polygon(array([[0,0],[0,1],[1,1],[1,0]]))
p = Point(0.5,0.5)
poly.contains(p) -> returns True
poly.contains(Point(-1,-1)) -> returns False
You can convert a moving.Point to a shapely point: p = moving.Point(1,2) p.asShapely() returns the equivalent shapely point
If you have several points to test, use moving.pointsInPolygon(points, polygon) where points are moving.Point and polygon is a shapely polygon.
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 16 Jul 2013 17:00:17 -0400 |
parents | 924e38c9f70e |
children | d0b86ed50f32 |
line wrap: on
line diff
--- a/python/utils.py Tue Jul 16 01:36:50 2013 -0400 +++ b/python/utils.py Tue Jul 16 17:00:17 2013 -0400 @@ -218,13 +218,14 @@ self.lengthFunc = lengthFunc self.alignmentShift = 0 - def similarities(self, l1, l2): + def similarities(self, l1, l2, shift=0): from numpy import zeros, int as npint n1 = len(l1) n2 = len(l2) self.similarityTable = zeros((n1+1,n2+1), dtype = npint) for i in xrange(1,n1+1): - for j in xrange(max(1,i-self.delta),min(n2+1,i+self.delta+1)): + for j in xrange(max(1,i-shift-self.delta),min(n2+1,i-shift+self.delta+1)): + #print max(1,i-shift-self.delta),min(n2+1,i-shift+self.delta+1) if self.similarityFunc(l1[i-1], l2[j-1]): self.similarityTable[i,j] = self.similarityTable[i-1,j-1]+1 else: @@ -249,51 +250,61 @@ eg distance(p1, p2) < epsilon ''' - from numpy import argmax + #from numpy import argmax self.similarities(l1, l2) - self.similarityTable = self.similarityTable[:, :min(len(l2), len(l1)+self.delta)+1] + #self.similarityTable = self.similarityTable[:, :min(len(l2), len(l1)+self.delta)+1] if computeSubSequence: self.subSequenceIndices = self.subSequence(len(l1), len(l2)) - return self.similarityTable[-1,-1] + return self.similarityTable.max() def _compute(self, _l1, _l2, computeSubSequence = False): '''returns the best matching if using a finite delta by shiftinig the series alignments''' + if len(_l2) < len(_l1): # l1 is the shortest + l1 = _l2 + l2 = _l1 + revertIndices = True + else: + l1 = _l1 + l2 = _l2 + revertIndices = False + n1 = len(l1) + n2 = len(l2) + if self.aligned: - from numpy import argmax - if len(_l2) < len(_l1): # l1 is the shortest - l1 = _l2 - l2 = _l1 - revertIndices = True - else: - l1 = _l1 - l2 = _l2 - revertIndices = False - n1 = len(l1) - n2 = len(l2) # for i in xrange(min(delta,n1), max(n1+n2-delta, n2+1)): # i is the alignment of the end of l1 in l2 # print l1[min(-i-1,n1):] # min(n1+n2-i,n1) # print l2[max(0,i-n1):] # print LCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):], similarityFunc, delta) lcssValues = {} similarityTables = {} - for i in xrange(min(self.delta,n1), max(n1+n2-self.delta, n2+1)): + #for i in xrange(min(self.delta,n1), max(n1+n2-self.delta, n2+1)): + for i in xrange(-max(n1,n2)-self.delta, +max(n1,n2)+self.delta): #print l1[min(-i-1,n1):] # min(n1+n2-i,n1) #print l2[max(0,i-n1):] - lcssValues[i] = self.computeLCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):]) + #lcssValues[i] = self.computeLCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):]) #print i, lcssValues[i] + lcssValues[i] = self.similarities(l1, l2, i) similarityTables[i] = self.similarityTable - imax = argMaxDict(lcssValues) - self.similarityTable = similarityTables[imax] - self.subSequenceIndices = self.subSequence(self.similarityTable.shape[0]-1, self.similarityTable.shape[1]-1) + #print i + print self.similarityTable + self.alignmentShift = argMaxDict(lcssValues) + self.similarityTable = similarityTables[self.alignmentShift] + # do the subsequence computation here, once similarityTable is set + #self.subSequenceIndices = self.subSequence(self.similarityTable.shape[0]-1, self.similarityTable.shape[1]-1) + #lcss = lcssValues[imax] + else: + self.alignmentShift = 0 + self.similarities(l1, l2) + self.similarityTable = self.similarityTable[:, :min(len(l2), len(l1)+self.delta)+1] + if computeSubSequence: + self.subSequenceIndices = self.subSequence(self.similarityTable.shape[0]-1, self.similarityTable.shape[1]-1) if revertIndices: - self.subSequenceIndices = [(j+imax-n1,i) for i,j in self.subSequenceIndices] - self.alignmentShift = imax-n1 + self.subSequenceIndices = [(j+self.alignmentShift,i) for i,j in self.subSequenceIndices] + #self.alignmentShift = imax-n1 else: - self.subSequenceIndices = [(i+n1-imax,j) for i,j in self.subSequenceIndices] - self.alignmentShift = n1-imax - return lcssValues[imax] - else: - return self.computeLCSS(_l1, _l2, computeSubSequence) + self.subSequenceIndices = [(i+self.alignmentShift,j) for i,j in self.subSequenceIndices] + #self.alignmentShift = n1-imax + return self.similarityTable[-1,-1] def compute(self, l1, l2, computeSubSequence = False): '''get methods are to be shadowed in child classes '''