from PIL import Image from latLng import latLng class layer: """template for a GPS image layer""" tl=latLng() br=latLng() pixel=(0,0) def __init__(self,file,ll1,ll2): try: self.image=Image.open(file) except: print "gps layer: failed to open", file try: self.tl.parse(ll1) self.br.parse(ll2) self.pixsize=latLng(abs(self.tl.lat-self.br.lat)/self.image.size[1],abs(self.tl.lng-self.br.lng)/self.image.size[0]) except: print "gps layer: failed to parse", file def checkcoord(self,pos): p=self.findpixel(pos) if p!=self.pixel: self.pixel=p return self.setcoord(p) else: return None def findpixel(self,pos): return (int((pos.lng-self.tl.lng)/self.pixsize.lng),int((pos.lat-self.br.lat)/self.pixsize.lat)) def setcoord(self,pos): """to be overwritten: gets a messages when values change" returns None otherwise""" return None class trigger(): """a generic trigger - id can be anything - i.e. a string or an integer""" def __init__(self,id,command,param): self.id=id self.command=(command,param) def check(self,id): if id==self.id: return command else: return None class indexlayer(layer): """generates gps triggers from an index colour image triggers when colour changes""" triggers=[] colour=-1 def setcoord(self,pos): result=None #210 35 5 185 c=self.image.getpixel(pos) if c!=self.colour: self.colour=c print "indexlayer: new colour",c for t in self.triggers: if c==t.id: result=t.command return result class scalelayer(layer): """generates a varying signal based on interpolating a greyscale image uses sub pixel position""" def findpixel(self,pos): #float version return ((pos.lng-self.tl.lng)/self.pixsize.lng,(pos.lat-self.br.lat)/self.pixsize.lat) def setcommand(self,command): self.command=command def setcoord(self,pos): px=int(pos[0]) py=int(pos[1]) c=float(self.image.getpixel((px,py)))/255.0 c1=float(self.image.getpixel((px+1,py)))/255.0 c2=float(self.image.getpixel((px,py+1)))/255.0 c3=float(self.image.getpixel((px+1,py+1)))/255.0 xf=pos[0]-px yf=pos[1]-py #lerp sub-pixel value return (self.command,(((c*(1.0-xf))+(c1*xf))*(1.0-yf))+(((c2*(1.0-xf))+(c3*xf))*yf))