Add boundary editing via lists of points

This commit is contained in:
Danila Fedorin 2020-05-14 00:42:44 -07:00
commit f3a711787c
3 changed files with 104 additions and 4 deletions

68
fgs/flatten.py Normal file
View File

@ -0,0 +1,68 @@
from fgs.model import *
import itertools
def flatten(points):
def set0x(b, v): b.coord0x = v
def set1x(b, v): b.coord1x = v
def set2x(b, v): b.coord2x = v
def set3x(b, v): b.coord3x = v
def set4x(b, v): b.coord4x = v
def set5x(b, v): b.coord5x = v
def set6x(b, v): b.coord6x = v
def set7x(b, v): b.coord7x = v
def set8x(b, v): b.coord8x = v
def set9x(b, v): b.coord9x = v
def set0y(b, v): b.coord0y = v
def set1y(b, v): b.coord1y = v
def set2y(b, v): b.coord2y = v
def set3y(b, v): b.coord3y = v
def set4y(b, v): b.coord4y = v
def set5y(b, v): b.coord5y = v
def set6y(b, v): b.coord6y = v
def set7y(b, v): b.coord7y = v
def set8y(b, v): b.coord8y = v
def set9y(b, v): b.coord9y = v
setters_x = [ set0x, set1x, set2x, set3x, set4x, set5x, set6x, set7x, set8x, set9x ]
setters_y = [ set0y, set1y, set2y, set3y, set4y, set5y, set6y, set7y, set8y, set9y ]
boundary = BoundingBox()
boundary.num_points = len(points)
for (point, set_x, set_y) in zip(points, setters_x, setters_y):
set_x(boundary, float(point['longitude']))
set_y(boundary, float(point['latitude']))
return boundary
def unflatten(boundary):
if boundary is None: return []
getters_x = [
lambda b: b.coord0x,
lambda b: b.coord1x,
lambda b: b.coord2x,
lambda b: b.coord3x,
lambda b: b.coord4x,
lambda b: b.coord5x,
lambda b: b.coord6x,
lambda b: b.coord7x,
lambda b: b.coord8x,
lambda b: b.coord9x
]
getters_y = [
lambda b: b.coord0y,
lambda b: b.coord1y,
lambda b: b.coord2y,
lambda b: b.coord3y,
lambda b: b.coord4y,
lambda b: b.coord5y,
lambda b: b.coord6y,
lambda b: b.coord7y,
lambda b: b.coord8y,
lambda b: b.coord9y
]
points = []
for i in range(boundary.num_points):
points.append({'longitude': str(getters_x[i](boundary)), 'latitude': str(getters_y[i](boundary))})
return points

View File

@ -6,6 +6,7 @@ class Collar(db.Model):
name = db.Column(db.String) name = db.Column(db.String)
active = db.Column(db.Boolean) active = db.Column(db.Boolean)
data_points = db.relationship('DataPoint') data_points = db.relationship('DataPoint')
boundary = db.relationship("BoundingBox", uselist=False)
class DataPoint(db.Model): class DataPoint(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -33,6 +34,32 @@ class StimulusActivation(db.Model):
voltage_level = db.Column(db.Integer) voltage_level = db.Column(db.Integer)
datetime = db.Column(db.DateTime) datetime = db.Column(db.DateTime)
class BoundingBox(db.Model):
id = db.Column(db.Integer, primary_key=True)
collar_id = db.Column(db.Integer, db.ForeignKey('collar.id'))
num_points = db.Column(db.Integer)
coord0x = db.Column(db.Float(precision=10))
coord0y = db.Column(db.Float(precision=10))
coord1x = db.Column(db.Float(precision=10))
coord1y = db.Column(db.Float(precision=10))
coord2x = db.Column(db.Float(precision=10))
coord2y = db.Column(db.Float(precision=10))
coord3x = db.Column(db.Float(precision=10))
coord3y = db.Column(db.Float(precision=10))
coord4x = db.Column(db.Float(precision=10))
coord4y = db.Column(db.Float(precision=10))
coord5x = db.Column(db.Float(precision=10))
coord5y = db.Column(db.Float(precision=10))
coord6x = db.Column(db.Float(precision=10))
coord6y = db.Column(db.Float(precision=10))
coord7x = db.Column(db.Float(precision=10))
coord7y = db.Column(db.Float(precision=10))
coord8x = db.Column(db.Float(precision=10))
coord8y = db.Column(db.Float(precision=10))
coord9x = db.Column(db.Float(precision=10))
coord9y = db.Column(db.Float(precision=10))
class User(db.Model): class User(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String) username = db.Column(db.String)

View File

@ -1,4 +1,5 @@
from . import app, db from . import app, db
from .flatten import flatten, unflatten
from .jwt import jwt_required 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
@ -89,7 +90,7 @@ def collar_detail(id):
order_by(StimulusActivation.datetime.desc()) order_by(StimulusActivation.datetime.desc())
n_stimulus = stimulus_points.count() n_stimulus = stimulus_points.count()
return jsonify({'id': collar.id, 'name': collar.name, 'stimulus': n_stimulus }) return jsonify({'id': collar.id, 'name': collar.name, 'stimilus': n_stimulus, 'boundary': unflatten(collar.boundary) })
@app.route('/collars/<int:id>/boundary/set', methods=['POST']) @app.route('/collars/<int:id>/boundary/set', methods=['POST'])
@jwt_required @jwt_required
@ -97,6 +98,10 @@ def collar_set_boundary(id):
collar = Collar.query.filter_by(id=id).first() collar = Collar.query.filter_by(id=id).first()
if collar is None: abort(404) if collar is None: abort(404)
print(request.json) new_boundary = flatten(request.json)
abort(500) if collar.boundary is not None:
db.session.delete(collar.boundary)
collar.boundary = new_boundary
db.session.add(new_boundary)
db.session.commit()
return jsonify({})