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