def sample_OD_latlngs_from_routes(routeFile,
                                  num=20,
                                  accept_rate=1,
                                  minDist=1000,
                                  maxDist=4000):
    # - accept_rate: prob accepting OD from routes in routeFile= .rou.xml
    # - minDist maxDist: air dist between OD.
    print("[ sample_OD_latlngs_from_routes ] " + routeFile)
    print(num, accept_rate, minDist, maxDist)
    res = []
    while len(res) < num:
        ode = sample_OD_edges_from_routes(routeFile, num, accept_rate)
        for e1, e2 in ode:
            ret = query_sumo_edge(e1, Server_IP_Port)
            j1, j2 = ret['to'], ret['from']
            ret1 = query_sumo_junction(j1, Server_IP_Port)
            ret2 = query_sumo_junction(j2, Server_IP_Port)
            latlng1 = [(ret1['lat'] + ret2['lat']) / 2,
                       (ret1['lng'] + ret2['lng']) / 2]  # mid of edge
            ret = query_sumo_edge(e2, Server_IP_Port)
            j1, j2 = ret['to'], ret['from']
            ret1 = query_sumo_junction(j1, Server_IP_Port)
            ret2 = query_sumo_junction(j2, Server_IP_Port)
            latlng2 = [(ret1['lat'] + ret2['lat']) / 2,
                       (ret1['lng'] + ret2['lng']) / 2]
            dist = get_dist_meters_latlng2(latlng1, latlng2)
            if dist >= minDist and dist <= maxDist:
                print("sampled frE %s to %s" % (e1, e2) +
                      " air-dist %.1f " % dist)
                res.append([latlng1, latlng2])
                if len(res) == num: break
    return res
def sample_OD_latlngs_from_indexed_routes(RoutesEdgesDefXmlFile, num,
                                          accept_rate, minDist, maxDist):
    print("[ sample_OD_latlngs_from_routes ] " + RoutesEdgesDefXmlFile)
    print(num, accept_rate, minDist, maxDist)
    res = {}
    while len(res) < num:
        oddic = sample_OD_edges_from_indexed_routes(RoutesEdgesDefXmlFile, num,
                                                    accept_rate)
        for rid in oddic.keys():
            e1, e2 = oddic[rid]
            ret = query_sumo_edge(e1, Server_IP_Port)
            j1, j2 = ret['to'], ret['from']
            ret1 = query_sumo_junction(j1, Server_IP_Port)
            ret2 = query_sumo_junction(j2, Server_IP_Port)
            latlng1 = [
                0.1 * ret1['lat'] + 0.9 * ret2['lat'],
                0.1 * ret1['lng'] + 0.9 * ret2['lng']
            ]
            ret = query_sumo_edge(e2, Server_IP_Port)
            j1, j2 = ret['to'], ret['from']
            ret1 = query_sumo_junction(j1, Server_IP_Port)
            ret2 = query_sumo_junction(j2, Server_IP_Port)
            latlng2 = [
                0.9 * ret1['lat'] + 0.1 * ret2['lat'],
                0.9 * ret1['lng'] + 0.1 * ret2['lng']
            ]
            dist = get_dist_meters_latlng2(latlng1, latlng2)
            if dist >= minDist and dist <= maxDist:
                print("sampled frE %s to %s" % (e1, e2) +
                      " air-dist %.1f " % dist)
                res[rid] = [latlng1, latlng2]
                if len(res) == num: break
    return res
Beispiel #3
0
def construct_edge(eid,
                   isInternal,
                   turn="",
                   allViaLanes=None,
                   tlsid=None,
                   internalNode=None):
    if not isInternal:
        edgeDic1 = query_sumo_edge(eid, Server_IP_Port)
        if edgeDic1 is None: print('\n   server.py not working ! ...')
        heading = edgeDic1['heading']
        ndDic = query_sumo_junction(edgeDic1["to"], Server_IP_Port)
        node1 = Node(str(edgeDic1["to"]),
                     x=ndDic["x"],
                     y=ndDic["y"],
                     lat=ndDic["lat"],
                     lng=ndDic["lng"],
                     incLanes=ndDic["incLanes"],
                     ntype=ndDic["type"],
                     tlsid=tlsid)
        edge = Edge(eid, heading, endNode=node1,
                    priority=edgeDic1['priority'])  # it is an edge.
    else:
        # Internal: https://sumo.dlr.de/wiki/Networks/SUMO_Road_Networks#Internal_Edges
        edge = Edge(eid.rsplit("_", 1)[0].rsplit("_", 1)[0],
                    turn=turn,
                    endNode=internalNode,
                    internalLaneID=eid,
                    priority=0)  # eid=lane id.
        edge.set_allViaLanes(allViaLanes)
    return edge
Beispiel #4
0
def show_obj(typ, obj): # be sure to turn on server.py.
	if typ == 'tls':
		eid= obj.inEdges[0]
		nid = query_sumo_edge(eid, Server_IP_Port)['to']
		show_obj('nodeID', nid)
	elif typ == 'nodeID':
		res = query_sumo_junction(obj, Server_IP_Port)
		GUI_show([res['x'],res['y']])
