class BBTree(object): class TItem(object): def __init__(self,bb,payload): self.bb=bb self.payload=payload def __repr__(self): return "TItem(%s,%s)"%(self.bb,self.payload) class Adapter(object): def __init__(self,vec): self.vec=vec self.val=[] def overlapping(self,box): result=[] for bspitem in self.bsp.items_whose_dominating_area_overlaps(box): for item in bspitem.val: if item.bb.overlaps(box): result.append(item) return result def __init__(self,items,epsilon): itarr=[] for item in items: bb=item.bb itarr.append(BBTree.Adapter((bb.x1,bb.y1))) itarr.append(BBTree.Adapter((bb.x2,bb.y2))) self.bsp=BspTree(itarr) for item in items: bb=item.bb.expanded(epsilon) dom=self.bsp.find_item_dominating(bb) dom.val.append(item)
def __init__(self,items,epsilon): itarr=[] for item in items: bb=item.bb itarr.append(BBTree.Adapter((bb.x1,bb.y1))) itarr.append(BBTree.Adapter((bb.x2,bb.y2))) self.bsp=BspTree(itarr) for item in items: bb=item.bb.expanded(epsilon) dom=self.bsp.find_item_dominating(bb) dom.val.append(item)
class UserData(object): def have_any(self): return not self.empty def __init__(self, user, orders=None): #print "Loading userdata for ",user self.user = user self.log = [] self.points = dict() self.spaces = dict() self.pointslookup = dict() self.spaceslookup = dict() self.empty = True zoomlevel = 13 pointtypes = ['obstacles', 'airfields', 'sigpoints'] spacestypes = ['airspaces', 'firs'] for pointtype in pointtypes: self.points[pointtype] = [] for spacetype in spacestypes: self.spaces[spacetype] = [] if orders == None: orders = [] for csets in meta.Session.query(CustomSets).filter( CustomSets.user == user).all(): customs = list( meta.Session.query(CustomSet).filter( sa.and_(CustomSet.user == user, CustomSet.setname == csets.setname, CustomSet.version == csets.active)).all()) orders.extend(customs) for custom in orders: try: #print "Found custom set:",custom.setname ##print "Found active custom set:",custom.setname,custom.version #print "Data:" #print "------------------" #print custom.data #print "Cont1" data = json.loads(u"".join([ x for x in custom.data.split("\n") if not x.strip().startswith("#") ]).encode('utf8')) if type(data) != dict: raise Exception( "Top level must be object, that is, file must start with '{'" ) #print "Cont2" for pointtype in pointtypes: if pointtype in data: out = [] for point in data[pointtype]: #print "val point" if validate_point(point, pointtype, self.log): out.append(point) #print "aft val point" if pointtype == 'airfields': #print "val airf" validate_airfield_space(point, self.log, self.spaces['airspaces']) #print "aft val airf" self.points[pointtype].extend(out) data.pop(pointtype) #print "Cont3" for spacetype in spacestypes: if spacetype in data: out = [] for space in data[spacetype]: if validate_space(space, spacetype, self.log): out.append(space) self.spaces[spacetype].extend(out) data.pop(spacetype) #print "Cont4" if len(data.keys()): for key in data.keys(): self.log.append("Unknown top-level key: %s" % (key, )) #print "Cont5" except Exception, cause: print "Problem parsing custom", traceback.format_exc() self.log.append(traceback.format_exc()) if len(self.log) == 0: #print "About to start bsptreein" for pointtype in pointtypes: bspitems = [] for item in self.points[pointtype]: #print "Adding BspTree item of type: ",pointtype,"item:",item bspitems.append( BspTree.Item( mapper.latlon2merc(mapper.from_str(item['pos']), 13), item)) self.pointslookup[pointtype] = BspTree(bspitems) if bspitems: self.empty = False airspaces = self.spaces.get('airspaces', []) firs = [space for space in airspaces if space['type'] == 'FIR'] regular_airspaces = [ space for space in airspaces if space['type'] != 'FIR' ] self.spaces['airspaces'] = regular_airspaces self.spaces['firs'] = firs if len(self.log) == 0: for spacetype in spacestypes: bbitems = [] for space in self.spaces[spacetype]: poly_coords = [] bb = BoundingBox(1e30, 1e30, -1e30, -1e30) for coord in space['points']: print "Coord:", coord x, y = mapper.latlon2merc(mapper.from_str(coord), zoomlevel) bb.x1 = min(bb.x1, x) bb.x2 = max(bb.x2, x) bb.y1 = min(bb.y1, y) bb.y2 = max(bb.y2, y) poly_coords.append(Vertex(int(x), int(y))) if len(poly_coords) < 3: continue poly = Polygon(vvector(poly_coords)) #print "Item:",space bbitems.append(BBTree.TItem(bb, (poly, space))) self.spaceslookup[spacetype] = BBTree(bbitems, 0.5) if bbitems: self.empty = False self.date = datetime.utcnow()