calculate total distance traveled
This commit is contained in:
parent
09cbae3233
commit
7a991f697a
BIN
fgs/.model.py.swp
Normal file
BIN
fgs/.model.py.swp
Normal file
Binary file not shown.
BIN
fgs/.views.py.swo
Normal file
BIN
fgs/.views.py.swo
Normal file
Binary file not shown.
BIN
fgs/.views.py.swp
Normal file
BIN
fgs/.views.py.swp
Normal file
Binary file not shown.
BIN
fgs/__init__.pyc
Normal file
BIN
fgs/__init__.pyc
Normal file
Binary file not shown.
BIN
fgs/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
fgs/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
fgs/__pycache__/jwt.cpython-37.pyc
Normal file
BIN
fgs/__pycache__/jwt.cpython-37.pyc
Normal file
Binary file not shown.
BIN
fgs/__pycache__/model.cpython-37.pyc
Normal file
BIN
fgs/__pycache__/model.cpython-37.pyc
Normal file
Binary file not shown.
BIN
fgs/__pycache__/views.cpython-37.pyc
Normal file
BIN
fgs/__pycache__/views.cpython-37.pyc
Normal file
Binary file not shown.
24
fgs/views.py
24
fgs/views.py
|
@ -3,6 +3,7 @@ from .jwt import jwt_required
|
||||||
from .model import *
|
from .model import *
|
||||||
from flask import g, jsonify, request, abort
|
from flask import g, jsonify, request, abort
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
import geopy.distance
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
|
@ -37,6 +38,29 @@ def collars():
|
||||||
|
|
||||||
return jsonify(active_collars)
|
return jsonify(active_collars)
|
||||||
|
|
||||||
|
@app.route('/collars/stats/distance')
|
||||||
|
@jwt_required
|
||||||
|
def collars_distance():
|
||||||
|
active_collars = []
|
||||||
|
for collar in Collar.query.filter_by(active=True).all():
|
||||||
|
coords = DataPoint.query.filter_by(collar_id=collar.id).\
|
||||||
|
order_by(DataPoint.datetime.desc())
|
||||||
|
|
||||||
|
distance_kilometers = 0
|
||||||
|
coords_pairs = list(map(lambda e : (e.longitude, e.latitude), coords))
|
||||||
|
|
||||||
|
coord_last = coords_pairs.pop()
|
||||||
|
for coord in coords_pairs:
|
||||||
|
distance_kilometers += geopy.distance.VincentyDistance(coord_last,coord).km
|
||||||
|
coord_last = coord
|
||||||
|
|
||||||
|
active_collars.append(
|
||||||
|
{'id': collar.id, 'name': collar.name,
|
||||||
|
'distance': distance_kilometers }
|
||||||
|
)
|
||||||
|
|
||||||
|
return jsonify(active_collars)
|
||||||
|
|
||||||
@app.route('/collars/<int:id>/history')
|
@app.route('/collars/<int:id>/history')
|
||||||
@jwt_required
|
@jwt_required
|
||||||
def collar_history(id):
|
def collar_history(id):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user