def test_matches(self, mock_urlopen): mock_urlopen.return_value = MockReadable( u'''{"code":"Ok","matchings":[{"confidence":0,"distance":8,"duration":1.1,"geometry":"g|j_Goro_CEO","legs":[{"steps":[],"summary":"","duration":1.1,"distance":8}]}],"tracepoints":[{"waypoint_index":0,"location":[21.05656,42.004042],"name":"","hint":"_BmZBOtFnIQAAAAABQAAABUAAAAAAAAAbQoAAIGx7AN-sewDbrcAADBMQQFK7oACWExBASDugAIAAAEBfDhq3w==","matchings_index":0},{"waypoint_index":1,"location":[21.056638,42.004072],"name":"","hint":"-xmZhP8ZmQQAAAAAAAAAABsAAAAAAAAAXwYAAAOkLAMBpCwDbrcAAH5MQQFo7oACnkxBAYTugAIAAAEBfDhq3w==","matchings_index":0}]}''' ) coords = [[21.0566, 42.0040], [21.05667, 42.0041]] result = osrm.match(coords) self.assertIn("matchings", result)
def test_non_existing_host(self): Profile = osrm.RequestConfig("localhost/v1/flying") self.assertEqual(Profile.host, "localhost") with self.assertRaises(URLError): osrm.nearest((12.36, 45.36), url_config=Profile) with self.assertRaises(URLError): osrm.trip([(13.38886, 52.51703), (10.00, 53.55), (52.374444, 9.738611)], url_config=Profile) with self.assertRaises(URLError): osrm.simple_route((13.38886, 52.51703), (10.00, 53.55), url_config=Profile) with self.assertRaises(URLError): osrm.AccessIsochrone((13.38886, 52.51703), points_grid=100, url_config=Profile) with self.assertRaises(URLError): osrm.match([(10.00, 53.55), (52.374444, 9.738611)], url_config=Profile) with self.assertRaises(URLError): osrm.table([(10.00, 53.55), (52.374444, 9.738611)], [(10.00, 53.55), (52.374444, 9.738611)], url_config=Profile)
def test_non_existing_host(self): Profile = osrm.RequestConfig("localhost/v1/flying") self.assertEqual(Profile.host, "localhost") with self.assertRaises(URLError): osrm.nearest((12.36, 45.36), url_config=Profile) with self.assertRaises(URLError): osrm.trip( [(13.38886, 52.51703), (10.00, 53.55), (52.374444, 9.738611)], url_config=Profile) with self.assertRaises(URLError): osrm.simple_route( (13.38886, 52.51703), (10.00, 53.55), url_config=Profile) with self.assertRaises(URLError): osrm.AccessIsochrone( (13.38886, 52.51703), points_grid=100, url_config=Profile) with self.assertRaises(URLError): osrm.match( [(10.00, 53.55), (52.374444, 9.738611)], url_config=Profile) with self.assertRaises(URLError): osrm.table( [(10.00, 53.55), (52.374444, 9.738611)], [(10.00, 53.55), (52.374444, 9.738611)], url_config=Profile)
def route_osrm_py(points): duration = 0 result = osrm.match(points, steps=False, overview="full") tmp = result[u"total_time"] duration = round(float(tmp) / 60.0) return int(duration)
def TaxiNetworkResults_T(combined_taxidf, T, min_dist, T_search_margin, t_accept): # split enormous dataframe... to values within T+/- T_search_margin #taxidf = pd.DataFrame() taxidf = combined_taxidf[(combined_taxidf.unix_ts < T + T_search_margin) & (combined_taxidf.unix_ts >= T - T_search_margin)] taxi_ids_before_T = taxidf[taxidf.unix_ts < T].taxi_id.unique() taxi_ids_after_T = taxidf[taxidf.unix_ts >= T].taxi_id.unique() #taxi_ids2process = set(taxi_ids_after_T).intersection(taxi_ids_before_T) taxi_ids_not2process = list( set(taxi_ids_after_T).symmetric_difference(taxi_ids_before_T)) if len(taxi_ids_not2process) > 0: for taxi_id_2drop in taxi_ids_not2process: taxidf.drop(taxidf[taxidf.taxi_id == taxi_id_2drop].index) # this ratio is interesting, early estimates suggest window length of 30s (~91%, # maybe better to have 1min , 60s long windows (accept_ratio ~1ish # needs more investigation print('reject ratio = %f' % float(len(taxi_ids_not2process) / len(taxidf.taxi_id.unique()))) reject_ratio = (len(taxi_ids_not2process) / len(taxidf.taxi_id.unique())) taxis_Tpos = [] taxis_Tids = [] taxi_ids_to_process = taxidf.taxi_id.unique().tolist() #for each trace, another loop here... for taxi_id in taxi_ids_to_process: #taxi_pos_estimate = [] #taxi_id = taxidf.taxi_id[31] # map match. taxi_subset = taxidf[taxidf.taxi_id == taxi_id].sort_values('unix_ts') timestamps2match = taxi_subset.unix_ts.tolist() taxi_pos2match = [ tuple(x) for x in taxi_subset[['longitude', 'latitude']].values ] matched_points = osrm.match(taxi_pos2match, overview="simplified", timestamps=timestamps2match, radius=None) # error checking.. if type(matched_points ) is str: # implies no points were matched, hence ditch... taxi_pos_estimate = None #snapped_subset = Snap2Road(taxi_subset) #matchedf = pd.DataFrame({'mpos':snapped_subset.snap_pos, 'mts':snapped_subset.unix_ts}) else: matchedf, nobody_index = ProcessMapMatchResults( matched_points, timestamps2match) # quickly remove those where taxi_ts = T if (any(matchedf['mts'] == T)) and (bool( np.isnan(matchedf[matchedf.mts == T].mpos.values[0][0])) is False): #taxi_TDF.append([matchedf[matchedf.mts==T]]) taxi_pos_estimate = matchedf[matchedf.mts == T].mpos.tolist()[0] else: # route&/interp #taxi_pos_estimate = RouteAndInterp(matchedf,T,min_dist) matchedf['ts_dff'] = matchedf['mts'] - T adf = matchedf[matchedf['ts_dff'] > 0].min() bdf = matchedf[matchedf['ts_dff'] < 0].max() #if map-matching doesn't work... # complete fail? --> snap coords instead, then route? # for now, people just do nothing if (adf.isnull().any() == True) or (bdf.isnull().any() == True): taxi_pos_estimate = None # [np.nan] #tuple([np.nan,np.nan]) #if (taxi_pos_estimate is not None) and ((bool(np.isnan(adf.mpos[0])) is True) or (bool(np.isnan(bdf.mpos[1])) is True)): taxi_pos_estimate = None #if adf.isnull().any()==True and len(nobody_index)>0: #taxi_subset[taxi_subset.unix_ts>T] # maybe just snap? then again routing might be a bitch. else: d = haversine_pc(adf.mpos[0], adf.mpos[1], bdf.mpos[0], bdf.mpos[1]) #if adf.mpos == bdf.mpos: if d <= min_dist: taxi_pos_estimate = adf.mpos else: osrm_route_result = osrm.simple_route( [bdf.mpos[0], bdf.mpos[1]], [adf.mpos[0], adf.mpos[1]], output='full', overview="full", geometry='polyline', steps='True', annotations='true') if type(osrm_route_result) is str: taxi_pos_estimate = None else: link_data, route_nodes = ProcessRouteResults( osrm_route_result, bdf.mts, adf.mts) #maybe another if statement, if link_data.dur_cumsum == T: ..., else: if any(link_data.dur_cumsum < T): T_index = max(link_data[ link_data['dur_cumsum'] <= T].index.tolist()) else: T_index = 0 x1 = route_nodes['longitude'][T_index] y1 = route_nodes['latitude'][T_index] if T_index == 0: t1 = link_data['dur_cumsum'][ 0] - link_data.duration[T_index] else: t1 = link_data['dur_cumsum'][T_index - 1] x2 = route_nodes['longitude'][T_index + 1] y2 = route_nodes['latitude'][T_index + 1] t2 = link_data['dur_cumsum'][T_index] T_longitude, T_latitude = Straight_Line_Interp( x1, y1, t1, x2, y2, t2, T) taxi_pos_estimate = tuple([T_longitude, T_latitude]) if taxi_pos_estimate is not None and (bool( np.isnan(taxi_pos_estimate[0])) is False) and (bool( np.isinf(taxi_pos_estimate)[0]) is False): taxis_Tpos.append(taxi_pos_estimate) taxis_Tids.append(taxi_id) print('succesfull spatial estimation = %f' % (float(len(taxis_Tids) / len(taxi_ids_to_process)))) reject_taxis_pos = (1 - float(len(taxis_Tids) / len(taxi_ids_to_process))) min_los_length = 0 max_los_length = 100 #same as f*****g paper. #def TaxisWithinNOLOSRange(input_gps_pos,taxis_Tids,max_los_length,min_los_length): input_gps_pos = taxis_Tpos # accpets tupple list of positions: [(long1,lat1),(long2,lat2),... etc.] # creates a haversine distance matrix, then finds pairs of taxis that are within # main_los_length and max_los_length, typical values [0,100] # outputs list of taxi_id pairs and their respective haversine distasnces between them # [(taxi_id_A,taxi_id_B,t,haversine_distance)] mat_length = len(input_gps_pos) Hdist_matrix = np.zeros((mat_length, mat_length), dtype=int) taxis_nolos = [] #queck2 = [] for row in range(0, mat_length): for col in range(0, row): Hdist = HaversineDistPC2(input_gps_pos[row], input_gps_pos[col]) Hdist_matrix[row, col] = Hdist if (Hdist > min_los_length) & (Hdist < max_los_length): taxis_nolos.append( (taxis_Tids[row], taxis_Tids[col], input_gps_pos[row], input_gps_pos[col], Hdist)) #queck2.append([row,col]) # Line of Sight Model: num_buildings = [] for i in range(len(taxis_nolos)): #i=0 #longitude,latitude in query LoS_execution_str = ( "SELECT * FROM rome_buildings WHERE ST_Intersects(ST_SetSRID('LINESTRING (%s %s, %s %s)'::geometry,4326), geom);" % (str(taxis_nolos[i][2][0]), str(taxis_nolos[i][2][1]), str(taxis_nolos[i][3][0]), str(taxis_nolos[i][3][1]))) LoS_df = pdsql.read_sql_query(LoS_execution_str, connection) num_buildings.append(len(LoS_df)) if len(taxis_nolos) > 0: taxiAid, taxiBid, Alonglat, Blonglat, Hdist = zip(*taxis_nolos) RESULT_DF = pd.DataFrame({ 'taxiAid': taxiAid, 'taxiBid': taxiBid, 'Alonglat': Alonglat, 'Blonglat': Blonglat, 'Hdist': Hdist, 'num_buildings': num_buildings }) #print('current search time: %i iterations left: %f' % (T, ((T_search_times[-1]-T)/T_search_margin))) return RESULT_DF, reject_ratio, reject_taxis_pos
def getDistances(viaggio): ''' Recupera la distanza tra i punti che compongono il viaggio dalla API osrm. :type viaggio: Viaggio :param viaggio: Il Viaggio del quale si vogliono calcolare le distanze tra punti ''' osrm.RequestConfig.host = "localhost:5000" blocksize = chooseBlockSize(viaggio) distances = [] steps = [] tracepoints = [] totalDistance = 0 coords = viaggio.getCoordsOsrm() nblocks = math.floor(float(len(coords) - 1) / blocksize) k = 0 lastperc = 0 # while k <= nblocks: perc = int(float(k + 1) / (nblocks + 1) * 100) # block = [] if lastperc != perc: # print u"Ottengo distanze tra i punti:", perc, "%\r", # sys.stdout.flush() # lastperc = perc # if k == 0: block = coords[k * blocksize:(k + 1) * blocksize] elif k < nblocks: block = coords[k * blocksize - 1:(k + 1) * blocksize] elif k == nblocks: block = coords[k * blocksize - 1:] if len(block) > 1: try: result = osrm.match(block) #if k==0: print result except urllib2.HTTPError as e: print e print u"C'è stato un errore durante il matching dei punti" return -1 tempSteps = [] nmatchings = len(result["matchings"]) i = 0 while i < nmatchings: totalDistance = totalDistance + ( result["matchings"][i]["distance"]) tempSteps.append( {"distance": 0} ) #il primo punto di ogni matching ha distanza dal precedente = 0 tempSteps.extend(result["matchings"][i]["legs"]) i = i + 1 steps.extend(tempSteps) tempTraces = result["tracepoints"] nnulltrace = 0 for trace in tempTraces: if trace == None: nnulltrace += 1 #print "Lung. blocco",len(block),"== Lung. traces",len(tempTraces),"?",len(block)==len(tempTraces) #print "Lung. steps",len(tempSteps),"== Lung. traces",len(tempTraces),"- tracce nulle",nnulltrace,"?",len(tempSteps)==len(tempTraces)-nnulltrace if k > 0: tempTraces.pop( 0 ) #RIMUOVE LA PRIMA TRACCIA POICHE' SI RIFERISCE A UNA DISTANZA GIA' CONTATA tracepoints.extend(tempTraces) else: print u"processing.getDistances: Non dovresti MAI entrare qui, la tua logica è bacata!" k = k + 1 print "" # nsteps = len(steps) nonecount = 0 for tracepoint in tracepoints: if tracepoint == None: nonecount = nonecount + 1 distances.append(-4) else: if len(steps) > 0: distances.append(steps.pop(0)["distance"]) else: distances.append(-5) #print u"npunti:",len(coords),"\nntracepoints:",len(tracepoints),"\nnnone:",nonecount,"\nnsteps:",nsteps #print distances return (totalDistance, distances)