Beispiel #5
0
def mapLoopToSumoNet(data,netObj, pickleFile=None):  
	dcnt=0
	for dic in data:
		dcnt+=1
		print(dcnt,len(data))
		n, deltas = 0, [2., 6., 10. ,25. , 50, 90]
		sucess= 0
		while sucess==0: # allow retry as inc search radius r:
			candi=[]
			while len(candi)==0 and n< len(deltas):
				candi = netObj.getNeighboringEdges(dic['x'],dic['y'], r=deltas[n])
				n+=1
			if len(candi)==0: 
				print(dic)
				print("NOT Mapped by rtree !! Consider increase dist-delta r? ")
				break
			mapped = 0
			tried, minD = [],1e6
			for edge, d in candi:
				eid = edge.getID()
				res = query_sumo_edge(eid, Server_IP_Port)
				res["eid"]=eid
				res["rd"]=d
				tried.append(res)
				ehead = res['heading']
				if min_angle_diff(dic['hd'],ehead)>45:
					continue
				if d<minD:
					minD = d
					nlane = edge.getLaneNumber()
					dic['eid']= str(eid)
					dic['nlane']=nlane
					mapped=1
			if mapped==0:
				print('')
				print(dic)
				print("heading not match with these:?")
				print(tried)
			if mapped==1: 
				sucess= 1
		if sucess==0:
			raw_input("Fail. Check a2.py GUI, proceed? ...")
	if pickleFile:
		print('pickle.dump(data) to '+pickleFile)
		pickle.dump(data,open(pickleFile,"wb"))
	return data
Beispiel #6
0
def mapSpeedSamplesToSumoEdges(data, netObj, pickleFile=None): 
	# add edge info inplace, and dump again and overwrite.
	dcnt=0
	for dic in data:
		dcnt+=1
		print(dcnt,len(data))
		deltas = [2., 6., 10. , 20., 40., 80., 120]
		mapped , tried = 0,[]
		for n  in range(len(deltas)): # allow retry as inc search radius r:
			candi = netObj.getNeighboringEdges(dic['x'], dic['y'], r=deltas[n])
			if len(candi)==0: 
				continue
			tried, minD = [],1e6
			for edge, d in candi:
				eid = edge.getID()
				res = query_sumo_edge(eid, Server_IP_Port)
				res["eid"]=eid
				res["rd"]=d
				tried.append(res)
				ehead = res['heading']
				if min_angle_diff(dic['hd'],ehead)>45:
					continue
				if d<minD:
					minD = d
					nlane = edge.getLaneNumber()
					dic['eid']= str(eid)
					dic['nlane']=nlane
					mapped=1
			if mapped ==1: break
		if mapped==0:
			print(dic)
			print("? ? heading not match with any of:")
			pprint.pprint(tried)
			raw_input("Failed. Check with a2.py, proceed? ...")
	if pickleFile:
		print('pickle.dump(data) to '+pickleFile)
		pickle.dump(data,open(pickleFile,"wb"))
	return data
Beispiel #7
0
def map_trip_to_sumo_net(data, netObj, selected_hour_list, start_sec,
                         TaxiTripXmlFile, trip_prefix, vtype,
                         TaxiTripToStatsFile,
                         maxDepartureTime):  # use a1*.py to extract data.
    print('[ map_trip_to_sumo_net ] in#', len(data))
    dcnt = 0
    timed = []
    timeOffset = 3600 * min(selected_hour_list)
    minTime, maxTime = 1e9, 0
    for dic in data:
        dcnt += 1
        if dic['hour'] not in selected_hour_list:
            continue
        eid, tried = map_xy_to_sumo_edge(dic['x1'], dic['y1'], netObj)
        if eid is None:
            print(dic)
            for e in tried:
                res = query_sumo_edge(e, Server_IP_Port)
                print("1 Not " + e, res)
            print("1 NOT Mapped by rtree !! Consider increase dist-delta r? ")
            continue
        startEdge = eid
        eid, tried = map_xy_to_sumo_edge(dic['x2'], dic['y2'], netObj)
        if eid is None:
            print(dic)
            for e in tried:
                res = query_sumo_edge(e, Server_IP_Port)
                print("2 Not " + e, res)
            print("2 NOT Mapped by rtree !! Consider increase dist-delta r? ")
            continue
        endEdge = eid
        dic['eid1'] = startEdge
        dic['eid2'] = endEdge
        hour = dic['hour']
        minute = dic['minute']
        departSec = hour * 3600 + minute * 60 - timeOffset
        if departSec > maxTime: maxTime = departSec
        if departSec < minTime: minTime = departSec
        timed.append([departSec, dic])
        if dcnt % 100 == 1:
            print(dcnt, len(data))
            print(dic)
    timed.sort()
    tspan = maxTime - minTime
    minTime = start_sec
    maxTime = tspan + minTime

    deltaT = float(maxTime - minTime) / max(1, len(timed))
    trip2stats = {}
    with open(TaxiTripXmlFile, "w") as f:
        f.write(
            '<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">\n'
        )
        t = minTime
        ind = 0
        for _, dic in timed:
            tripID = trip_prefix + str(ind)
            wstr = '	<trip id="%s" depart="%.2f" from="%s" to="%s" type="%s" departLane="best" departSpeed="max" departPos="base" arrivalPos="max"/>\n' % (
                tripID, t, dic['eid1'], dic['eid2'], vtype)
            f.write(wstr)
            t += deltaT
            ind += 1
            trip2stats[tripID] = dic
        f.write('</routes>')
    print("Done " + TaxiTripXmlFile)
    print("pickle dump " + TaxiTripToStatsFile)
    pickle.dump(trip2stats, open(TaxiTripToStatsFile, "wb"))
