return centroid of offset locations where rides _will be_
This commit is contained in:
parent
1baf2e39d7
commit
8a861dba1e
42
model.py
42
model.py
@ -3,6 +3,7 @@ Object-Document Map for bike model
|
||||
"""
|
||||
from mongoengine import Document, DateTimeField, GeoPointField, \
|
||||
FloatField, connection
|
||||
import bson
|
||||
import datetime
|
||||
from LatLon23 import LatLon, Latitude, Longitude
|
||||
from geopy import distance
|
||||
@ -22,21 +23,30 @@ class Flock:
|
||||
bearings = list()
|
||||
n = 0
|
||||
for b in bikes:
|
||||
|
||||
dt = datetime.datetime.now() - b['last_update']
|
||||
progress = (b['speed'] * dt.seconds) / 1000 # in km
|
||||
point = LatLon(b['point'][1], b['point'][0])
|
||||
next_point = point.offset(b['bearing'], progress)
|
||||
|
||||
speeds.append(b['speed'])
|
||||
bearings.append(b.get('bearing', 0))
|
||||
lats.append(float(b['point'][1]))
|
||||
lons.append(float(b['point'][0]))
|
||||
lats.append(float(next_point.lat))
|
||||
lons.append(float(next_point.lon))
|
||||
n += 1
|
||||
|
||||
|
||||
self.mean_speed = 0
|
||||
self.size = 0
|
||||
self.centroid = None
|
||||
|
||||
if n > 0:
|
||||
self.size = n
|
||||
self.mean_speed = sum(speeds) / float(len(speeds))
|
||||
|
||||
|
||||
self.centroid = LatLon(Latitude(sum(lats) / len(lats)),
|
||||
Longitude(sum(lons) / len(lons)))
|
||||
else:
|
||||
self.mean_speed = 0
|
||||
self.size = 0
|
||||
self.centroid = None
|
||||
|
||||
|
||||
class Bike(Document):
|
||||
@ -54,8 +64,9 @@ class Bike(Document):
|
||||
self.point = (lon, lat)
|
||||
self.speed = speed
|
||||
self.bearing = bearing
|
||||
self.last_update = datetime.datetime.now()
|
||||
self.save()
|
||||
|
||||
|
||||
def heading_to(self, target):
|
||||
s = LatLon(Latitude(self.point[1]),
|
||||
Longitude(self.point[0]))
|
||||
@ -95,11 +106,16 @@ class Bike(Document):
|
||||
# these are bikes around me
|
||||
local = [bike for bike in
|
||||
self.db.bike.find(
|
||||
{'point':
|
||||
{'$near':
|
||||
{'$geometry': {'type': 'Point',
|
||||
'coordinates': self.point},
|
||||
'$maxDistance': local_radius}}})]
|
||||
{'point': {
|
||||
'$near': {
|
||||
'$geometry': {
|
||||
'type': 'Point',
|
||||
'coordinates': self.point},
|
||||
'$maxDistance': local_radius}},
|
||||
'last_update': {
|
||||
'$gte': datetime.datetime.now() - datetime.timedelta(minutes=15)},
|
||||
'_id': {'$ne': bson.objectid.ObjectId(self.id)}})]
|
||||
|
||||
local_ids = set([bike['_id'] for bike in local])
|
||||
|
||||
# these are going near my destination
|
||||
@ -122,7 +138,7 @@ class Bike(Document):
|
||||
:return: heading from point to flock centroid, in degrees
|
||||
"""
|
||||
flock = Flock(self.find_flock())
|
||||
if flock.size > 1: # no flocking with self!
|
||||
if flock.size > 0:
|
||||
flock_heading = self.heading_to(flock.centroid)
|
||||
flock_distance = distance.geodesic((self.point[1],
|
||||
self.point[0]), (self.destination[1],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user