示例#1
0
    def aggre_records(self):
        ards = []
        
        tmp_rds = [self.rds[0]]
        cent = self.rds[0]['gps_lonlat']

        def ards_add():
            tt = tmp_rds[len(tmp_rds) / 2]['time']
            hh = tmp_rds[len(tmp_rds) / 2]['head']
            ss = tmp_rds[len(tmp_rds) / 2]['speed']

            avg_rd = {'time':tt, 'head':hh, 'speed':ss, 'gps_lonlat':cent}
            ards.append(avg_rd)

        for i,rd in enumerate(self.rds[1:]):
            d = cg.lonlats2km(rd['gps_lonlat'], cent)
            if d < 0.01:
                tmp_rds.append(rd)
                cent = ((cent[0] + rd['gps_lonlat'][0]) / 2, (cent[1] + rd['gps_lonlat'][1]) / 2)
            else:
                ards_add()               
                tmp_rds = [rd]
                cent = rd['gps_lonlat']
       
        ards_add()

        return ards
def mm():
	for tbname_suffix in tbname_suffixs:
       		gw = cg.new_gw()
          	cdb.create_path_occupied_crusing_table(tbname_suffix,method=METHOD)#函数调用的时候,如果第一个参数使用了关键字绑定,后面的参数也必须使用关键字绑定!
        	cdb.create_path_occupied_crusing_table_attr(tbname_suffix,method=METHOD)
        	trd = cdb.new_track_reader_for_purpose(tbname_suffix,purpose='mm')
        	pwd = cdb.new_path_writer_for_method(tbname_suffix,method=METHOD)
        	pawd = cdb.new_path_attr_writer_for_method(tbname_suffix,method=METHOD)
        	#p2tj = cj.new_pt2geojson(method=METHOD)

		print ''
		start_at = datetime.datetime.now()
		print tbname_suffix + (' start at ' + str(start_at) + ' ').center(70, '-')

		print 'preparing...',
		alg.prepare(gw)
		print 'end'
		
		tracks_num = 0
		paths_failed = 0
		max_fetched = TRACKS_TO_ROW - TRACKS_FROM_ROW + 1
		
		start_match_at = datetime.datetime.now()
		print (' start matching at ' + str(start_match_at) + '').center(70, '-')

		while TRACKS_TO_ROW < 0 or trd.fetched_num < max_fetched:
			track = trd.fetch_one()

			if trd.fetched_num % 1000 == 0:
				print 'fetched', trd.fetched_num
			if track is None:
				break

			d_max = 0
        		pre_lonlat = track.rds[0]['gps_lonlat']
        		for i in range(1,len(track.rds)):
            			cur_lonlat = track.rds[i]['gps_lonlat']
          		        d = cg.lonlats2km(pre_lonlat, cur_lonlat)
           		        if d > d_max:
					d_max = d
           	       	        pre_lonlat = cur_lonlat
     			
			if d_max > 3 or track.length() >= 80:
				continue
			else:
				tracks_num += 1
			
			print 'track', str(track.tid).ljust(10),' ',
			if not match(pwd,pawd,gw,track):
				paths_failed += 1
		end_match_at = datetime.datetime.now()
		print(' end matching at ' + str(end_match_at) + ' ').center(70, '-'),'elapsed time',str(end_match_at - start_match_at)
		print 'fetched tracks: %s, paths failed: %s' % (trd.fetched_num, paths_failed)
		print ''.center(70, '-')
		
		#clear()
		end_at = datetime.datetime.now()
		print tbname_suffix + (' end at ' + str(end_at) + ' ').center(70, '-'),'elapsed time',str(end_at - start_at)
示例#3
0
 def length(self):
     l = 0
     pre_lonlat = self.rds[0]['gps_lonlat']
     for i in range(1,len(self.rds)):
         cur_lonlat = self.rds[i]['gps_lonlat']
         d = cg.lonlats2km(pre_lonlat, cur_lonlat)
         l = l + d
         pre_lonlat = cur_lonlat
     return l
