def google_walking_driving_path(start, dest, city_id, dist): start_map = start.split('#')[-1] dest_map = dest.split('#')[-1] real_start_map = formatCoor(start_map) real_dest_map = formatCoor(dest_map) ssdb_key_walking = "###".join(["walking", real_start_map, real_dest_map]) ssdb_key_driving = "###".join(["driving", real_start_map, real_dest_map]) if dist < 3000: path_walking = ssdb.request("get", [ssdb_key_walking]) path_walking_str = str(path_walking) if path_walking_str == "d None None" or path_walking_str == "not_found None None": path_driving = ssdb.request("get", [ssdb_key_driving]) path_driving_str = str(path_driving) #google步行和驾车数据都为空,拉一条直线路径 if path_driving_str == "d None None" or path_driving_str == "not_found None None": return line_walk_path(start, dest, city_id, dist) else: return path_driving_str[8:] else: return path_walking_str[8:] else: path_driving = ssdb.request("get", [ssdb_key_driving]) path_driving_str = str(path_driving) if path_driving_str == "d None None" or path_driving_str == "not_found None None": path_walking = ssdb.request("get", [ssdb_key_walking]) path_walking_str = str(path_walking) #google步行和驾车数据都为空时,拉一条直线距离 if path_walking_str == "d None None" or path_walking_str == "not_found None None": return line_walk_path(start, dest, city_id, dist) else: return path_walking_str[8:] else: return path_driving_str[8:]
def google_walk_path(start, dest): #start_list = start.split('#') #dest_list = dest.split('#') #real_start_map = formatCoor(start_list[len(start_list) - 1]) #real_dest_map = formatCoor(dest_list[len(dest_list) - 1]) real_start_map = formatCoor(start.split('#')[-1]) real_dest_map = formatCoor(dest.split('#')[-1]) ssdb_key = "###".join(['walking', real_start_map, real_dest_map]) path = ssdb.request('get', [ssdb_key]) return path
def google_driving_path(start, dest): start_map = start.split('#')[-1] dest_map = dest.split('#')[-1] real_start_map = formatCoor(start_map) real_dest_map = formatCoor(dest_map) ssdb_key_driving = "###".join(["driving", real_start_map, real_dest_map]) path_driving = ssdb.request("get", [ssdb_key_driving]) path_driving_str = str(path_driving) if path_driving_str == "d None None" or path_driving_str == "not_found None None": return "" return path_driving_str[8:]
def find_path_sub(start, dest, start_type, dest_type, start_nearest_station, dest_nearest_station, city_id): #start_list = start.split('#') #dest_list = dest.split('#') #start_poi_map = start_list[len(start_list) - 1] #dest_poi_map = dest_list[len(dest_list) - 1] start_poi_map = start.split('#')[-1] dest_poi_map = dest.split('#')[-1] strKey_list = [] #起始和终止poi点附近所有交通站点的组合,方便从redis数据库中批量读取数据 for start_station in start_nearest_station: start_map = start_station[0].split('#')[-1] for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] if start_map != dest_map: strKey = start_map.strip() + "###" + dest_map.strip() + "@0@0" strKey_list.append(strKey) #批量读取交通路线数据 strValue_list = r0.mget(strKey_list) path_list = {} #读取的数据拼接成字典形式 for index in range(len(strValue_list)): path_list[strKey_list[index]] = strValue_list[index] min_score = sys.maxint res_path = {} start_path_str_res = "" dest_path_str_res = "" path_str_res = "" start_t = time.time() #批量获取起始poi点到附近交通站点的Google交通数据(步行数据和驾车数据) start_walking_ssdbkey_list = [] start_driving_ssdbkey_list = [] for start_station in start_nearest_station: start_map = start_station[0].split('#')[-1] real_start_map = formatCoor(start_poi_map) real_dest_map = formatCoor(start_map) ssdb_key_walking = "###".join( ["walking", real_start_map, real_dest_map]) ssdb_key_driving = "###".join( ["driving", real_start_map, real_dest_map]) start_driving_ssdbkey_list.append(ssdb_key_driving) start_walking_ssdbkey_list.append(ssdb_key_walking) start_walking_path_str = str( ssdb.request('multi_get', start_walking_ssdbkey_list)) start_driving_path_str = str( ssdb.request('multi_get', start_driving_ssdbkey_list)) start_walking_path_str = start_walking_path_str[8:] start_driving_path_str = start_driving_path_str[8:] start_walking_path_dic = eval(start_walking_path_str) start_driving_path_dic = eval(start_driving_path_str) start_driving_path_dic = start_driving_path_dic["items"] start_walking_path_dic = start_walking_path_dic["items"] #批量获取终止poi点到附近交通站点的Google交通数据(步行数据和驾车数据) dest_walking_ssdbkey_list = [] dest_driving_ssdbkey_list = [] for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] real_start_map = formatCoor(dest_map) real_dest_map = formatCoor(dest_poi_map) ssdb_key_walking = "###".join( ["walking", real_start_map, real_dest_map]) ssdb_key_driving = "###".join( ["driving", real_start_map, real_dest_map]) dest_walking_ssdbkey_list.append(ssdb_key_walking) dest_driving_ssdbkey_list.append(ssdb_key_driving) dest_walking_path_str = str( ssdb.request('multi_get', dest_walking_ssdbkey_list)) dest_driving_path_str = str( ssdb.request('multi_get', dest_driving_ssdbkey_list)) dest_walking_path_str = dest_walking_path_str[8:] dest_driving_path_str = dest_driving_path_str[8:] dest_walking_path_dic = eval(dest_walking_path_str) dest_driving_path_dic = eval(dest_driving_path_str) dest_walking_path_dic = dest_walking_path_dic["items"] dest_driving_path_dic = dest_driving_path_dic["items"] end_t = time.time() #print "ssdb mutli_get : ",end_t - start_t #拼接Google获取的交通数据和交通站点的交通数据进行拼接 start_t = time.time() for start_station in start_nearest_station: start_map = start_station[0].split('#')[-1] start_real_start_map = formatCoor(start_poi_map) start_real_dest_map = formatCoor(start_map) start_dis = CoordDist.getDistance( start_poi_map.split(',') + start_map.split(',')) start_path = "" #两点之间的直线距离小于3000m时,优先选择Google步行数据,如果Google步行数据为空,则选择驾车数据,如果驾车数据也为空,则在两点之间拉一条直线 if start_dis <= 3000: ssdb_key_walking = "###".join( ["walking", start_real_start_map, start_real_dest_map]) start_path = start_walking_path_dic.get(ssdb_key_walking) if start_path is None: ssdb_key_driving = "###".join( ["driving", start_real_start_map, start_real_dest_map]) start_path = start_driving_path_dic.get(ssdb_key_driving) if start_path is None: start_path = line_walk_path(start, start_station[0], city_id, start_dis) else: ssdb_key_driving = "###".join( ["driving", start_real_start_map, start_real_dest_map]) start_path = start_driving_path_dic.get(ssdb_key_driving) if start_path is None: ssdb_key_walking = "###".join( ["walking", start_real_start_map, start_real_dest_map]) start_path = start_walking_path_dic.get(ssdb_key_walking) if start_path is None: start_path = line_walk_path(start, start_station[0], city_id, start_dis) #start_path = google_walking_driving_path(start_poi_map,start_map,start_dis) #google返回的数据为空,则在两个poi点之间拉一条直线 #if start_path == "": # start_path = line_walk_path(start,start_station[0],city_id,start_dis) for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] dest_real_start_map = formatCoor(dest_map) dest_real_dest_map = formatCoor(dest_poi_map) dest_dis = CoordDist.getDistance( dest_poi_map.split(',') + dest_map.split(',')) dest_path = "" if dest_dis <= 3000: ssdb_key_walking = "###".join( ["walking", dest_real_start_map, dest_real_dest_map]) dest_path = dest_walking_path_dic.get(ssdb_key_walking) if dest_path is None: ssdb_key_driving = "###".join( ["driving", dest_real_start_map, dest_real_dest_map]) dest_path = dest_driving_path_dic.get(ssdb_key_driving) if dest_path is None: dest_path = line_walk_path(dest_station[0], dest, city_id, dest_dis) else: ssdb_key_driving = "###".join( ["driving", dest_real_start_map, dest_real_dest_map]) dest_path = dest_driving_path_dic.get(ssdb_key_driving) if dest_path is None: ssdb_key_walking = "###".join( ["walking", dest_real_start_map, dest_real_dest_map]) dest_path = dest_walking_path_dic.get(ssdb_key_walking) if dest_path is None: dest_path = line_walk_path(dest_station[0], dest, city_id, dest_dis) #dest_path = google_walking_driving_path(dest_map,dest_poi_map,dest_dis) #google返回的数据为空,则在两个poi点之间拉一条直线 #if dest_path == "": # dest_path = line_walk_path(dest_station[0],dest,city_id,dest_dis) if start_map != dest_map: strKey = start_map.strip() + "###" + dest_map.strip() + "@0@0" #pathStr = r0.get(strKey) pathStr = path_list[strKey] #两个交通站点之间没有计算出路径信息,则选择google步行数据 if pathStr is None: path_dis = CoordDist.getDistance( start_map.split(',') + dest_map.split(',')) pathStr = google_walking_driving_path( start_station[0], dest_station[0], city_id, path_dis) start_path_dic = eval(start_path) dest_path_dic = eval(dest_path) try: path_dic = eval(pathStr) except: print strKey tmp_score = int(path_dic["score"]) if start_type.strip() == "1": tmp_score += int(start_path_dic["time"]) if dest_type.strip() == "1": tmp_score += int(dest_path_dic["time"]) #path_dic = combine_path(start_path,dest_path,start_type,dest_type,pathStr) if tmp_score < min_score: start_path_str_res = start_path dest_path_str_res = dest_path path_str_res = pathStr min_score = tmp_score end_t = time.time() #print "for time : ",end_t - start_t res_path = combine_path(start_path_str_res, dest_path_str_res, start_type, dest_type, path_str_res) res_path["start_id"] = start res_path["dest_id"] = dest res_path["start_map"] = start.split('#')[-1] res_path["dest_map"] = dest.split("#")[-1] #print res_path return str(res_path)
def find_path_sub_0_1(start, dest, dest_nearest_station, city_id, pathes_dic): start_map = start.split('#')[-1] dest_poi_map = dest.split('#')[-1] strKey_list = [] #起始和终止poi点附近所有交通站点的组合,方便从redis数据库中批量读取数据 for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] if start_map != dest_map: strKey = start_map.strip() + "###" + dest_map.strip() + "@0@0" strKey_list.append(strKey) #批量读取交通路线数据 strValue_list = r0.mget(strKey_list) path_list = {} #读取的数据拼接成字典形式 for index in range(len(strValue_list)): path_list[strKey_list[index]] = strValue_list[index] min_score = sys.maxint res_path = {} dest_path_str_res = "" path_str_res = "" #批量获取终止poi点到附近交通站点的Google交通数据(步行数据和驾车数据) dest_walking_ssdbkey_list = [] dest_driving_ssdbkey_list = [] for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] real_start_map = formatCoor(dest_map) real_dest_map = formatCoor(dest_poi_map) ssdb_key_walking = "###".join( ["walking", real_start_map, real_dest_map]) ssdb_key_driving = "###".join( ["driving", real_start_map, real_dest_map]) dest_walking_ssdbkey_list.append(ssdb_key_walking) dest_driving_ssdbkey_list.append(ssdb_key_driving) dest_walking_path_str = str( ssdb.request('multi_get', dest_walking_ssdbkey_list)) dest_driving_path_str = str( ssdb.request('multi_get', dest_driving_ssdbkey_list)) dest_walking_path_str = dest_walking_path_str[8:] dest_driving_path_str = dest_driving_path_str[8:] dest_walking_path_dic = eval(dest_walking_path_str) dest_driving_path_dic = eval(dest_driving_path_str) dest_walking_path_dic = dest_walking_path_dic["items"] dest_driving_path_dic = dest_driving_path_dic["items"] #拼接Google获取的交通数据和交通站点的交通数据进行拼接 for dest_station in dest_nearest_station: dest_map = dest_station[0].split('#')[-1] dest_real_start_map = formatCoor(dest_map) dest_real_dest_map = formatCoor(dest_poi_map) dest_dis = CoordDist.getDistance( dest_poi_map.split(',') + dest_map.split(',')) dest_path = "" if dest_dis <= 3000: ssdb_key_walking = "###".join( ["walking", dest_real_start_map, dest_real_dest_map]) dest_path = dest_walking_path_dic.get(ssdb_key_walking) if dest_path is None: ssdb_key_driving = "###".join( ["driving", dest_real_start_map, dest_real_dest_map]) dest_path = dest_driving_path_dic.get(ssdb_key_driving) if dest_path is None: dest_path = line_walk_path(dest_station[0], dest, city_id, dest_dis) else: ssdb_key_driving = "###".join( ["driving", dest_real_start_map, dest_real_dest_map]) dest_path = dest_driving_path_dic.get(ssdb_key_driving) if dest_path is None: ssdb_key_walking = "###".join( ["walking", dest_real_start_map, dest_real_dest_map]) dest_path = dest_walking_path_dic.get(ssdb_key_walking) if dest_path is None: dest_path = line_walk_path(dest_station[0], dest, city_id, dest_dis) dest_path_dic = eval(dest_path) if start_map != dest_map: strKey = start_map.strip() + "###" + dest_map.strip() + "@0@0" pathStr = path_list[strKey] #两个交通站点之间没有计算出路径信息,则选择google步行数据 if pathStr is None: path_dis = CoordDist.getDistance( start_map.split(',') + dest_map.split(',')) pathStr = google_walking_driving_path(start_station[0], dest_station[0], city_id, path_dis) path_dic = eval(pathStr) tmp_score = int(path_dic["score"]) + int(dest_path_dic["time"]) if tmp_score < min_score: dest_path_str_res = dest_path path_str_res = pathStr min_score = tmp_score else: tmp_score = int(dest_path_dic["time"]) if tmp_score < min_score: dest_path_str_res = dest_path path_str_res = "" min_score = tmp_score if path_str_res == "": res_path = eval(dest_path_str_res) else: res_path = combine_path("", dest_path_str_res, "0", "1", path_str_res) res_path["start_id"] = start res_path["dest_id"] = dest res_path["start_map"] = start.split('#')[-1] res_path["dest_map"] = dest.split("#")[-1] str_res_key = start_map + "###" + dest_poi_map + "@0@0" pathes_dic[str_res_key] = str(res_path) #print res_path return str(res_path)
def find_path_sub_1_0(start, start_nearest_station, dest, city_id, pathes_dic): start_poi_map = start.split('#')[-1] dest_poi_map = dest.split('#')[-1] min_score = sys.maxint res_path = {} start_path_str_res = "" path_str_res = "" #批量获取起始poi点到附近交通站点的Google交通数据(步行数据和驾车数据) start_walking_ssdbkey_list = [] start_driving_ssdbkey_list = [] for start_station in start_nearest_station: start_map = start_station[0].split('#')[-1] real_start_map = formatCoor(start_poi_map) real_dest_map = formatCoor(start_map) ssdb_key_walking = "###".join( ["walking", real_start_map, real_dest_map]) ssdb_key_driving = "###".join( ["driving", real_start_map, real_dest_map]) start_driving_ssdbkey_list.append(ssdb_key_driving) start_walking_ssdbkey_list.append(ssdb_key_walking) start_walking_path_str = str( ssdb.request('multi_get', start_walking_ssdbkey_list)) start_driving_path_str = str( ssdb.request('multi_get', start_driving_ssdbkey_list)) start_walking_path_str = start_walking_path_str[8:] start_driving_path_str = start_driving_path_str[8:] start_walking_path_dic = eval(start_walking_path_str) start_driving_path_dic = eval(start_driving_path_str) start_driving_path_dic = start_driving_path_dic["items"] start_walking_path_dic = start_walking_path_dic["items"] #拼接Google获取的交通数据和交通站点的交通数据进行拼接 for start_station in start_nearest_station: start_map = start_station[0].split('#')[-1] start_real_start_map = formatCoor(start_poi_map) start_real_dest_map = formatCoor(start_map) start_dis = CoordDist.getDistance( start_poi_map.split(',') + start_map.split(',')) start_path = "" #两点之间的直线距离小于3000m时,优先选择Google步行数据,如果Google步行数据为空,则选择驾车数据,如果驾车数据也为空,则在两点之间拉一条直线 if start_dis <= 3000: ssdb_key_walking = "###".join( ["walking", start_real_start_map, start_real_dest_map]) start_path = start_walking_path_dic.get(ssdb_key_walking) if start_path is None: ssdb_key_driving = "###".join( ["driving", start_real_start_map, start_real_dest_map]) start_path = start_driving_path_dic.get(ssdb_key_driving) if start_path is None: start_path = line_walk_path(start, start_station[0], city_id, start_dis) else: ssdb_key_driving = "###".join( ["driving", start_real_start_map, start_real_dest_map]) start_path = start_driving_path_dic.get(ssdb_key_driving) if start_path is None: ssdb_key_walking = "###".join( ["walking", start_real_start_map, start_real_dest_map]) start_path = start_walking_path_dic.get(ssdb_key_walking) if start_path is None: start_path = line_walk_path(start, start_station[0], city_id, start_dis) dest_map = dest.split('#')[-1] start_path_dic = eval(start_path) if start_map != dest_map: strKey = start_map + "###" + dest_map + "@0@0" pathStr = pathes_dic[strKey] path_dic = eval(pathStr) tmp_score = int(path_dic["time"]) + int(start_path_dic["time"]) if tmp_score < min_score: start_path_str_res = start_path path_str_res = pathStr min_score = tmp_score else: tmp_score = int(start_path_dic["time"]) if tmp_score < min_score: start_path_str_res = start_path path_str_res = "" min_score = tmp_score if path_str_res == "": res_path = eval(start_path_str_res) else: res_path = combine_path(start_path_str_res, "", "1", "0", path_str_res) res_path["start_id"] = start res_path["dest_id"] = dest res_path["start_map"] = start.split('#')[-1] res_path["dest_map"] = dest.split("#")[-1] return str(res_path)