Beispiel #8
0
def merge_phases(raw_phases, gran_level=LV_strict_mode, print_str=False):
    ''' merge [MovesOfPhase,  ..] 
	- gran_level: criteria granularity, following levels:  
		- strict_mode: only merge when fully match moves.
		- fromEdge_only: merge as long as one move shares from-edge.
		- guess_edge: merge if from-edges are in opposite direction.
		- direction_guess: merge if edge heading is opposite direction.
	'''
    if print_str: print('\n[ merge_phases ] in #%d' % len(raw_phases))
    allPhases = raw_phases[:]
    idmap = {}
    while 1:
        happened = 0
        mergedPhases = []
        while allPhases:
            mergedPhases.append(allPhases.pop(0))
            basePhase = mergedPhases[-1]
            j = 0
            while j < len(allPhases):
                check_phase = None
                if check_phase is None and gran_level >= LV_strict_mode:  # full all moves match valid.
                    if basePhase.moves == allPhases[j].moves:
                        check_phase = allPhases.pop(j)
                        happened += 1
                if check_phase is None and gran_level >= LV_at_least_one_move:  # at least one move matches.
                    inter = [
                        value for value in basePhase.moves
                        if value in allPhases[j].moves
                    ]
                    if len(inter) > 0:
                        check_phase = allPhases.pop(j)
                        happened += 1
                if check_phase is None:  # from same edge will merge.
                    fe1 = set([mv.fromEdge for mv in basePhase.moves])
                    fe2 = set([mv.fromEdge for mv in allPhases[j].moves])
                if check_phase is None and gran_level >= LV_fromEdge_only:  # guess if edge on same street/oppo dir.
                    if fe1 & fe2:
                        check_phase = allPhases.pop(j)
                        happened += 1
                if check_phase is None and gran_level >= LV_guess_edge:
                    fedge1 = set([e.lstrip("-").split("#", 1)[0]
                                  for e in fe1])  # extract main edge name.
                    fedge2 = set([e.lstrip("-").split("#", 1)[0] for e in fe2])
                    if fedge1 & fedge2:
                        check_phase = allPhases.pop(j)
                        happened += 1
                if check_phase is None and gran_level >= LV_direction_guess:  # guess if edge on oppo dir.
                    try:  # query edge server.
                        de1 = [
                            query_sumo_edge(e, Server_IP_Port)['heading']
                            for e in fe1
                        ]
                        de2 = [
                            query_sumo_edge(e, Server_IP_Port)['heading']
                            for e in fe2
                        ]
                        if all_same_heading(
                                de1, allow_180=True,
                                threshold=15.) and all_same_heading(
                                    de2, allow_180=True, threshold=15.):
                            if same_angle(de1[0],
                                          de2[0],
                                          allow_180=True,
                                          threshold=15.):
                                check_phase = allPhases.pop(j)
                                happened += 1
                        if check_phase is None:
                            if print_str: print(fe1, de1, "   !=   ", fe2, de2)
                    except:
                        print("[sumo_tls] Server %s is Not On...." %
                              Server_IP_Port)
                if check_phase:  # successfully merged.
                    basePhase.merge_phases_from_other(check_phase)
                    basePhase.merge_move_from_other(check_phase)
                    idmap[check_phase.id] = basePhase.id
                else:
                    j += 1
        allPhases = mergedPhases
        if happened == 0: break  # one scan may not be enough!
    # change id to reflect merge.
    for ph in mergedPhases:
        if ph.id in idmap: pd.id = idmap[ph.id]
        for duraPhase in ph.sameCycleNextPhasesDuras:
            pid = duraPhase[1].id
            if pid in idmap: duraPhase[1].id = idmap[pid]
    # re-check/remove self-loop in next list:
    for ph in mergedPhases:
        i = 0
        while i < len(ph.sameCycleNextPhasesDuras):
            duraPhase = ph.sameCycleNextPhasesDuras[i]
            if ph.id == duraPhase[1].id:
                ph.sameCycleNextPhasesDuras.pop(i)
            else:
                i += 1
    return mergedPhases