Example #1
0
def case1_vehicle(interval):
	sig = 5
	# Algorithm parameters
	n = 100  # number of samples
	K = 100  # minimum number of candidate paths
	vmean = 10. # mean velocity
	vvar = 4. # velocity variance

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()

	cur.execute('''select measurements, gt_manul_edges, test_id
		from testcases
		where time_slot=%s and gt_manul_edges != '{}'
		''', (interval,))

	tests = cur.fetchall()
	results = []
	for test in tests:
		allmeasurements_id = test[0]
		manul_route = test[1]
		locations,timestamp = get_exact_record(allmeasurements_id,cur)
		zl = [locations[0],locations[-1]]
		t = [timestamp[0],timestamp[-1]]
		allmeasurements = get_allmeasurements(allmeasurements_id,cur)

		G= testdb.london_roadmap(allmeasurements_id)
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t

		if len(G.edges())!=501 and nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				
				if allmeasurements:
					# tpath = real_path(G,allmeasurements,startvertex,endvertex,cur)
					# print "TRUE PATH", tpath
					# tedge = get_edges(tpath,cur)
					tedge = manul_route
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						try:
							area = surrounding_area(edge,pp[j][0],tedge,startvertex,cur)
						except:
							conn.rollback()
							area = surrounding_area(edge,pp[j][-1],tedge,startvertex,cur)
						result.append({'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur,tedge),
							'area':area,
							'common_length':share_distance(edge,pp[j][0],tedge,startvertex,cur),
							'edgeIDList':edge
							})
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],"test_id":test[2]}
					results.append(newtest)
					print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
	return results
Example #2
0
def case1_vehicle(vehicleid,interval):
	boundingboxratio=0.5
	sig = 5
	# Algorithm parameters
	n = 200  # number of samples
	K = 50  # minimum number of candidate paths
	vmean = 6. # mean velocity
	vvar = 4. # velocity variance
	includetruepath=False# without inputting true path

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()
	pointsqlx ="ST_X(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	pointsqly ="ST_Y(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	cur.execute("select id, timestamp,"+pointsqlx+","+pointsqly+" from near_vertex_measurement where vehicleid=%s and type=%s order by timestamp",vehicleid)
	vehicle_records = MeasureList([measurement for measurement in cur],interval)
	zl,tl,idl=vehicle_records.next()
	# mesnum = len(rows)
	results = []
	while zl:
		starttime=tl[0]
		t=[(item-starttime).total_seconds() for item in tl]
		allmeasurements = get_allmeasurements(idl,vehicleid,cur)
		allmeasurements_id = [measurement[0] for measurement in allmeasurements]
		addrow_query = "INSERT INTO testcases3(time_slot, start_num, end_num, measurements) VALUES (%s, %s, %s, %s) RETURNING test_id;"
		measurementsstr = "{%s}" % (','.join([str(m_id) for m_id in allmeasurements_id]),)
		cur.execute(addrow_query,(t[-1],idl[0],idl[-1],measurementsstr))
		testid = cur.fetchone()[0]
		conn.commit()

		minx, maxx, miny, maxy = cal_min_bbox(allmeasurements,cur)
		width=maxx-minx
		height=maxy-miny
		minx=minx-width*boundingboxratio/2.0
		maxx=maxx+width*boundingboxratio/2.0
		miny=miny-height*boundingboxratio/2.0
		maxy=maxy+height*boundingboxratio/2.0

		G=testdb.london_roadmap((minx,miny,maxx,maxy))
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t
		# large graph take too long time to process
		if nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				tedge,tpath = cal_route(allmeasurements_id,testid,cur)
				updateroute = "{%s}" % (','.join(tedge),)
				updatevertex = "{%s}" % (','.join([str(x) for x in tpath]),)
				cur.execute('''update testcases3 set gt_mm_edges=%s where test_id = %s''',(updateroute,testid))
				cur.execute('''update testcases3 set gt_mm_vertex=%s where test_id = %s''',(updatevertex,testid))
				conn.commit()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig,tpath if includetruepath else None)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				# print pp
				if allmeasurements and allmeasurements[0][0]==idl[0] and allmeasurements[-1][0]==idl[-1]:
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						sarea = surrounding_area(edge,pp[j][0],cur,testid)

						temproute = {'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur,allmeasurements_id,testid),
							'common_length':share_distance(edge,pp[j][0],cur,testid),
							'common_edges':common_edge(tedge,edge),
							'area':sarea,
							'edgeIDList':edge
							}
						result.append(temproute)
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],
						'measurements':allmeasurements_id,
						"tpath":tpath,
						"tedge":tedge,
						"vehicleid":vehicleid,
						"test_id":testid,
						'v':len(G.nodes()),
						'e':len(G.edges())}
					results.append(newtest)
					# conn.commit()
					if len(results)>30:
						return results
					else:
						print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
		zl,tl,idl=vehicle_records.next()
	return results
