diff --git a/rowers/courses.py b/rowers/courses.py index 34a7b379..25a23d11 100644 --- a/rowers/courses.py +++ b/rowers/courses.py @@ -14,6 +14,8 @@ from utils import myqueue from matplotlib import path import xml.etree.ElementTree as et +import pandas as pd + ns = {'opengis': 'http://www.opengis.net/kml/2.2'} import django_rq @@ -34,8 +36,59 @@ class InvalidTrajectoryError(Exception): def __str__(self): return repr(self.value) +def polygon_coord_center(polygon): + + points = GeoPoint.objects.filter(polygon=polygon).order_by("order_in_poly") + + latitudes = pd.Series([p.latitude for p in points]) + longitudes = pd.Series([p.longitude for p in points]) + + return latitudes.mean(), longitudes.mean() + +def course_coord_center(course): + + polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course") + + latitudes = [] + longitudes = [] + + for p in polygons: + latitude,longitude = polygon_coord_center(p) + latitudes.append(latitude) + longitudes.append(longitude) + + latitude = pd.Series(latitudes).median() + longitude = pd.Series(longitudes).median() + + coordinates = pd.DataFrame({ + 'latitude':latitudes, + 'longitude':longitudes, + }) + + return latitude,longitude,coordinates + +def course_coord_maxmin(course): + + polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course") + + latitudes = [] + longitudes = [] + + for p in polygons: + latitude,longitude = polygon_coord_center(p) + latitudes.append(latitude) + longitudes.append(longitude) + + lat_min = pd.Series(latitudes).min() + lat_max = pd.Series(latitudes).max() + long_min = pd.Series(longitudes).min() + long_max = pd.Series(longitudes).max() + + + 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_polygon") s = [] for point in points: s.append([point.latitude,point.longitude]) diff --git a/rowers/interactiveplots.py b/rowers/interactiveplots.py index cbe08d1e..9ec4834e 100644 --- a/rowers/interactiveplots.py +++ b/rowers/interactiveplots.py @@ -1,5 +1,8 @@ import colorsys -from rowers.models import Workout, User, Rower, WorkoutForm,RowerForm,GraphImage +from rowers.models import ( + Workout, User, Rower, WorkoutForm,RowerForm, + GraphImage,GeoPolygon,GeoCourse,GeoPoint + ) from rowingdata import rower as rrower from rowingdata import main as rmain from rowingdata import cumcpdata,histodata @@ -35,6 +38,10 @@ from bokeh.core.properties import value from collections import OrderedDict from django.conf import settings +from courses import ( + course_coord_center,course_coord_maxmin, + ) + import datetime import math import numpy as np @@ -707,6 +714,124 @@ def interactive_histoall(theworkouts): script, div = components(plot) return [script,div] +def course_map(course): + latmean,lonmean,coordinates = course_coord_center(course) + lat_min, lat_max, long_min, long_max = course_coord_maxmin(course) + + scoordinates = "[" + + for index,row in coordinates.iterrows(): + scoordinates += """[{x},{y}], + """.format( + x=row['latitude'], + y=row['longitude'] + ) + + scoordinates +="]" + + polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course") + + + 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 += """ + ]""" + + + print pcoordinates + + script = """ + + """.format( + latmean=latmean, + lonmean=lonmean, + scoordinates=scoordinates, + pcoordinates=pcoordinates + ) + + div = """ +