示例#4
0
def influence_dag(dag, wi, rds):
    idag = dag.copy()
    es = dag.edges()
    for e in es:
        i = e[0][0]
        if i == wi:
            continue
        d_wi_i = cg.lonlats2km(rds[wi]['gps_lonlat'], rds[i]['gps_lonlat'])
        idag[e[0]][e[1]]['weight'] = dag[e[0]][e[1]]['weight'] * (2**(-d_wi_i))
    return idag
示例#5
0
def influence_dag(dag, wi, rds):
    idag = dag.copy()
    es = dag.edges()
    for e in es:
        i = e[0][0]
        if i == wi:
            continue
        d_wi_i = cg.lonlats2km( rds[wi]['gps_lonlat'], rds[i]['gps_lonlat'] )
        idag[e[0]][e[1]]['weight'] = dag[e[0]][e[1]]['weight'] * ( 2**(-d_wi_i) )
    return idag
示例#6
0
def create_dag_rds(gw, track, k=5, r=0.1, sigma=0.02):
    dag = nx.DiGraph()
    rds = []
    projss = []

    # for every gps-record in track, find its valid projection candidates
    for i in range(0, len(track.rds)):
        rd = track.rds[i]
        if i > 0:
            if time.mktime(track.rds[i]['time']) - time.mktime(
                    track.rds[i - 1]['time']) <= 0:
                continue
        lonlat_rds = rd['gps_lonlat']
        projs = gw.find_projs_within(lonlat_rds, r)
        # max candidates of each gps-record is k
        if len(projs) > k:
            ordered_projs = list(projs)
            ordered_projs.sort(key=operator.itemgetter('d_proj'))
            projs = tuple(ordered_projs[0:k])

        # if no projection candidates found, remove the gps-record from track
        if len(projs) > 0:
            rds.append(rd)
            projss.append(projs)

    # for every projection candidate of every gps-record, add vertex to DAG
    for i in range(0, len(projss)):
        projs = projss[i]
        for ii in range(0, len(projs)):
            proj = projs[ii]

            # vertex in DAG represented by tuple (i,ii)
            # 1) i is the index of gps-record in track
            # 2) ii is the index of projection candidate in i gps-record
            dag.add_node((i, ii), s = proj['s'], t = proj['t'], \
                    gps_lonlat = rds[i]['gps_lonlat'], \
                    proj_lonlat = proj['proj_lonlat'], \
                    l_s = proj['l_s'], \
                    l_t = proj['l_t'], \
                    d_proj = proj['d_proj'], \
                    weight = norm(0,sigma).pdf(proj['d_proj']))

    # for every pair of contiguous gps-records
    for i in range(0, len(projss) - 1):
        j = i + 1
        i_projs = projss[i]
        j_projs = projss[j]
        d_i_j = cg.lonlats2km(rds[i]['gps_lonlat'], rds[j]['gps_lonlat'])
        time_i_j = (time.mktime(rds[j]['time']) -
                    time.mktime(rds[i]['time'])) / 3600.0  # in hour
        if time_i_j <= 0:
            time_i_j = 0.0001

        # for every projection candidate ii of i gps-record
        for ii in range(0, len(i_projs)):
            ii_proj = i_projs[ii]
            s_ii = ii_proj['s']
            t_ii = ii_proj['t']
            speed_ii = gw.G[s_ii][t_ii]['speed']

            # for every projection candidate jj of j=i+1 gps-record
            for jj in range(0, len(j_projs)):
                jj_proj = j_projs[jj]
                s_jj = jj_proj['s']
                t_jj = jj_proj['t']
                speed_jj = gw.G[s_jj][t_jj]['speed']

                # calculate:
                # 1) path on map between ii and jj
                # 2) weight of path between ii and jj

                w_tii_sjj = 0
                if t_ii == s_jj:
                    p_tii_sjj = ()
                    ls_tii_sjj = [ii_proj['l_t'], jj_proj['l_s']]
                    vs_tii_sjj = [speed_ii, speed_jj]

                elif t_ii == t_jj and s_ii == s_jj:
                    p_tii_sjj = ()
                    ls_tii_sjj = [
                        jj_proj['l_s'] - ii_proj['l_s'],
                    ]
                    vs_tii_sjj = [
                        speed_ii,
                    ]

                    if abs(ls_tii_sjj[0]) < 0.00000001:
                        ls_tii_sjj = [
                            d_i_j,
                        ]
                    elif ls_tii_sjj[0] < 0:
                        p_tii_sjj = gw.shortest_path_from_to(
                            t_ii, s_jj, 'time')

                        if not len(p_tii_sjj) > 0:
                            w_tii_sjj = -INF
                        else:
                            ls_tii_sjj = [
                                ii_proj['l_t'],
                            ] + list(gw.get_edges_attr(p_tii_sjj,
                                                       'length')) + [
                                                           jj_proj['l_s'],
                                                       ]
                            vs_tii_sjj = [
                                speed_ii,
                            ] + list(gw.get_edges_attr(p_tii_sjj, 'speed')) + [
                                speed_jj,
                            ]
                else:
                    p_tii_sjj = gw.shortest_path_from_to(t_ii, s_jj, 'time')

                    if not len(p_tii_sjj) > 0:
                        w_tii_sjj = -INF
                    else:
                        ls_tii_sjj = [
                            ii_proj['l_t'],
                        ] + list(gw.get_edges_attr(p_tii_sjj, 'length')) + [
                            jj_proj['l_s'],
                        ]
                        vs_tii_sjj = [
                            speed_ii,
                        ] + list(gw.get_edges_attr(p_tii_sjj, 'speed')) + [
                            speed_jj,
                        ]

                if w_tii_sjj != 0:
                    dag.add_edge((i, ii), (j, jj),
                                 path=p_tii_sjj,
                                 weight=w_tii_sjj)
                    continue
                # add edge between ii and jj to DAG
                N_jj = norm(0, sigma).pdf(jj_proj['d_proj'])
                #ts = []
                #for a in range(0,len(ls_ii_jj)):
                #    ts.append(ls_ii_jj[a] / vs_ii_jj[a])
                #V_ii_jj = sum(ts)
                t_tii_sjj = 0
                for ilv in range(0, len(ls_tii_sjj)):
                    t_tii_sjj = t_tii_sjj + ls_tii_sjj[ilv] / vs_tii_sjj[ilv]
                l_tii_sjj = sum(ls_tii_sjj)
                if l_tii_sjj <= 0:
                    V_tii_sjj = 1
                else:
                    V_tii_sjj = d_i_j / l_tii_sjj
                Fs_tii_sjj = V_tii_sjj * N_jj
                avg_v = l_tii_sjj / time_i_j
                Ft_tii_sjj = sum(vs_tii_sjj) / math.sqrt(
                    sum([v**2
                         for v in vs_tii_sjj])) / math.sqrt(len(vs_tii_sjj))
                #Ft_tii_sjj = math.sqrt(sum([(v-avg_v)**2, for v in vs_tii_sjj]))
                F_tii_sjj = Fs_tii_sjj * Ft_tii_sjj
                dag.add_edge((i, ii), (j, jj),
                             path=p_tii_sjj,
                             weight=F_tii_sjj)

    return {'dag': dag, 'rds': rds}
