diff --git a/rowers/courses.py b/rowers/courses.py index bd81ffb3..ab8bc77b 100644 --- a/rowers/courses.py +++ b/rowers/courses.py @@ -95,43 +95,36 @@ def polygon_to_path(polygon): return p -def coordinate_in_polygon(latitude,longitude, polygon): - p = polygon_to_path(polygon) +def coordinate_in_path(latitude,longitude, p): return p.contains_points([(latitude,longitude)])[0] -def time_in_polygon(df,polygon,maxmin='max'): +def time_in_path(df,p,maxmin='max'): if df.empty: return 0 - # df has timestamp, latitude, longitude - p = polygon_to_path(polygon) - latitude = df.latitude longitude = df.longitude - f = lambda x: coordinate_in_polygon(x['latitude'],x['longitude'],polygon) + f = lambda x: coordinate_in_path(x['latitude'],x['longitude'],p) df['inpolygon'] = df.apply(f,axis=1) - - mask = df['inpolygon'] == True - - - if df[mask].empty: - print polygon.name - raise InvalidTrajectoryError("Trajectory doesn't go through {name}".format(name=polygon.name)) - - if maxmin == 'max': - time = df[mask]['time'].max() + if maxmin=='max': + b = (~df['inpolygon']).shift(-1)+df['inpolygon'] else: - time = df[mask]['time'].min() + b = (~df['inpolygon']).shift(1)+df['inpolygon'] + + if len(df[b==2]): + return df[b==2]['time'].min() + + raise InvalidTrajectoryError("Trajectory doesn't go through path") - return time + return 0 def crewnerdcourse(doc): courses = [] @@ -210,12 +203,15 @@ def createcourse( j = 0 for point in p['points']: if i==0 and j==0: - loc = geolocator.reverse((point['latitude'],point['longitude'])) - country = loc.raw['address']['country'] - if isinstance(country,unicode): - country = country.encode('utf8') - elif isinstance(country, str): - country = country.decode('utf8') + try: + loc = geolocator.reverse((point['latitude'],point['longitude'])) + country = loc.raw['address']['country'] + if isinstance(country,unicode): + country = country.encode('utf8') + elif isinstance(country, str): + country = country.decode('utf8') + except GeocoderInsufficientPrivileges: + country = "Unknown Country" c.country = country c.save() @@ -231,33 +227,33 @@ def createcourse( return c -def coursetime_polygons(data,polygons): +def coursetime_paths(data,paths): entrytime = data['time'].max() coursecompleted = False - # corner case - empty list of polygons - if len(polygons) == 0: + # corner case - empty list of paths + if len(paths) == 0: return 0,True # end - just the Finish polygon - if len(polygons) == 1: + if len(paths) == 1: try: - entrytime = time_in_polygon(data,polygons[0],maxmin='min') + entrytime = time_in_path(data,paths[0],maxmin='min') coursecompleted = True - print entrytime,polygons[0].name + print entrytime except InvalidTrajectoryError: entrytime = data['time'].max() coursecompleted = False return entrytime,coursecompleted - if len(polygons) > 1: + if len(paths) > 1: try: - time = time_in_polygon(data, polygons[0]) + time = time_in_path(data, paths[0]) data = data[data['time']>time] data['time'] = data['time']-time - print time,polygons[0].name - timenext, coursecompleted = coursetime_polygons(data,polygons[1:]) + print time + timenext, coursecompleted = coursetime_paths(data,paths[1:]) return time+timenext, coursecompleted except InvalidTrajectoryError: entrytime = data['time'].max() @@ -291,7 +287,13 @@ def get_time_course(ws,course): # we may want to expand the time (interpolate) polygons = GeoPolygon.objects.filter(course=course) + paths = [] + for polygon in polygons: + path = polygon_to_path(polygon) + paths.append(path) - coursetimeseconds,coursecompleted = coursetime_polygons(rowdata,polygons) + coursetimeseconds,coursecompleted = coursetime_paths(rowdata,paths) + + print 'course time?',coursetimeseconds return coursetimeseconds,coursecompleted