def checkNonMatchingTrust(segment, gps_report): closest = { 'match': None, 'time_error': datetime.timedelta(days=1), 'dist_error': 1000000 } for match in segment.matching: if (match['trust'] is not None) and (match['gps'] is None): time_error = abs( match['trust']['event_time'] - gps_report['event_time']) dist_error = geo_distance.calculateDistance( match['trust']['tiploc'], gps_report['tiploc'] ) if dist_error < globals.tolerance['distance']\ and time_error < globals.tolerance['time']: if dist_error < closest['dist_error'] and\ time_error < closest['time_error']: closest['match'] = match closest['dist_error'] = dist_error closest['time_error'] = time_error if closest['match'] is not None: closest['match']['gps'] = gps_report closest['match']['dist_error'] = dist_error if segment.gps_car_id is None: segment.gps_car_id = gps_report['gps_car_id'] segment.isPlanned = db_queries.isPlanned(segment.gps_car_id, segment.headcode) socket_io.emitSegment('update', segment) return True return False
def createNewSegment(report): segment = Segment() if 'planned_pass' in report.keys(): # Trust has planned pass segment.trust(report) else: segment.gps(report) segments[segment.id] = segment socket_io.emitSegment('new', segment)
def deleteReports(segment): segment.matching = filter(lambda x: not x['delete'], segment.matching[-globals.backmatches:]) + segment.matching[:-globals.backmatches] if len(segment.matching) == 0: socket_io.emitSegment('delete', segment.id) del globals.segments[segment.id] else: socket_io.emitSegment('update', segment)
def combineSegments(segments, i, j): for k in range(i + 1, j + 1): if segments[k].gps_car_id != segments[i].gps_car_id: removeGPSReports(segments[k], segments[i].gps_car_id) segments[i].matching.extend(segments[k].matching) segments[k].remove = True segments[i].matching.sort(key=lambda x: getReportTime(x), reverse=False) socket_io.emitSegment('update', segments[i])
def checkPotentialMatches(segment, potential_matches): if len(potential_matches) > globals.min_matching: globals.lock.acquire() segment.headcode = potential_matches[0]['trust']['headcode'] segment.cif_uid = db_queries.cif_uidFromHeadcode(segment.headcode) segment.origin_location = potential_matches[0]['trust']['origin_location'] segment.origin_departure = potential_matches[0]['trust']['origin_departure'] for match in potential_matches: segment.matching[match['index']]['trust'] = match['trust'] socket_io.emitSegment('update', segment) globals.lock.release()
def checkPotentialMatches(segment, potential_matches): if len(potential_matches) > globals.min_matching: globals.lock.acquire() segment.headcode = potential_matches[0]["trust"]["headcode"] segment.cif_uid = db_queries.cif_uidFromHeadcode(segment.headcode) segment.origin_location = potential_matches[0]["trust"]["origin_location"] segment.origin_departure = potential_matches[0]["trust"]["origin_departure"] for match in potential_matches: segment.matching[match["index"]]["trust"] = match["trust"] socket_io.emitSegment("update", segment) globals.lock.release()
def findEmptySegment(segments1, trust): segments = filter(lambda x: x['segment'].gps_car_id is None, segments1) if len(segments) > 0: segments.sort(key=lambda x: isBetterSegment(x), reverse=True) segment = segments[0]['segment'] segment.matching.append({ 'dist_error': None, 'gps': None, 'trust': trust }) socket_io.emitSegment('update', segment) return True return False
def checkEmptyMatches(segment, empty_segment, potential_matches): if len(potential_matches) > globals.min_matching: for i, match in enumerate(potential_matches): if "index" in match: # It can match to a gps dist_error = geo_distance.calculateDistance( match["trust"]["tiploc"], segment.matching[match["index"]]["gps"]["tiploc"] ) segment.matching[match["index"]]["trust"] = match["trust"] segment.matching[match["index"]]["dist_error"] = dist_error del potential_matches[i] for match in potential_matches: segment.matching.append({"dist_err": None, "gps": None, "trust": match["trust"]}) socket_io.emitSegment("update", segment) deleteReports(empty_segment)
def checkEmptyMatches(segment, empty_segment, potential_matches): if len(potential_matches) > globals.min_matching: for i, match in enumerate(potential_matches): if 'index' in match: # It can match to a gps dist_error = geo_distance.calculateDistance( match['trust']['tiploc'], segment.matching[match['index']]['gps']['tiploc']) segment.matching[match['index']]['trust'] = match['trust'] segment.matching[match['index']]['dist_error'] = dist_error del potential_matches[i] for match in potential_matches: segment.matching.append({ 'dist_err': None, 'gps': None, 'trust': match['trust'] }) socket_io.emitSegment('update', segment) deleteReports(empty_segment)
def addGPS(gps_report): if gps_report is None: return globals.lock.acquire() segment = findClosestSegment(globals.segments, gps_report) if segment is None: globals.createNewSegment(gps_report) elif not checkNonMatchingTrust(segment, gps_report): segment = findClosestGPSSegment(globals.segments, gps_report) if segment is None: globals.createNewSegment(gps_report) else: segment.matching.append({ 'gps': gps_report, 'trust': None, 'dist_error': None }) socket_io.emitSegment('update', segment) globals.lock.release()
def getBestStop(segments1, trust, with_seq): segments = filter(lambda x: x['segment'].gps_car_id is not None, segments1) segments.sort(key=lambda x: isBetterSegment(x), reverse=True) for segment1 in segments: segment = segment1['segment'] segment.matching.sort(key=lambda x: getTimeForMatching(x), reverse=True) matching = segment.matching[0: globals.backmatches] best = getBestStopHelper(matching, trust, True) if best is not None: best['match']['trust'] = trust best['match']['dist_error'] = best['dist_error'] if segment.headcode is None: segment.headcode = trust['headcode'] segment.cif_uid = db_queries.cif_uidFromHeadcode(trust['headcode']) segment.isPlanned = db_queries.isPlanned(segment.gps_car_id, trust['headcode']) segment.origin_location = trust['origin_location'] segment.origin_departure = trust['origin_departure'] socket_io.emitSegment('update', segment) return True return False
def removeSegments(): for key, segment in globals.segments.items(): if segment.remove: socket_io.emitSegment('delete', key) del globals.segments[key]