more precise course length
This commit is contained in:
@@ -28,7 +28,8 @@ from rowers.models import (
|
||||
Rower, Workout,
|
||||
GeoPoint,GeoPolygon, GeoCourse,
|
||||
course_length,course_coord_center,course_coord_maxmin,
|
||||
polygon_coord_center,PlannedSession
|
||||
polygon_coord_center,PlannedSession,
|
||||
polygon_to_path,coordinate_in_path
|
||||
)
|
||||
|
||||
from utils import geo_distance
|
||||
@@ -41,56 +42,6 @@ class InvalidTrajectoryError(Exception):
|
||||
def __str__(self):
|
||||
return repr(self.value)
|
||||
|
||||
def get_dir_vector(polygon1,polygon2):
|
||||
lat1,lon1 = polygon_coord_center(polygon1)
|
||||
lat2,lon2 = polygon_coord_center(polygon2)
|
||||
|
||||
return [lat2-lat1,lon2-lon1]
|
||||
|
||||
def get_delta(vector,polygon):
|
||||
x = pd.Series(range(10000))/9999.
|
||||
vlat = vector[0]
|
||||
vlon = vector[1]
|
||||
|
||||
lat1,lon1 = polygon_coord_center(polygon)
|
||||
|
||||
lat = x.apply(lambda x:lat1+x*vlat)
|
||||
lon = x.apply(lambda x:lon1+x*vlon)
|
||||
|
||||
totdist,bearing = geo_distance(lat1,lon1,lat1+vlat,lon1+vlat)
|
||||
print totdist
|
||||
|
||||
dist = x*totdist
|
||||
|
||||
p = polygon_to_path(polygon)
|
||||
|
||||
f = lambda x: coordinate_in_path(x['lat'],x['lon'],p)
|
||||
|
||||
df = pd.DataFrame({'x':x,
|
||||
'lat':lat,
|
||||
'lon':lon,
|
||||
'dist':dist,
|
||||
})
|
||||
|
||||
df['inpolygon'] = df.apply(f,axis=1)
|
||||
|
||||
|
||||
b = (~df['inpolygon']).shift(-1)+df['inpolygon']
|
||||
|
||||
|
||||
if len(df[b==2]):
|
||||
return 1.0e3*df[b==2]['dist'].min()
|
||||
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def get_delta_start(course):
|
||||
polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course")
|
||||
vector = get_dir_vector(polygons[0],polygons[1])
|
||||
delta = get_delta(vector,polygons[0])
|
||||
|
||||
return delta
|
||||
|
||||
def get_course_timezone(course):
|
||||
polygons = GeoPolygon.objects.filter(course = course)
|
||||
@@ -111,19 +62,6 @@ def get_course_timezone(course):
|
||||
|
||||
return timezone_str
|
||||
|
||||
def polygon_to_path(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(s[:-1])
|
||||
|
||||
return p
|
||||
|
||||
def coordinate_in_path(latitude,longitude, p):
|
||||
|
||||
return p.contains_points([(latitude,longitude)])[0]
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ from collections import OrderedDict
|
||||
from timezonefinder import TimezoneFinder
|
||||
|
||||
import types
|
||||
|
||||
from matplotlib import path
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
TWEET_ACCESS_TOKEN_KEY,
|
||||
@@ -363,6 +363,19 @@ def polygon_coord_center(polygon):
|
||||
|
||||
|
||||
|
||||
def polygon_to_path(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(s[:-1])
|
||||
|
||||
return p
|
||||
|
||||
def coordinate_in_path(latitude,longitude, p):
|
||||
|
||||
return p.contains_points([(latitude,longitude)])[0]
|
||||
|
||||
|
||||
def course_coord_center(course):
|
||||
@@ -407,6 +420,62 @@ def course_coord_maxmin(course):
|
||||
|
||||
return lat_min,lat_max,long_min,long_max
|
||||
|
||||
def get_dir_vector(polygon1,polygon2):
|
||||
lat1,lon1 = polygon_coord_center(polygon1)
|
||||
lat2,lon2 = polygon_coord_center(polygon2)
|
||||
|
||||
return [lat2-lat1,lon2-lon1]
|
||||
|
||||
def get_delta(vector,polygon):
|
||||
x = pd.Series(range(10000))/9999.
|
||||
vlat = vector[0]
|
||||
vlon = vector[1]
|
||||
|
||||
lat1,lon1 = polygon_coord_center(polygon)
|
||||
|
||||
lat = x.apply(lambda x:lat1+x*vlat)
|
||||
lon = x.apply(lambda x:lon1+x*vlon)
|
||||
|
||||
totdist,bearing = geo_distance(lat1,lon1,lat1+vlat,lon1+vlon)
|
||||
|
||||
dist = x*totdist
|
||||
|
||||
p = polygon_to_path(polygon)
|
||||
|
||||
f = lambda x: coordinate_in_path(x['lat'],x['lon'],p)
|
||||
|
||||
df = pd.DataFrame({'x':x,
|
||||
'lat':lat,
|
||||
'lon':lon,
|
||||
'dist':dist,
|
||||
})
|
||||
|
||||
df['inpolygon'] = df.apply(f,axis=1)
|
||||
|
||||
|
||||
b = (~df['inpolygon']).shift(-1)+df['inpolygon']
|
||||
|
||||
|
||||
if len(df[b==2]):
|
||||
return 1.0e3*df[b==2]['dist'].min()
|
||||
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def get_delta_start(course):
|
||||
polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course")
|
||||
vector = get_dir_vector(polygons[0],polygons[1])
|
||||
delta = get_delta(vector,polygons[0])
|
||||
|
||||
return delta
|
||||
|
||||
def get_delta_finish(course):
|
||||
polygons = GeoPolygon.objects.filter(course=course).order_by("-order_in_course")
|
||||
vector = get_dir_vector(polygons[0],polygons[1])
|
||||
delta = get_delta(vector,polygons[0])
|
||||
|
||||
return delta
|
||||
|
||||
def course_length(course):
|
||||
polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course")
|
||||
@@ -421,7 +490,14 @@ def course_length(course):
|
||||
|
||||
totaldist += 1000.*dist[0]
|
||||
|
||||
return int(totaldist)
|
||||
vector = get_dir_vector(polygons[0],polygons[1])
|
||||
deltastart = get_delta(vector,polygons[0])
|
||||
|
||||
polygons = polygons.reverse()
|
||||
vector = get_dir_vector(polygons[0],polygons[1])
|
||||
deltafinish = get_delta(vector,polygons[0])
|
||||
|
||||
return int(totaldist-deltastart-deltafinish)
|
||||
|
||||
sexcategories = (
|
||||
('male','male'),
|
||||
|
||||
Reference in New Issue
Block a user