def generateRouteGraph(self): '''Generate Directed RouteGraph It's a one time job, won't be called by other functions. Only when there is new bus stop added should use this function to update RouteGraph ''' allBusStop = self.db.BusStop.find() busStopNameList = {} tmpList = [] for bs in allBusStop: busStopNameList[bs['name']] = bs['_id'] dictKey = ['busStop', 'condinates', 'distance', '_id', 'timeCost'] busstop = self.db.BusStop.find() for b in busstop: connectedBusStop = [] connectedNameList = [] nearStop = coordinates_to_nearest_stops(float(b['longitude']), float(b['latitude']), DB.connectedBusStopBound) for j in range(len(nearStop['bus_stops'])): if nearStop['bus_stops'][j][0] in busStopNameList.keys() and len(nearStop['bus_stops'][j][0]) > 0 \ and nearStop['bus_stops'][j][0] not in connectedNameList and nearStop['bus_stops'][j][0] != b['name']: condinatesList = [(float(b['longitude']), float(b['latitude'])), (nearStop['bus_stops'][j][1][0], nearStop['bus_stops'][j][1][1])] timeDict = get_route(condinatesList) timeCost = timeDict['cost'][1] timeCostInMin = int( math.ceil(float(timeCost) / float(DB.minutesHour))) tmpList = list(nearStop['bus_stops'][j]) tmpList.append( busStopNameList.get(nearStop['bus_stops'][j][0])) tmpList.append(timeCostInMin) busStopDict = dict(zip(dictKey, tmpList)) connectedBusStop.append(busStopDict) connectedNameList.append(nearStop['bus_stops'][j][0]) objID = ObjectId() orgBusID = busStopNameList.get(b['name']) orgBusName = b['name'] newRoute = { "_id": objID, "orgBusID": orgBusID, "orgBusName": orgBusName, "connectedBusStop": connectedBusStop } #print newRoute self.db.RouteGraph.insert_one(newRoute)
def generateRouteGraph(self): '''Generate Directed RouteGraph It's a one time job, won't be called by other functions. Only when there is new bus stop added should use this function to update RouteGraph ''' allBusStop = self.db.BusStop.find() busStopNameList = {} tmpList = [] for bs in allBusStop: busStopNameList[bs['name']] = bs['_id'] dictKey = ['busStop', 'condinates', 'distance', '_id', 'timeCost'] busstop = self.db.BusStop.find() for b in busstop: connectedBusStop = [] connectedNameList = [] nearStop = coordinates_to_nearest_stops(float(b['longitude']), float(b['latitude']), DB.connectedBusStopBound) for j in range(len(nearStop['bus_stops'])): if nearStop['bus_stops'][j][0] in busStopNameList.keys() and len(nearStop['bus_stops'][j][0]) > 0 \ and nearStop['bus_stops'][j][0] not in connectedNameList and nearStop['bus_stops'][j][0] != b['name']: condinatesList = [(float(b['longitude']), float(b['latitude'])), (nearStop['bus_stops'][j][1][0], nearStop['bus_stops'][j][1][1])] timeDict = get_route(condinatesList) timeCost = timeDict['cost'][1] timeCostInMin = int(math.ceil(float(timeCost)/float(DB.minutesHour))) tmpList = list(nearStop['bus_stops'][j]) tmpList.append(busStopNameList.get(nearStop['bus_stops'][j][0])) tmpList.append(timeCostInMin) busStopDict = dict(zip(dictKey, tmpList)) connectedBusStop.append(busStopDict) connectedNameList.append(nearStop['bus_stops'][j][0]) objID = ObjectId() orgBusID = busStopNameList.get(b['name']) orgBusName = b['name'] newRoute = { "_id": objID, "orgBusID": orgBusID, "orgBusName": orgBusName, "connectedBusStop": connectedBusStop } #print newRoute self.db.RouteGraph.insert_one(newRoute)
def storeRoutesToDB(self,routes): '''store routes(a bus stop id list) from generateRoutes into DB, should be written to collection Routes for timebeing, it be stored into DynamicRoute for testing @param: routes: a list of new dynamic routes in list of bus stop id output: new route will be written into DynamicRoute ''' duration = 0 line = 1 trajectory = [] tmpList = [] todayDate = datetime.datetime.now() today = datetime.datetime(todayDate.year, todayDate.month, todayDate.day) todayRoute = self.dbClass.db.dynamicRoute.find({'date': today}) dictKey = ['interval', 'busStop'] if todayRoute.count() == 0: print 'todays route is writing......' for i in range(len(routes)): for j in range(len(routes[i])): if j == 0: tmpList.append(0) else: adjointBusStopList = [(self.dbClass.getBusStopLongitudeByID(routes[i][j]), self.dbClass.getBusStopLatitudebyID(routes[i][j])), \ (self.dbClass.getBusStopLongitudeByID(routes[i][j-1]), self.dbClass.getBusStopLatitudebyID(routes[i][j-1]))] adjointBusStopRoute = get_route(adjointBusStopList) interval = int(math.ceil(float(adjointBusStopRoute['cost'][1])/float(self.dbClass.minutesHour))) tmpList.append(interval) duration += interval tmpList.append(routes[i][j]) trajectoryDict = dict(zip(dictKey, tmpList)) tmpList = [] trajectory.append(trajectoryDict) objID = ObjectId() route = {"_id": objID, "trajectory": trajectory, "date": today, "duration": duration, "line": line} self.dbClass.db.DynamicRoute.insert_one(route) line += 1 duration = 0 tmpList = [] trajectory = []