server/fgs/flatten.py

84 lines
2.6 KiB
Python

from fgs.model import *
import itertools
def flatten(points):
"""
This function is used to convert
a list of points of arbitrary length into
a BoundingBox, which only has a fixed number
of fields (all of which are hardcoded variable names.
"""
# To convert an index to a variable namne, we define
# a list of setters, where the 1st setter changes the
# coord0 field, and so on.
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):
"""
This function is used to convert a grazing boundary,
which has 10 hardcoded vertex fields, to
an arbitrary list of points.
"""
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