point towards flock centroid
This commit is contained in:
parent
904aa582cc
commit
6717f07a49
25
model.py
25
model.py
@ -18,9 +18,11 @@ class Flock:
|
||||
lats = list()
|
||||
lons = list()
|
||||
speeds = list()
|
||||
bearings = list()
|
||||
n = 0
|
||||
for b in bikes:
|
||||
speeds.append(b['speed'])
|
||||
bearings.append(b.get('bearing', 0))
|
||||
lats.append(float(b['point'][1]))
|
||||
lons.append(float(b['point'][0]))
|
||||
n += 1
|
||||
@ -40,6 +42,7 @@ class Bike(Document):
|
||||
point = GeoPointField()
|
||||
destination = GeoPointField(required=True)
|
||||
speed = FloatField(default=0)
|
||||
bearing = FloatField(default=0)
|
||||
last_update = DateTimeField(default=datetime.datetime.utcnow)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@ -52,9 +55,16 @@ class Bike(Document):
|
||||
self.bearing = bearing
|
||||
self.save()
|
||||
|
||||
def heading_to(self, point):
|
||||
s = LatLon(Latitude(self.point[1]), Longitude(self.point[0]))
|
||||
t = LatLon(Latitude(point[1]), Longitude(point[0]))
|
||||
def heading_to(self, target):
|
||||
s = LatLon(Latitude(self.point[1]),
|
||||
Longitude(self.point[0]))
|
||||
if type(target) is list:
|
||||
t = LatLon(Latitude(target[0]),
|
||||
Longitude(target[1]))
|
||||
else:
|
||||
t = target
|
||||
|
||||
print(s, t)
|
||||
return s.heading_initial(t)
|
||||
|
||||
def distance_to(self, point):
|
||||
@ -62,7 +72,6 @@ class Bike(Document):
|
||||
(point[1],
|
||||
point[0])).meters
|
||||
|
||||
|
||||
def get_point(self):
|
||||
return (self.point[1],
|
||||
self.point[0])
|
||||
@ -71,7 +80,6 @@ class Bike(Document):
|
||||
return (self.destination[1],
|
||||
self.destination[0])
|
||||
|
||||
|
||||
def find_flock(self, point_altruism=0.1, dest_altruism=0.2):
|
||||
"""
|
||||
:return: list of Bike objects
|
||||
@ -82,7 +90,6 @@ class Bike(Document):
|
||||
local_radius = trip_len * point_altruism
|
||||
destination_radius = trip_len * dest_altruism
|
||||
|
||||
|
||||
# TODO: filter by timestamp
|
||||
|
||||
# these are bikes around me
|
||||
@ -116,8 +123,10 @@ class Bike(Document):
|
||||
"""
|
||||
flock = Flock(self.find_flock())
|
||||
if flock.size > 1: # no flocking with self!
|
||||
flock_heading: self.heading_to(flock.centroid)
|
||||
flock_distance: distance.geodesic(self.point, self.destination).meters
|
||||
flock_heading = self.heading_to(flock.centroid)
|
||||
flock_distance = distance.geodesic((self.point[1],
|
||||
self.point[0]), (self.destination[1],
|
||||
self.destination[0])).meters
|
||||
return {'flock_heading': flock_heading,
|
||||
'flock_distance': flock_distance,
|
||||
'flock_avg_speed': flock.mean_speed,
|
||||
|
||||
@ -37,10 +37,12 @@ def update(bike_id):
|
||||
speed = float(request.args.get('speed', None))
|
||||
bearing = float(request.args.get('bearing', None))
|
||||
|
||||
|
||||
bike.update(lat, lon, speed, bearing)
|
||||
|
||||
print('d', bike.destination, bike.flock_data())
|
||||
return jsonify(
|
||||
dest_heading=bike.heading_to(bike.destination),
|
||||
dest_heading=bike.heading_to([bike.destination[1],
|
||||
bike.destination[0]]),
|
||||
dest_distance=bike.distance_to(bike.destination),
|
||||
speed=bike.speed,
|
||||
**bike.flock_data())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user