diff --git a/rowers/interactiveplots.py b/rowers/interactiveplots.py index faa5c3c0..aa884ea1 100644 --- a/rowers/interactiveplots.py +++ b/rowers/interactiveplots.py @@ -2049,21 +2049,70 @@ def leaflet_chart(lat,lon,name=""): return script,div -def leaflet_chart_compare(workoutids,labeldict={},startenddict={}): +def leaflet_chart_compare(course,workoutids,labeldict={},startenddict={}): data = [] for id in workoutids: w = Workout.objects.get(id=id) rowdata = rdata(w.csvfilename) + time = rowdata.df['TimeStamp (sec)'] df = pd.DataFrame({ - 'id':id, + 'workoutid':id, 'lat':rowdata.df[' latitude'], - 'lon':rowdata.df[' longitude'] + 'lon':rowdata.df[' longitude'], + 'time':time-time[0], }) - data.append(f) + data.append(df) df = pd.concat(data,axis=0) + latmean,lonmean,coordinates = course_coord_center(course) + lat_min, lat_max, long_min, long_max = course_coord_maxmin(course) + + coordinates = course_spline(coordinates) + + + + polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course") + + plabels = '' + + for p in polygons: + coords = polygon_coord_center(p) + + plabels += """ + var marker = L.marker([{latbegin}, {longbegin}]).addTo(mymap); + marker.bindPopup("{name}"); + + """.format( + latbegin = coords[0], + longbegin = coords[1], + name = p.name + ) + + pcoordinates = """[ + """ + + for p in polygons: + pcoordinates += """[ + [""" + + points = GeoPoint.objects.filter(polygon=p).order_by("order_in_poly") + + for pt in points: + pcoordinates += "[{x},{y}],".format( + x = pt.latitude, + y = pt.longitude + ) + + # remove last comma + pcoordinates = pcoordinates[:-1] + pcoordinates += """] + ], + """ + + pcoordinates += """ + ]""" # Throw out 0,0 @@ -2071,25 +2120,26 @@ def leaflet_chart_compare(workoutids,labeldict={},startenddict={}): df = df.loc[(df!=0).any(axis=1)] df.fillna(method='bfill',axis=0,inplace=True) df.fillna(method='ffill',axis=0,inplace=True) + + + lat = df['lat'] lon = df['lon'] if lat.empty or lon.empty: return [0,"invalid coordinate data"] + latbegin = lat.values[0] + longbegin = lon.values[0] + latend = lat.values[-1] + longend = lon.values[-1] + + colors = itertools.cycle(palette) + try: + items = itertools.izip(workoutids,colors) + except AttributeError: + items = zip(workoutids,colors) - coordinates = zip(lat,lon) - - scoordinates = "[" - - for x,y in coordinates: - scoordinates += """[{x},{y}], - """.format( - x=x, - y=y - ) - - scoordinates += "]" script = """ """.format( latmean=latmean, lonmean=lonmean, @@ -2179,9 +2225,69 @@ def leaflet_chart_compare(workoutids,labeldict={},startenddict={}): latend=latend, longbegin=longbegin, longend=longend, - scoordinates=scoordinates, + pcoordinates=pcoordinates, + plabels=plabels, ) + for id,color in items: + group = df[df['workoutid']==int(id)].copy() + try: + startsecond,endsecond = startenddict[id] + except KeyError: + startsecond = 0 + endsecond = 0 + + try: + label = labeldict[id] + except KeyError: + label = str(id) + + group.sort_values(by='time',ascending=True,inplace=True) + group.dropna(axis=0,how='any',inplace=True) + if endsecond > 0: + group['time'] = group['time'] - startsecond + mask = group['time'] < 0 + group.mask(mask,inplace=True) + mask = group['time'] > (endsecond-startsecond) + group.mask(mask,inplace=True) + + lat = group['lat'].dropna() + lon = group['lon'].dropna() + + coordinates = zip(lat,lon) + + scoordinates = "[" + for x,y in coordinates: + scoordinates += """[{x},{y}], + """.format(x=x,y=y) + scoordinates += "]" + + script += """ + var latlongs = {scoordinates} + var polyline = L.polyline(latlongs, {{color:'{color}'}}).addTo(mymap) + polyline.bindPopup("{label}",{{ autoPan: false, autoClose: false }}).openPopup() + polyline.on('mouseover',function(ev) {{ + ev.target.openPopup(); + }}); + polyline.on('dblclick', function (e) {{ + mymap.removeLayer(this); + }}); + mymap.fitBounds(polyline.getBounds()) + """.format( + scoordinates=scoordinates, + color=color, + label=label, + id=id, + ) + + script += """ + + + """ + + + + div = """
Click on a line to see the label. Double click on a line to remove it. Reload to get back all lines.
+We show your original GPS coordinates on the course. For course time, we use a finer interpolation, to get + the exact time when you crossed the line.
- -Compare Results + {% if race.sessiontype == 'race' %} + Compare Course + {% endif %}
{% if race.manager == request.user %} \b[0-9A-Fa-f]+\b)/wind/$',views.workout_wind_view,name='workout_wind_view'), re_path(r'^workout/(?P