Exemplo n.º 1
0
def get_firs(lat, lon, user):
    if user == None: return
    zoomlevel = 13
    px, py = mapper.latlon2merc((lat, lon), zoomlevel)
    bb0 = BoundingBox(px, py, px, py)
    for item in ensure_user_data(user).spaceslookup['firs'].overlapping(bb0):
        yield item.payload[1]
Exemplo n.º 2
0
def get_png(width,height,ll1,ll2,ll3,ll4):
    """
    Orientation of lls:
    
    ll1----------ll4
    |             |
    |             |
    |             |
    |             |
    ll2----------ll3
    """
    zl=13
    while zl>=5:
        pp1=mapper.latlon2merc(ll1,zl)
        pp2=mapper.latlon2merc(ll2,zl)
        pp3=mapper.latlon2merc(ll3,zl)
        pp4=mapper.latlon2merc(ll4,zl)
        pps=[pp1,pp2,pp3,pp4]
        xs=[t[0] for t in pps]
        ys=[t[1] for t in pps]
        x1=min(xs)
        x2=max(xs)
        y1=min(ys)
        y2=max(ys)
        
        ysize=(y2-y1)
        xsize=(x2-x1)
        if xsize>=2*width or ysize>=2*height:
            zl-=1
        else:
            break                        
    
    big=get_big_map_img((x1,y1),(x2-x1,y2-y1),zl)
    qs=[]
    for pp in pps:
        x,y=pp
        o=[x-x1,y-y1]
        qs.extend(o)
        
    res=big.transform((width,height),
        Image.QUAD,
        tuple(qs))
                  
    return res


    
Exemplo n.º 3
0
def get_generic(lat, lon, zoomlevel, user, what):
    px, py = mapper.latlon2merc((lat, lon), 13)
    rad = 8 << (13 - zoomlevel)
    bb0 = BoundingBox(px - rad, py - rad, px + rad, py + rad)
    ud = ensure_user_data(user)
    for item in ud.pointslookup[what].findall_in_bb(bb0):
        print "Got:", item.val
        yield item.val
Exemplo n.º 4
0
def get_png(width, height, ll1, ll2, ll3, ll4):
    """
    Orientation of lls:
    
    ll1----------ll4
    |             |
    |             |
    |             |
    |             |
    ll2----------ll3
    """
    zl = 13
    while zl >= 5:
        pp1 = mapper.latlon2merc(ll1, zl)
        pp2 = mapper.latlon2merc(ll2, zl)
        pp3 = mapper.latlon2merc(ll3, zl)
        pp4 = mapper.latlon2merc(ll4, zl)
        pps = [pp1, pp2, pp3, pp4]
        xs = [t[0] for t in pps]
        ys = [t[1] for t in pps]
        x1 = min(xs)
        x2 = max(xs)
        y1 = min(ys)
        y2 = max(ys)

        ysize = (y2 - y1)
        xsize = (x2 - x1)
        if xsize >= 2 * width or ysize >= 2 * height:
            zl -= 1
        else:
            break

    big = get_big_map_img((x1, y1), (x2 - x1, y2 - y1), zl)
    qs = []
    for pp in pps:
        x, y = pp
        o = [x - x1, y - y1]
        qs.extend(o)

    res = big.transform((width, height), Image.QUAD, tuple(qs))

    return res
Exemplo n.º 5
0
def poly(points): 
    last=None
    poly_coords=[]
    for coord in points:
        merc=mapper.latlon2merc(mapper.from_str(coord),13)
        if merc==last: continue
        last=merc
        poly_coords.append(Vertex(int(merc[0]),int(merc[1])))
    if len(poly_coords)>=3:
        return Polygon(vvector(poly_coords))
    return None
Exemplo n.º 6
0
def poly(points):
    last = None
    poly_coords = []
    for coord in points:
        merc = mapper.latlon2merc(mapper.from_str(coord), 13)
        if merc == last: continue
        last = merc
        poly_coords.append(Vertex(int(merc[0]), int(merc[1])))
    if len(poly_coords) >= 3:
        return Polygon(vvector(poly_coords))
    return None
Exemplo n.º 7
0
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()