Mercurial Hosting > traffic-intelligence
view python/poly-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 | 1d90e9080cb2 |
children | 5e09583275a4 |
line wrap: on
line source
#! /usr/bin/env python '''Various utilities to load data saved by the POLY new output(s)''' import sys import utils from moving import TimeInterval import numpy as np __metaclass__ = type # inputs variables #dirname= 'G:/0-phdstart/Code/trial/indicatorsNew/' #extension= '-indicatorsNew.csv' #indicatorsNames= {1:'Distance',2:'Cosine',3:'collision Course Angle',4:'Velocity Cosine',5:'Velocity Angle',6:'Speed Differential',7:'Collision Probability',8:'Severity Index',9:'TTC'} ''' min Distance case''' dirname= 'G:/0-phdstart/Code/trial/minDistanceIndicator/' extension= '-minDistanceInd.csv' indicatorsNames= {1:'minDistance'} def loadNewInteractions(videoFilename,interactionType, roaduserNum1,roaduserNum2, selectedIndicators=[]): '''Loads interactions from the POLY traffic event format''' from events import Interaction from indicators import SeverityIndicator #filename= dirname + videoFilename + extension filename= dirname + interactionType+ '-' + videoFilename + extension # case of min distance todo: change the saving format to be matched with all outputs file = utils.openCheck(filename) if (not file): return [] interactions = [] interactionNum = 0 data= np.loadtxt(filename) indicatorFrameNums= data[:,0] inter = Interaction(interactionNum, TimeInterval(indicatorFrameNums[0],indicatorFrameNums[-1]), roaduserNum1, roaduserNum2) inter.addVideoFilename(videoFilename) inter.addInteractionType(interactionType) for key in indicatorsNames.keys(): values= {} for i,t in enumerate(indicatorFrameNums): values[t] = data[i,key] inter.addIndicator(SeverityIndicator(indicatorsNames[key], values)) if selectedIndicators !=[]: values= {} for i,t in enumerate(indicatorFrameNums): values[t] = [data[i,index] for index in selectedIndicators] inter.addIndicator(SeverityIndicator('selectedIndicators', values)) interactions.append(inter) file.close() return interactions