def getPath(Graph, curloc, destination): nearby = find_nearby_edge_points(curloc[0], curloc[1], 15) source1 = nearby[0][0] source2 = nearby[0][1] intersection = nearby[1] len1 = vc(intersection, source1).meters + nx.shortest_path_length( Graph, source1, destination) len2 = vc(intersection, source2).meters + nx.shortest_path_length( Graph, source2, destination) if (len1 >= len2): path = nx.shortest_path(Graph, source2, destination) else: path = nx.shortest_path(Graph, source1, destination) finalpath = {} finalpath[0] = {'loc': curloc, 'attributes': None} finalpath[1] = {'loc': list(intersection), 'attributes': None} i = 2 k = 0 while k < len(path): finalpath[i] = { 'loc': list(path[k]), 'attributes': Graph.node[path[k]]["attributes"] } i += 1 k += 1 return finalpath
def getDeviationFromPath(self, curloc, loc1, loc2): brng1 = self.getBearing(curloc, loc1) brng2 = self.getBearing(curloc, loc2) angle = radians(fabs(getDifference(brng1, brng2))) a = vc(curloc, loc1).meters b = vc(curloc, loc2).meters base = vc(loc1, loc2).meters h = a * b * sin(angle) / base return h
def rerouting(self, Graph, index, fpath, destination): curloc = gps.loc() noLoc = fpath[index + 1]['loc'] listSrc = find_nearby_nodes(curloc, 100) alterRoute = None mindist = nx.shortest_path_length(Graph, fpath[0]['loc'], destination) + 500 for i in range(len(listSrc)): source = listSrc[i] path = nx.shortest_path(Graph, source, destination) dist = vc(curloc, source).meters + nx.shortest_path_length( Graph, source, destination) l = len(path) j = 0 while j < l: if path[j] == noLoc: break else: if j == l - 1: if dist < mindist: alterRoute = path mindist = dist j += 1 alterPath = None if alterRoute is not None: alterPath[0] = {'loc': curloc, 'attributes': Graph.node[curloc]} for k in len(alterRoute): alterPath[k + 1] = { 'loc': path[k + 1], 'attributes': Graph.node[path[k + 1]] } return alterPath
def return_path(self, path): #Traversing the navigation file self.curlocindex = 0 self.tts.speak(path[curlocindex]["ins"]) self.curlocindex += 1 #starting thread for checking deviation from path self.stop_dev = True th1 = threading.Thread(target=self.detectDeviation, args=(path, )) th1.setDaemon(True) th1.start() while curlocindex < len(path): if (vc(self.gps.loc(), (float(path[curlocindex]["loc"][0]), float(path[curlocindex]["loc"][1]))).meters < 5): self.tts.wait() self.tts.speak(path[curlocindex]["ins"]) self.curlocindex += 1 time.sleep(0.02) if self.input.pressed(3, 0): self.key = 1 self.tts.wait() self.tts.speak("Press enter to exit navigation") elif self.input.pressed(3, 2): if self.key == -1: self.tts.wait() self.tts.speak("No option selected") elif self.key == 1: self.key = -1 self.tts.wait() self.tts.speak("Returning to home") return
def GPSProximity(coords1, coords2, units='miles'): """ Get the proximity between two GPS locations in miles. Depends on the geopy module. coords1, coords2 are 2-item lists of [lat, lon] """ from geopy.distance import vincenty as vc return vc(coords1, coords2).miles
def get_distance(prev, curr): # prev and curr are coordinate pairs of (lat, long) return vc(prev, curr).miles
#CHICAGO inst["Chicago-lat"] = final_members.get_value("Chicago","lat")[0] inst["Chicago-lng"] = final_members.get_value("Chicago","lng")[0] serie = [] for college in inst.iterrows(): serie.append(college[1]) distances = [] for uni in serie: ls = uni.tolist() distances.append(vc(ls[:2],ls[2:]).miles) inst.drop(["Chicago-lat","Chicago-lng"], axis = 1,inplace = True) chicago_mean_distance = sum(distances)/len(distances) #BOSTON inst["lat2"] = final_members.get_value("Boston","lat") inst["lng2"] = final_members.get_value("Boston","lng") boston_serie= [] for college in inst.iterrows(): boston_serie.append(college[1]) boston_distances = []
def navigation(self, dest): #Traversing the navigation file self.attr = {} self.out_of_path = False curloc = self.gps.loc() p = routeSave.getPath(self.map, curloc, dest) path = routeSave.createFile(p) self.curlocindex = 0 self.tts.speak(path[curlocindex]["ins"]) self.curlocindex += 1 #starting thread for checking deviation from path self.stop_dev = True new_path = None th1 = threading.Thread(target=self.detectDeviation, args=(path, )) th1.setDaemon(True) th1.start() while curlocindex < len(path): time.sleep(0.02) if (vc(self.gps.loc(), (float(path[curlocindex]["loc"][0]), float(path[curlocindex]["loc"][1]))).meters < 5): self.tts.wait() self.tts.speak(path[curlocindex]["ins"]) self.curlocindex += 1 if self.input.pressed(3, 0): self.key = 1 self.tts.wait() self.tts.speak("Press enter to exit navigation") elif self.input.pressed(3, 1): self.key = 2 self.tts.wait() self.tts.speak("Press enter to add attributes") elif self.input.pressed(1, 1): self.key = 3 self.tts.wait() self.tts.speak("Press enter to return to source") elif self.input.pressed(2, 1) and self.out_of_path: self.key = 4 new_path = rerouting(map, curlocindex - 1, p, dest) if new_path == None: self.tts.wait() self.tts.speak("Rerouting from here is not possible") else: self.tts.wait() self.tts.speak("Reroute from here") elif self.input.pressed(3, 2): if self.key == -1: self.tts.wait() self.tts.speak("No option selected") elif self.key == 1: self.key = -1 self.tts.wait() self.tts.speak("Returning to home") arr = sorted(self.attr.items()) size = len(arr) li = [] for i in range(size): Li.append((tuple(arr[i][1]["loc"]), { "attributes": arr[i][1]["attributes"] })) self.map.merge_with_path(Li) return -1 elif self.key == 2: self.key = -1 self.tts.wait() self.addAttr if (vc(self.gps.loc(), (float(path[curlocindex - 1]["loc"][0]), float(path[curlocindex - 1]["loc"][1]))).meters < 5): a = curlocindex - 2 else: a = curlocindex - 1 if a < 0: self.addAttr(None, self.gps.heading()) else: self.addAttr(path[a]["loc"], None) # add attributes elif self.key == 3: self.key = -1 self.tts.wait() self.stop_dev = False arr = sorted(self.attr.items()) size = len(arr) li = [] for i in range(size): Li.append((tuple(arr[i][1]["loc"]), { "attributes": arr[i][1]["attributes"] })) self.map.merge_with_path(Li) self.tts.speak( "Press any key to begin navigation to source") path = routeSave.returnFromPt(curlocindex - 1, p) while (self.input.key() == False).all() or not self.gps.check_device_pos(): time.sleep(0.05) if not (self.input.key() == False).all(): while not (self.input.key() == False).all(): time.sleep(0.01) self.tts.stop() self.tts.speak( "Device not placed properly.Correct device position and press any key to start navigation" ) self.tts.stop() self.return_path(path) return -1 elif self.key == 4: self.key = -1 self.tts.wait() self.stop_dev = False self.tts.speak("New path detected") self.tts.wait() path = routeSave.createFile(new_path) self.curlocindex = 0 self.tts.speak(path[curlocindex]["ins"]) self.curlocindex += 1 #starting thread for checking deviation from path self.stop_dev = True new_path = None th1 = threading.Thread(target=self.detectDeviation, args=(path, )) th1.setDaemon(True) th1.start() continue
def detectDeviation(self, waypoints): getloc = self.gps() loc1 = (float(waypoints[0]["loc"][0]), float(waypoints[0]["loc"][1])) x = 1 loc2 = (float(waypoints[x]["loc"][0]), float(waypoints[x]["loc"][1])) while x < len(waypoints) and self.stop_dev: curloc = tuple(getloc.loc()) while (vc(curloc, loc2).meters > 7 and self.stop_dev): curloc = tuple(getloc.loc()) loc2 = (float(waypoints[x]["loc"][0]), float(waypoints[x]["loc"][1])) deviation = self.getDeviationFromPath(curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.2) if (deviation < 7 and self.getDifference(idealbrng, routebrng) > 40): pulsev.infwave(0.5, 1.5) while (deviation < 7 and self.getDifference(idealbrng, routebrng) > 20 and self.stop_dev): #code to turn left curloc = tuple(getloc.loc()) deviation = self.getDeviationFromPath( curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.5) pulsev.switchoff() if (deviation < 7 and self.getDifference(idealbrng, routebrng) < (-40)): pulsev.infwave(1.5, 0.5) while (deviation < 7 and self.getDifference(idealbrng, routebrng) < (-20) and self.stop_dev): #code to turn right curloc = tuple(getloc.loc()) deviation = self.getDeviationFromPath( curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.5) pulsev.switchoff() if (deviation > 7 and self.getDifference(idealbrng, routebrng) > 40): self.out_of_path = True pulsev.infwave(0.5, 1.5) pulseb.infwave(1, 1) while (deviation > 7 and self.getDifference(idealbrng, routebrng) > 20 and self.stop_dev): #code to turn left and buzzer as well curloc = tuple(getloc.loc()) deviation = self.getDeviationFromPath( curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.5) pulsev.switchoff() pulseb.switchoff() if (deviation > 7 and self.getDifference(idealbrng, routebrng) < (-40)): self.out_of_path = True pulsev.infwave(1.5, 0.5) pulseb.infwave(1, 1) while (deviation > 7 and self.getDifference(idealbrng, routebrng) < (-20) and self.stop_dev): #code to turn right and buzzer as well curloc = tuple(getloc.loc()) deviation = self.getDeviationFromPath( curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.5) pulseb.switchoff() pulsev.switchoff() if (deviation > 7 and fabs(self.getDifference(idealbrng, routebrng)) < 20): self.out_of_path = True pulseb.infwave(1, 1) while (deviation > 7 and fabs(self.getDifference(idealbrng, routebrng)) < 20 and self.stop_dev): #code for buzzer only curloc = tuple(getloc.loc()) deviation = self.getDeviationFromPath( curloc, loc1, loc2) idealbrng = self.getBearing(curloc, loc2) routebrng = getloc.heading() time.sleep(0.5) pulseb.switchoff() if self.curlocindex > x: loc1 = loc2 x += 1
def store(self): self.attr = {} sampledpts = dict() self.tts.speak("Enter Destination") self.tts.wait() name = self.input.text() self.tts.speak( "Destination saved. Press any key to start route saving") while (self.input.key() == False).all() or not self.gps.check_device_pos(): time.sleep(0.05) if not (self.input.key() == False).all() and not self.gps.check_device_pos(): while not (self.input.key() == False).all(): time.sleep(0.01) self.tts.stop() self.tts.speak( "Device not placed properly.Correct device position and press any key to start route saving" ) self.tts.stop() self.tts.speak("Route saving started") curloc = self.gps.loc() prevloc = curloc timing = time.time() sampledpts[timing] = {"loc": curloc, 'attributes': None} num_points = 0 while True: time.sleep(0.02) if not self.gps.check_device_pos(): self.tts.speak("Device not placed properly.") self.tts.wait() while not self.gps.check_device_pos(): time.sleep(0.01) self.tts.speak("Device correctly placed") curloc = self.gps.loc() if (vc(prevloc, curloc).meters > 1): prevloc = curloc timing = time.time() sampledpts[timing] = {"loc": curloc, 'attributes': None} num_points += 1 if self.input.pressed(3, 0): self.key = 1 self.tts.stop() self.tts.speak("Press enter to exit") elif self.input.pressed(3, 1): self.key = 2 self.tts.stop() self.tts.speak("Press enter to add attributes") elif self.input.pressed(3, 2): if self.key == -1: self.tts.stop() if num_points < 10: self.tts.speak("Not enough points to save route") else: self.tts.speak("Press again to save route") self.key = 3 elif self.key == 1: self.key = -1 self.tts.stop() self.tts.speak("Returning to home") arr = sorted(self.attr.items()) size = len(arr) li = [] for i in range(size): Li.append((tuple(arr[i][1]["loc"]), { "attributes": arr[i][1]["attributes"] })) self.map.merge_with_path(Li) return elif self.key == 2: self.key = -1 self.tts.stop() self.addAttr self.addAttr(None, None) # add attributes elif self.key == 3: self.key = -1 self.tts.stop() self.tts.speak("Route saved.Returning to home") end_point = self.gps.loc() timing = time.time() sampledpts[timing] = {"loc": end_point, 'attributes': None} out = self.sampling(sampledpts, self.attr) size = len(out) Li = [] for i in range(size - 1): Li.append((tuple(out[i]["loc"]), { "attributes": out[i]["attributes"] })) i = size - 1 Li.append((tuple(out[i]["loc"]), { "attributes": out[i]["attributes"], "name": name })) self.map.merge_with_path(Li) with open('li', 'wb') as f: dill.dump(Li, f) print(Li) self.map.draw_html("test_points.html") self.map.save_graph("graph.gml") return
def distance_calc(data, row1, row2): start = (data.iloc[row1, 3], data.iloc[row1, 4]) stop = (data.iloc[row2, 5], data.iloc[row2, 6]) return np.log(vc(start, stop).miles)
def distance_calc1(row): start = (row['buyer_lat'], row['buyer_long']) stop = (row['target_lat'], row['target_long']) return np.log(vc(start, stop).miles)
def metric_distance(p1, p2): """ Distance between two GPS coordinates in meters """ return vc(p1, p2).meters
def nearest_point_of_edge(a1, a2, b): return a1 if vc(a1, b).meters <= vc(a2, b).meters else a2