Private
Public Access
1
0

course check working but gives wrong result

This commit is contained in:
Sander Roosendaal
2018-02-21 18:32:18 +01:00
parent 0c00fb164f
commit 27d7e7774c

View File

@@ -14,6 +14,7 @@ from matplotlib import path
import xml.etree.ElementTree as et
import pandas as pd
import numpy as np
import dataprep
@@ -85,12 +86,12 @@ def course_coord_maxmin(course):
return lat_min,lat_max,long_min,long_max
def polygon_to_path(polygon):
points = GeoPoint.objects.filter(polygon==polygon).order_by("order_in_polygon")
points = GeoPoint.objects.filter(polygon=polygon).order_by("order_in_poly")
s = []
for point in points:
s.append([point.latitude,point.longitude])
p = path.Path(np.array(s))
p = path.Path(s[:-1])
return p
@@ -102,6 +103,10 @@ def coordinate_in_polygon(latitude,longitude, polygon):
def time_in_polygon(df,polygon,maxmin='max'):
if df.empty:
return 0
# df has timestamp, latitude, longitude
p = polygon_to_path(polygon)
@@ -112,10 +117,13 @@ def time_in_polygon(df,polygon,maxmin='max'):
df['inpolygon'] = df.apply(f,axis=1)
mask = df['inpolygon'] == True
if df[mask].empty():
raise InvalidTrajectoryError
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()
@@ -237,6 +245,7 @@ def coursetime_polygons(data,polygons):
try:
entrytime = time_in_polygon(data,polygons[0],maxmin='min')
coursecompleted = True
print entrytime,polygons[0].name
except InvalidTrajectoryError:
entrytime = data['time'].max()
coursecompleted = False
@@ -245,6 +254,14 @@ def coursetime_polygons(data,polygons):
if len(polygons) > 1:
try:
time = time_in_polygon(data, polygons[0])
data = data[data['time']>time]
data['time'] = data['time']-time
print time,polygons[0].name
timenext, coursecompleted = coursetime_polygons(data,polygons[1:])
return time+timenext, coursecompleted
except InvalidTrajectoryError:
entrytime = data['time'].max()
coursecompleted = False
return entrytime, coursecompleted
@@ -253,16 +270,27 @@ def get_time_course(ws,course):
coursecompleted = 0
w = ws[0]
columns = ['time','latitude','longitude']
columns = ['time',' latitude',' longitude']
rowdata = dataprep.getsmallrowdata_db(
columns,
ids = [w.id],
doclean=True,
workstrokesonly=False
)
rowdata.rename(columns = {
' latitude':'latitude',
' longitude':'longitude',
}, inplace=True)
rowdata['time'] = rowdata['time']/1000.
rowdata.fillna(method='backfill',inplace=True)
rowdata['time'] = rowdata['time']-rowdata.ix[0,'time']
# we may want to expand the time (interpolate)
polygons = GeoPolygon.object.filter(course=course)
polygons = GeoPolygon.objects.filter(course=course)
coursetimeseconds,coursecompleted = coursetime_polygons(rowdata,polygons)