def mm():
    for tbname_suffix in tbname_suffixs:
        gw = cg.new_gw()
        cdb.create_path_occupied_crusing_table(
            tbname_suffix,
            method=METHOD)  #函数调用的时候,如果第一个参数使用了关键字绑定,后面的参数也必须使用关键字绑定!
        cdb.create_path_occupied_crusing_table_attr(tbname_suffix,
                                                    method=METHOD)
        trd = cdb.new_track_reader_for_purpose(tbname_suffix, purpose='mm')
        pwd = cdb.new_path_writer_for_method(tbname_suffix, method=METHOD)
        pawd = cdb.new_path_attr_writer_for_method(tbname_suffix,
                                                   method=METHOD)
        #p2tj = cj.new_pt2geojson(method=METHOD)

        print ''
        start_at = datetime.datetime.now()
        print tbname_suffix + (' start at ' + str(start_at) + ' ').center(
            70, '-')

        print 'preparing...',
        alg.prepare(gw)
        print 'end'

        tracks_num = 0
        paths_failed = 0
        max_fetched = TRACKS_TO_ROW - TRACKS_FROM_ROW + 1

        start_match_at = datetime.datetime.now()
        print(' start matching at ' + str(start_match_at) + '').center(70, '-')

        while TRACKS_TO_ROW < 0 or trd.fetched_num < max_fetched:
            track = trd.fetch_one()

            if trd.fetched_num % 1000 == 0:
                print 'fetched', trd.fetched_num
            if track is None:
                break

            d_max = 0
            pre_lonlat = track.rds[0]['gps_lonlat']
            for i in range(1, len(track.rds)):
                cur_lonlat = track.rds[i]['gps_lonlat']
                d = cg.lonlats2km(pre_lonlat, cur_lonlat)
                if d > d_max:
                    d_max = d
                pre_lonlat = cur_lonlat

            if d_max > 3 or track.length() >= 80:
                continue
            else:
                tracks_num += 1

            print 'track', str(track.tid).ljust(10), ' ',
            if not match(pwd, pawd, gw, track):
                paths_failed += 1
        end_match_at = datetime.datetime.now()
        print(' end matching at ' + str(end_match_at) + ' ').center(
            70, '-'), 'elapsed time', str(end_match_at - start_match_at)
        print 'fetched tracks: %s, paths failed: %s' % (trd.fetched_num,
                                                        paths_failed)
        print ''.center(70, '-')

        #clear()
        end_at = datetime.datetime.now()
        print tbname_suffix + (' end at ' + str(end_at) + ' ').center(
            70, '-'), 'elapsed time', str(end_at - start_at)
