def getDistance(self): self.temp=self.a.getLocation()# stores current location in self.temp threshold=900.0000 #This is the threshold value in meter that user wants to have visual alarm before he/she enters into the Hazardous area. data = [500,600,700,800,900,1000]#Random numers describing the number of accident occured in listed 5 intersections accordingly if self.temp: self.distanceHB = Haversine(self.temp,Haversine.HomerBlock)#stores current distance from Homerwatson/Block line intersection self.distanceFW = Haversine(self.temp,Haversine.FairwayWilson)#stores current distance from Fairway/Wilson intersection self.distanceHBishop = Haversine(self.temp,Haversine.HasplarBishop)#stores current distance from Hasplar/Bishop intersectiomn self.distanceHM = Haversine(self.temp,Haversine.HasplarMaple)#stores current distance from haslpar/maple intersection self.distanceHQ = Haversine(self.temp,Haversine.HasplarQueen)#stores current distance from hasplar/Queen intersection ''' Storing all the distance in one dictionary ''' self.dict1= {"Homer Block":self.distanceHB.m(),"Fairview Wilson":self.distanceFW.m(),"Hasplar Bishop":self.distanceHBishop.m(),\ "Hasplar Maple":self.distanceHM.m(),"Hasplar Queen":self.distanceHQ.m()} print("_____________________________________________") ''' conditioning for visual alarm ''' if self.distanceHB.m() < threshold or self.distanceFW.m() < threshold or self.distanceHBishop.m() < threshold or \ self.distanceHM.m() < threshold or self.distanceHQ.m() <threshold: print("\nHazardous Area Ahead\n") self.label.setPixmap(QtGui.QPixmap("warning.png")) if self.distanceHB.m() < threshold: self.msgBox.setText(str(data[0])+" "+"Accidents happend at the next intersection") elif self.distanceFW.m() <threshold: self.msgBox.setText(str(data[1])+" "+"Accidents happend at the next intersection") elif self.distanceHBishop.m() < threshold: self.msgBox.setText(str(data[2])+" "+"Accidents happend at the next intersection") elif self.distanceHM.m() <threshold: self.msgBox.setText(str(data[3])+" "+"Accidents happend at the next intersection") elif self.distanceHQ.m() < threshold: self.msgBox.setText(str(data[4])+" "+"Accidents happend at the next intersection") else: print("\n you are safe\n") self.label.setPixmap(QtGui.QPixmap("normal.png")) self.msgBox.setText("You are good to go !!!") ''' Printing all Keys and values from the disctionary on the shell ''' for x in self.dict1: print(self.dict1,"\n") if self.dict1[x]<threshold: print(x,"->",self.dict1[x])
def find_k_nearest_neigh_by_place(self, objective_coor, k, database): dist_loc = [] for i, line in database.iterrows(): dist_loc.append( (i, Haversine(objective_coor, (line['lat'], line['lon'])).meters)) return sorted(dist_loc, key=lambda x: x[1])[:k]
def calculate_mean_offroute(route, points): offroute = [] offroute_directional = [] halfsize = len(points) / 2 for i, point in enumerate(points): start_distance = Haversine((route['start_point']['latitude'], route['start_point']['longitude']), (point.latitude, point.longitude)).meters end_distance = Haversine((point.latitude, point.longitude), (route['end_point']['latitude'], route['end_point']['longitude'])).meters if i < halfsize: offroute_directional.append(start_distance) else: offroute_directional.append(end_distance) offroute.append(start_distance + end_distance) off_route = math.ceil(statistics.mean(offroute) - route['distance']) off_direction = math.ceil(statistics.mean(offroute_directional)) return (off_route, off_direction)
def data_in_desc(input_str, points_gpx): # Straight Line Distance straight_distance_km = 0 if len(points_gpx) > 1: straight_distance_km = Haversine( (points_gpx[0].latitude, points_gpx[0].longitude), (points_gpx[-1].latitude, points_gpx[-1].longitude)).km input_str = input_str.replace("#STRAIGHT_DISTANCE_KM#", "%.1f" % (straight_distance_km)) # Height Difference height_diff = 0 if len(points_gpx) > 1: height_diff = points_gpx[-1].elevation - points_gpx[0].elevation input_str = input_str.replace("#HEIGHT_DIFFERENCE#", "%.1f" % (height_diff)) return input_str
def get_routes(filename): data = None with open(filename, 'r') as json_file: data = json.load(json_file) for route in data['routes']: print("Setting up route '%s'" % (route["name"]), end=" ") route['start_point'] = data['points'][route['start']] route['end_point'] = data['points'][route['end']] route['distance'] = Haversine((route['start_point']['latitude'], route['start_point']['longitude']), (route['end_point']['latitude'], route['end_point']['longitude'])).meters check_radius = route['distance'] * data['proximity_limits']['ratio'] check_radius = min(check_radius, data['proximity_limits']['max']) check_radius = max(check_radius, data['proximity_limits']['min']) route['check_radius'] = check_radius print( "with straight line distance of %.1fkm and a start/end check with a radius of %.1fm" % (route['distance'] / 1000.0, route['check_radius'])) return data
def calculate_fuel_price(self, src_lat, src_lng, dst_lat, dst_lng, mpg=6.5, interval=200): distance = Haversine((src_lat, src_lng), (dst_lat, dst_lng)).miles refuelTimes = int((distance / interval)) latestpercent = interval / distance points = [(src_lat, src_lng)] while refuelTimes > 0: result = self.midpoint(src_lat, src_lng, dst_lat, dst_lng, latestpercent) points.append(result) latestpercent += interval / distance refuelTimes = refuelTimes - 1 results = rg.search(points) state_names = [] for state in results: state_names.append(state["admin1"]) state_codes = [] for state_name in state_names: state = us.states.lookup(state_name) nameState = state.abbr state_codes.append(nameState) gallonsPerInterval = interval / mpg totalCost = 0 for state_code in state_codes: totalCost += gallonsPerInterval * self.savedOutput[str(state_code)] return totalCost
class Ui_MainWindow(): def __init__(self): self.a=Gps() self.setupUi(MainWindow) ''' Setting up UI ''' def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 480) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 0, 800, 480)) self.label.setText("") self.label.setPixmap(QtGui.QPixmap("normal.png")) self.label.setScaledContents(True) self.label.setObjectName("label") self.msgBox = QtWidgets.QLabel(self.centralwidget) self.msgBox.setGeometry(QtCore.QRect(50,10,711,31)) font= QtGui.QFont() font.setPointSize(20) self.msgBox.setFont(font) self.msgBox.setStyleSheet("color:rgb(255,255,255)") self.msgBox.setTextFormat(QtCore.Qt.AutoText) self.msgBox.setScaledContents(True) self.msgBox.setObjectName("msgBox") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) ''' very 50ms this method will get the current distance from the Hazardous area/intersection ''' def getDistance(self): self.temp=self.a.getLocation()# stores current location in self.temp threshold=900.0000 #This is the threshold value in meter that user wants to have visual alarm before he/she enters into the Hazardous area. data = [500,600,700,800,900,1000]#Random numers describing the number of accident occured in listed 5 intersections accordingly if self.temp: self.distanceHB = Haversine(self.temp,Haversine.HomerBlock)#stores current distance from Homerwatson/Block line intersection self.distanceFW = Haversine(self.temp,Haversine.FairwayWilson)#stores current distance from Fairway/Wilson intersection self.distanceHBishop = Haversine(self.temp,Haversine.HasplarBishop)#stores current distance from Hasplar/Bishop intersectiomn self.distanceHM = Haversine(self.temp,Haversine.HasplarMaple)#stores current distance from haslpar/maple intersection self.distanceHQ = Haversine(self.temp,Haversine.HasplarQueen)#stores current distance from hasplar/Queen intersection ''' Storing all the distance in one dictionary ''' self.dict1= {"Homer Block":self.distanceHB.m(),"Fairview Wilson":self.distanceFW.m(),"Hasplar Bishop":self.distanceHBishop.m(),\ "Hasplar Maple":self.distanceHM.m(),"Hasplar Queen":self.distanceHQ.m()} print("_____________________________________________") ''' conditioning for visual alarm ''' if self.distanceHB.m() < threshold or self.distanceFW.m() < threshold or self.distanceHBishop.m() < threshold or \ self.distanceHM.m() < threshold or self.distanceHQ.m() <threshold: print("\nHazardous Area Ahead\n") self.label.setPixmap(QtGui.QPixmap("warning.png")) if self.distanceHB.m() < threshold: self.msgBox.setText(str(data[0])+" "+"Accidents happend at the next intersection") elif self.distanceFW.m() <threshold: self.msgBox.setText(str(data[1])+" "+"Accidents happend at the next intersection") elif self.distanceHBishop.m() < threshold: self.msgBox.setText(str(data[2])+" "+"Accidents happend at the next intersection") elif self.distanceHM.m() <threshold: self.msgBox.setText(str(data[3])+" "+"Accidents happend at the next intersection") elif self.distanceHQ.m() < threshold: self.msgBox.setText(str(data[4])+" "+"Accidents happend at the next intersection") else: print("\n you are safe\n") self.label.setPixmap(QtGui.QPixmap("normal.png")) self.msgBox.setText("You are good to go !!!") ''' Printing all Keys and values from the disctionary on the shell ''' for x in self.dict1: print(self.dict1,"\n") if self.dict1[x]<threshold: print(x,"->",self.dict1[x])
def is_closer_than(current_point, reference_point, meters): distance = Haversine(current_point, reference_point).meters if distance <= meters: return True return False