예제 #1
0
 def __init__(self):
     #instantiate a GPSTalker
     #mytalker = GPSTalker()
     self.mytalker = TestTalker("data/gps3.out", .01)
     self.mytalker.runLoop()
     self.db = Database()
     self.get_bearings()
     return
예제 #2
0
class MapFactory:

    location = None
    mytalker = None
    
    def __init__(self):
        #instantiate a GPSTalker
        #mytalker = GPSTalker()
        self.mytalker = TestTalker("data/gps3.out", .01)
        self.mytalker.runLoop()
        self.db = Database()
        self.get_bearings()
        return
    
    def get_location(self):
        return location
        
    def update_location(self):
        #while not self.mytalker.getMsg():
        time.sleep(.1)
        loc = self.mytalker.getMsg()
        print "loc=", loc
        self.location = {'time': loc[0], 'lat': loc[1], 'lon': loc[2]}
        
    def get_bearings(self):
        cur_intersection = None    #Where road segment begins
        self.update_location()
        #find the list of intersections of the road I'm on (database)
        con_list = self.db.getNeighborsFromCoord(self.location['lat'], \
            self.location['lon'])
        print "con_list", con_list
        while True:
            self.update_location()
            con_list = self.db.getNeighborsFromCoord(self.location['lat'], \
                self.location['lon'])
            print "Con list:", con_list, "\n"
            for intersection in con_list:
                if self.get_dist(self.location, intersection) < 100:
                    self.find_first_segment(intersection)
                    break

    def map_points(self, segment_begin, cur_intersection):
        time_taken = None   #road segment duration
        print "setting seg begin to None"
        segment_end = None  #Where road segment ends
        closest_pt = None   #our closest point to intersection coordinates
        #con_list = None  #connection list: list of intersections that connect
        running = True
        
        while running:  #loop to get data from queue
            print " Starting looping ness"
            self.update_location()
            #find next intersections
            con_list = self.db.getNeighbors(self.location)
            while 100 > self.get_dist(self.location, cur_intersection):
                for intersection in con_list:
                    #distance to next intersection
                    current_dist = get_dist(self.location, intersection)
                    if 100 > current_dist:
                        closest_pt = find_segment_end(cur_intersection, \
                        self.location)
                        cur_intersection = intersection
                    segment_end = closest_pt
                    time_taken = segment_end['time'] - segment_begin['time']
                    #send Laura time_taken and segment_begin['time']
                    print "Setting travel time:", segment_begin['lat'], \
                        segment_begin['lon'], "segment end:", segment_end['lat'], \
                        segment_end['lon'], "time:", time_taken
                    db.setTravelTime(segment_begin['lat'], segment_begin['lon'], \
                        segment_end['lat'], segment_end['lon'], time_taken)
                    segment_begin = segment_end
        return
    
    def find_first_segment(self, cur_intersection):
        print "in find first segment"
        cur_dist = 100
        closest_dist = 999999
        while cur_dist < 125:
            print "with 125 of first intersection\n"
            cur_dist = self.get_dist(self.location, cur_intersection)
            if cur_dist < closest_dist:
                closest_dist = cur_dist
                closest_pt = cur_intersection
                closest_pt['time'] = self.location['time']
            self.update_location()
        while True:
            self.update_location()
            con_list = self.db.getNeighborsFromCoord(self.location['lat'], \
                self.location['lon'])
            for intersection in con_list:
                if self.get_dist(self.location, intersection) < 100:
                    self.map_points(closest_pt, intersection)
                    break
                    
                        
    def find_segment_end(self, cur_intersection, closest_pt):
        self.update_location()     
        current_dist = self.get_dist(self.location, cur_intersection)  
        closest_pt_dist = self.get_dist(closest_pt, cur_intersection)
        if closest_pt_dist > current_dist:
            closest_pt['time'] = self.location['time']
            closest_pt['lat'] = cur_intersection[0]
            closest_pt['lon'] = cur_intersection[1]
        if current_dist > 125:
            return closest_pt
        else:
            return find_segment_end(cur_intersection, closest_pt)

    def get_dist(self, pt1, pt2):
        coord_dist = math.sqrt(math.pow(abs(pt2['lat'] - pt1['lat']),2) +  \
        math.pow(abs(pt2['lon'] - pt1['lon']),2))
        dist = (coord_dist*10000)*36.5 #distance between pts in ft
        return dist

    def get_vel(self, pt1, pt2):
        coord_dist = math.sqrt(math.pow(abs(pt2['lat'] - pt1['lat']),2) +  \
        math.pow(abs(pt2['lon'] - pt1['lon']),2))
        dist = (coord_dist*10000)*36.5 #distance between pts in ft
        tim = pt2['time']-pt1['time'] #almost always 1 sec exactly
        vel = ((dist/5280)*3600)/(tim)  #vel in MPH
        return vel