def case1_vehicle(vehicleid,interval):
	boundingboxratio=0.6
	sig = 5
	# Algorithm parameters
	n = 100  # number of samples
	K = 100  # minimum number of candidate paths
	vmean = 10. # mean velocity
	vvar = 4. # velocity variance

	conn = psycopg2.connect("dbname=gis user=postgres password=peach")
	cur = conn.cursor()
	pointsqlx ="ST_X(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	pointsqly ="ST_Y(ST_transform(ST_setsrid(ST_makepoint(longitude,latitude),4326),32630))"
	cur.execute("select id, timestamp,"+pointsqlx+","+pointsqly+" from near_vertex_measurement where vehicleid=%s and type=%s order by timestamp",vehicleid)
	vehicle_records = MeasureList([measurement for measurement in cur],interval)
	zl,tl,idl=vehicle_records.next()
	# mesnum = len(rows)
	results = []
	while zl:
		starttime=tl[0]
		t=[(item-starttime).total_seconds() for item in tl]
		allmeasurements = get_allmeasurements(idl,vehicleid,cur)

		minx, maxx, miny, maxy = cal_min_bbox(allmeasurements,cur)
		width=maxx-minx
		height=maxy-miny
		minx=minx-width*boundingboxratio/2.0
		maxx=maxx+width*boundingboxratio/2.0
		miny=miny-height*boundingboxratio/2.0
		maxy=maxy+height*boundingboxratio/2.0

		G=testdb.london_roadmap((minx,miny,maxx,maxy))
		if G.nodes():
			startvertex = nearest_vertex(G,zl[0])
			endvertex = nearest_vertex(G,zl[-1])

		print zl
		print t
		# large graph take too long time to process
		if 5000>len(G.nodes())>10 and nx.has_path(G,startvertex,endvertex) and startvertex!=endvertex:
			try:
				t0 = time.time()
				[pp,sep,wp] = case1.calcpostprobs(G,zl,t,n,K,vmean,vvar,sig*sig)
				et = time.time()-t0
				print et, "secs, done algorithm"
				possibleroutes = len(pp)
				
				if allmeasurements and allmeasurements[0][0]==idl[0] and allmeasurements[-1][0]==idl[-1]:
					tpath = real_path(G,allmeasurements,startvertex,endvertex,cur)
					# print "TRUE PATH", tpath
					tedge = get_edges(tpath,cur)
					result=[]
					for j in range(possibleroutes):
						# print "modeled path:",pp[j]
						edge = get_edges(pp[j],cur)
						try:
							area = surrounding_area(edge,pp[j][0],tedge,startvertex,cur)
						except:
							conn.rollback()
							area = surrounding_area(edge,pp[j][-1],tedge,startvertex,cur)
						result.append({'route':pp[j],
							'possibility':wp[j],
							'RMSE':RSME(edge,allmeasurements,cur),
							'temporal_RMSE':temporal_RSME(edge,allmeasurements,pp[j][0],cur),
							'area':area,
							'common_length':share_distance(edge,pp[j][0],tedge,startvertex,cur),
							'edgeIDList':edge
							})
					newtest = Test(result)
					newtest.test_para = {'period':t[-1],'measurements':[x[0] for x in allmeasurements],"tpath":tpath,"tedge":tedge,"vehicleid":vehicleid}
					results.append(newtest)
					if len(results)>30:
						return results
					else:
						print "Success.************************", len(results)
				else:
					print "vertx edge table error",idl,allmeasurements
			except:
				conn.rollback()
				print "FAIL............................."
		zl,tl,idl=vehicle_records.next()
	return results