示例#8
0
def create_dag_rds(gw, track, k=5, r=0.1, sigma=0.02):
    dag = nx.DiGraph()
    rds = []
    projss = []

    # for every gps-record in track, find its valid projection candidates
    for i in range(0,len(track.rds)):
        rd = track.rds[i]
        if i > 0:
            if time.mktime(track.rds[i]['time']) - time.mktime(track.rds[i-1]['time']) <= 0:
                continue
        lonlat_rds = rd['gps_lonlat']
        projs = gw.find_projs_within(lonlat_rds, r)
        # max candidates of each gps-record is k
        if len(projs) > k:
            ordered_projs = list(projs)
            ordered_projs.sort(key = operator.itemgetter('d_proj'))
            projs = tuple(ordered_projs[0:k])
            

        # if no projection candidates found, remove the gps-record from track
        if len(projs) > 0:
            rds.append(rd)
            projss.append(projs)

    # for every projection candidate of every gps-record, add vertex to DAG
    for i in range(0,len(projss)):
        projs = projss[i]
        for ii in range(0,len(projs)):
            proj = projs[ii]

            # vertex in DAG represented by tuple (i,ii)
            # 1) i is the index of gps-record in track
            # 2) ii is the index of projection candidate in i gps-record
            dag.add_node((i, ii), s = proj['s'], t = proj['t'], \
                    gps_lonlat = rds[i]['gps_lonlat'], \
                    proj_lonlat = proj['proj_lonlat'], \
                    l_s = proj['l_s'], \
                    l_t = proj['l_t'], \
                    d_proj = proj['d_proj'], \
                    weight = norm(0,sigma).pdf(proj['d_proj']))

    # for every pair of contiguous gps-records
    for i in range(0,len(projss)-1):
        j = i + 1
        i_projs = projss[i]
        j_projs = projss[j]
        d_i_j = cg.lonlats2km(rds[i]['gps_lonlat'], rds[j]['gps_lonlat'])
        time_i_j = (time.mktime(rds[j]['time']) - time.mktime(rds[i]['time'])) / 3600.0   # in hour
        if time_i_j <= 0:
            time_i_j = 0.0001

        # for every projection candidate ii of i gps-record
        for ii in range(0,len(i_projs)):
            ii_proj = i_projs[ii]
            s_ii = ii_proj['s']
            t_ii = ii_proj['t']
            speed_ii = gw.G[s_ii][t_ii]['speed']

            # for every projection candidate jj of j=i+1 gps-record
            for jj in range(0,len(j_projs)):
                jj_proj = j_projs[jj]
                s_jj = jj_proj['s']
                t_jj = jj_proj['t']
                speed_jj = gw.G[s_jj][t_jj]['speed']

                # calculate:
                # 1) path on map between ii and jj
                # 2) weight of path between ii and jj

                w_tii_sjj = 0
                if t_ii == s_jj:
                    p_tii_sjj = ()
                    ls_tii_sjj = [ii_proj['l_t'], jj_proj['l_s']]
                    vs_tii_sjj = [speed_ii, speed_jj]

                elif t_ii == t_jj and s_ii == s_jj:
                    p_tii_sjj = ()
                    ls_tii_sjj = [jj_proj['l_s'] - ii_proj['l_s'],]
                    vs_tii_sjj = [speed_ii, ]

                    if abs(ls_tii_sjj[0]) < 0.00000001:
                        ls_tii_sjj = [d_i_j,]
                    elif ls_tii_sjj[0] < 0:
                        p_tii_sjj = gw.shortest_path_from_to(t_ii, s_jj, 'time')

                        if not len(p_tii_sjj) > 0:
                            w_tii_sjj = -INF
                        else:
                            ls_tii_sjj = [ii_proj['l_t'],] + list(gw.get_edges_attr(p_tii_sjj, 'length')) + [jj_proj['l_s'],]
                            vs_tii_sjj = [speed_ii, ] + list(gw.get_edges_attr(p_tii_sjj, 'speed')) + [speed_jj, ]
                else:
                    p_tii_sjj = gw.shortest_path_from_to(t_ii, s_jj, 'time')

                    if not len(p_tii_sjj) > 0:
                        w_tii_sjj = -INF
                    else:
                        ls_tii_sjj = [ii_proj['l_t'],] + list(gw.get_edges_attr(p_tii_sjj, 'length')) + [jj_proj['l_s'],]
                        vs_tii_sjj = [speed_ii, ] + list(gw.get_edges_attr(p_tii_sjj, 'speed')) + [speed_jj, ]


                if w_tii_sjj != 0:
                    dag.add_edge((i, ii), (j, jj), path = p_tii_sjj, weight = w_tii_sjj)
                    continue
                # add edge between ii and jj to DAG
                N_jj = norm(0,sigma).pdf(jj_proj['d_proj'])
                #ts = []
                #for a in range(0,len(ls_ii_jj)):
                #    ts.append(ls_ii_jj[a] / vs_ii_jj[a])
                #V_ii_jj = sum(ts)
                t_tii_sjj = 0
                for ilv in range(0,len(ls_tii_sjj)):
                    t_tii_sjj = t_tii_sjj + ls_tii_sjj[ilv] / vs_tii_sjj[ilv]
                l_tii_sjj = sum(ls_tii_sjj)
                if l_tii_sjj <= 0:
                    V_tii_sjj = 1
                else:
                    V_tii_sjj = d_i_j / l_tii_sjj
                Fs_tii_sjj = V_tii_sjj * N_jj
                avg_v = l_tii_sjj / time_i_j
                Ft_tii_sjj = sum(vs_tii_sjj) / math.sqrt(sum([v**2 for v in vs_tii_sjj])) / math.sqrt(len(vs_tii_sjj))
                #Ft_tii_sjj = math.sqrt(sum([(v-avg_v)**2, for v in vs_tii_sjj]))
                F_tii_sjj = Fs_tii_sjj * Ft_tii_sjj
                dag.add_edge((i, ii), (j, jj), path = p_tii_sjj, weight = F_tii_sjj)

    return {'dag':dag, 'rds':rds}