Private
Public Access
1
0

course time not working yet

This commit is contained in:
Sander Roosendaal
2018-02-21 21:17:17 +01:00
parent 27d7e7774c
commit 8a679f1626

View File

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